Hex string to byte []
Public static byte [] stringtobyte1 (string value)
{
Int Len = value. Length/2;
Byte [] ret = new byte [Len];
For (INT I = 0; I <Len; I ++)
RET [I] = (byte) convert. toint32 (value. substring (I * 2, 2), 16 );
Return ret;
}
Byte [] to hex string
1. bitconverter. tostring (bytes), and then drop "-" replace
2. Public static string convertbytestohex (byte [] value)
{
Int num = value. length;
Char [] charray = new char [num * 2];
Int num2 = 0;
For (INT Index = 0; index <(Num * 2); index + = 2)
{
Byte num5 = value [num2 ++];
Charray [Index] = gethexvalue (num5/0x10 );
Charray [index + 1] = gethexvalue (num5% 0x10 );
}
Return new string (charray );
}
Private Static char gethexvalue (int I)
{
If (I <10)
{
Return (char) (I + 0x30 );
}
Return (char) (I-10) + 0x41 );
}
3. Private Static char [] hexdigits = {'0', '1', '2', '3', '4', '5', '6 ', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F '};
Internal static string tohexstring (byte [] bytes)
{
Int num = bytes. length;
Char [] chars = new char [num * 2];
For (INT I = 0; I <num; I ++)
{
Int B = bytes [I];
Chars [I * 2] = hexdigits [B> 4];
Chars [I * 2 + 1] = hexdigits [B & 0xf];
}
Return new string (chars );
}
Conclusion: The third is the most efficient.