In computer systems, the commonly used encoding generally uses a hexadecimal string instead of a binary string. Two hexadecimal numbers can represent exactly one byte, the hexadecimal format is shorter. during conversion, a hexadecimal number can have four digits at most. But when we writeProgramGenerally, byte arrays are used. How is the conversion between them? The following describes how they are converted to each other.
Hexadecimal string to byte array
Public Static Byte[] Hexstringtobyte (StringHexstring)
{
Char[] Charlist = hexstring. tochararray ();
Byte[] Resultlist =New Byte[Charlist. Length/2];
IntBytecount = 0;
For(IntI = 0; I <charlist. length; I + = 2)
{
// A byte is equivalent to two hexadecimal numbers.
ByteB = 0x00;
B | = convert. tobyte ("0x0"+ Charlist [I], 16 );
B <= 4;// Shift operation, four digits left
B | = convert. tobyte ("0x0"+ Charlist [I + 1], 16 );
Resultlist [bytecount] = B;
Bytecount ++;
}
ReturnResultlist;
}
Byte array to hexadecimal string
Public StringBytetostring (Byte[] Bytelist)
{
Stringbuilder sbresult =NewStringbuilder ();
Foreach(ByteOnebyteInBytelist)
{
StringTempstr = string. Format ("{0: X2 }", Onebyte );
Sbresult. append (tempstr );
}
ReturnSbresult. tostring ();
}