For example, convert "AE00CF" to 0XAE00CF, "3031" to New byte[]{0x30,0x31};
Public Static byte[] GetBytes (stringHexString, out intdiscarded) {Discarded=0; stringNewString =""; CharC; //Remove All None a-f, 0-9, characters for(intI=0; i) {C=Hexstring[i]; if(Ishexdigit (c)) NewString+=C; ElseDiscarded++; } //if odd number of characters, discard last character if(Newstring.length%2!=0) {Discarded++; NewString= Newstring.substring (0, newstring.length-1); } intByteLength = newstring.length/2; byte[] bytes =New byte[ByteLength]; stringHex; intj =0; for(intI=0; I<bytes. Length; i++) {hex=NewString (NewChar[] {newstring[j], newstring[j+1]}); Bytes[i]=hextobyte (hex); J= j+2; } returnbytes; }
After the debug results run, "There is no hextobyte in the current context" error, I think the reason for this error is: I have only defined local variables, not defined global variables.
Next, define the global variables later.
Private Static byte Hextobyte (string hex) { bytebyte. Parse (hex, System.Globalization.NumberStyles.HexNumber); return tt; }
That's it!
16 binary format string converted to byte[]