Home > Net >  Look at elder advice StringToHexByte transformation, the second day
Look at elder advice StringToHexByte transformation, the second day

Time:09-30

Docking with a company's ability to each other as Java USES bcprov - jdk16-143. The jar package methods in the Hex, decode () to decrypt a hexadecimal string, I now want to use. Net implement this method, failure, unable to follow-up work, hope the great god directions,
Debugging results are as follows:
 
Var hexString="6 ff5cb38ddef84d5b7fdaadcbe249061";
//CSDN great god code, the output is: 111-245-203-56-221-239-132-213-183-253-170-220-190-36-144-97 (ToString (), digital byte value)
//and Hex. Decode (), output is not consistent: the 111-11-5 356-35-17-124-43-73-3-8 6-36-6636-11297
Public static byte [] ToHex (this string hexString)
{
Var datas=new byte [hexString. Length/2];
Var j=0;
For (var I=0; i Datas [j++]=the Convert. ToByte (hexString. Substring (I, 2), 16);
Return datas;
}

Hex. Decode () code below
 
Public static byte [] decodeHexUpper (String STR) throws UnsupportedEncodingException {
Return Hex. Decode (STR. ToLowerCase (Locale. US));
}

To decompiled jars for Hex. Decode () source for
 
Public int decode (final String s, final OutputStream OutputStream) throws IOException {
int n=0;
Int length;
For (length=s.l ength (); Length & gt; 0 & amp; & Ignore (s.c harAt (length - 1)); - length) {}
int i=0;
While (I & lt; Length) {
While (I & lt; Length & amp; & Ignore (s.c harAt (I))) {
+ + I;
}
Byte b;
For (b=this. DecodingTable [s.c harAt (i++)]; i Final byte b2=this. DecodingTable [s.c harAt (i++)];
If ((b | b2) & lt; 0 {
Throw new IOException (" invalid characters encountered in Hex string ");
}
OutputStream. Write (b & lt; <4 | b2);
+ + n.
}
Return n.
}


CodePudding user response:

Java code public static byte [] decodeHexUpper (String STR) throws UnsupportedEncodingException
Can't be output 111-11 - 5356-35... Because byte maximum value is 255, obviously can't be a 5356 byte output,
You may want to check whether the output of the Java example,

Java Hex. Decode () source have to skip the Hex character processing, is this your c # realize no,
Hex. For example, decode () can decode "f5 cb 6 f," you can't, c # now see only the little difference,

CodePudding user response:

reference 1st floor github_36000833 response:
Java code public static byte [] decodeHexUpper (String STR) throws UnsupportedEncodingException
Can't be output 111-11 - 5356-35... Because byte maximum value is 255, obviously can't be a 5356 byte output,
You may want to check whether the output of the Java example,

Java Hex. Decode () source have to skip the Hex character processing, is this your c # realize no,
Hex. For example, decode () can decode "f5 cb 6 f," you can't, c # now see only the little difference,

Github_36000833 sorry correction under the Java output: 53 56-111-11-17-124-35-43-73-3-86-97-36-66-36-112 is negative
At the same time, under the correct output: c # 56, 221, 239, 132, 111, 245, 203, 213, 183, 253, 170, 220, 190, 36, 144, 97

Is there any way to analysis the this

CodePudding user response:

Byte - & gt; Sbyte
 
Var datas=new sbyte [hexString. Length/2];
Var j=0;
For (var I=0; i Datas [j++]=the Convert. ToSByte (hexString. Substring (I, 2), 16);

CodePudding user response:

?? A signed number, that is sbyte, not byte

CodePudding user response:

reference 4 floor wanghui0380 response:
?? A signed number, that is sbyte, not byte

Or with your c # ToHex (this string hexString), print the displayed a there are symbols of the output,
 static void Main () 
{
Var hexString="6 ff5cb38ddef84d5b7fdaadcbe249061";
Var bytes=hexString. ToHex ();

//output 53 56-111-11-17-124-35-43-73-3-86-36-66-36-112-97
Console. WriteLine (string. Join (" ", bytes. The Select (x=& gt; (sbyte) x)));
}

As for why, 245, 203, with a signed number became - 11-53, you can search the relevant knowledge such as' 2 complement,
The 256-11=245
The 256-53=203

CodePudding user response:

Upstairs, positive solutions shows that the output of ask, in fact, your transformation is no problem,

CodePudding user response:

In c #, byte unsigned 8-bit integer, and Sbyte signed 8-bit integer, the corresponding Java type byte,
  •  Tags:  
  • C#
  • Related