2024年5月6日发(作者:)

字符串转16进制字节数组

view plaincopy to clipboardprint?

private static byte[] strToToHexByte(string hexString)

{

hexString = e(" ", "");

if (( % 2) != 0)

hexString += " ";

byte[] returnBytes = new byte[ / 2];

for (int i = 0; i < ; i++)

returnBytes[i] = (ing(i * 2, 2), 16);

return returnBytes;

}

private static byte[] strToToHexByte(string hexString)

{

hexString = e(" ", "");

if (( % 2) != 0)

hexString += " ";

byte[] returnBytes = new byte[ / 2];

for (int i = 0; i < ; i++)

returnBytes[i] = (ing(i * 2, 2), 16);

return returnBytes;

}

字节数组转16进制字符串

view plaincopy to clipboardprint?

public static string byteToHexStr(byte[] bytes)

{

string returnStr = "";

if (bytes != null)

{

for (int i = 0; i < ; i++)

{

returnStr += bytes[i].ToString("X2");

}

}

return returnStr;

}

public static string byteToHexStr(byte[] bytes)

{

string returnStr = "";

if (bytes != null)

{

for (int i = 0; i < ; i++)

{

returnStr += bytes[i].ToString("X2");

}

}

return returnStr;

}

从汉字转换到16进制

view plaincopy to clipboardprint?

public static string ToHex(string s, string charset, bool fenge)

{

if (( % 2) != 0)

{

s += " ";//空格

//throw new ArgumentException("s is not valid chinese string!");

}

ng chs = oding(charset);

byte[] bytes = es(s);

string str = "";

for (int i = 0; i < ; i++)

{

str += ("{0:X}", bytes[i]);

if (fenge && (i != - 1))

{

str += ("{0}", ",");

}

}

return r();

}

public static string ToHex(string s, string charset, bool fenge)

{

if (( % 2) != 0)

{

s += " ";//空格

//throw new ArgumentException("s is not valid chinese string!");

}

ng chs = oding(charset);

byte[] bytes = es(s);

string str = "";

for (int i = 0; i < ; i++)

{