C# 16进制与字符串、字节数组之间的转换
程序员文章站
2022-07-22 10:01:51
复制代码 代码如下:/// /// 字符串转16进制字节数组 /// ///
/// <summary>
/// 字符串转16进制字节数组
/// </summary>
/// <param name="hexstring"></param>
/// <returns></returns>
private static byte[] strtotohexbyte(string hexstring)
{
hexstring = hexstring.replace(" ", "");
if ((hexstring.length % 2) != 0)
hexstring += " ";
byte[] returnbytes = new byte[hexstring.length / 2];
for (int i = 0; i < returnbytes.length; i++)
returnbytes[i] = convert.tobyte(hexstring.substring(i * 2, 2), 16);
return returnbytes;
}
字节数组转16进制字符串
/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string bytetohexstr(byte[] bytes)
{
string returnstr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.length; i++)
{
returnstr += bytes[i].tostring("x2");
}
}
return returnstr;
}
从汉字转换到16进制
/// <summary>
/// 从汉字转换到16进制
/// </summary>
/// <param name="s"></param>
/// <param name="charset">编码,如"utf-8","gb2312"</param>
/// <param name="fenge">是否每字符用逗号分隔</param>
/// <returns></returns>
public static string tohex(string s, string charset, bool fenge)
{
if ((s.length % 2) != 0)
{
s += " ";//空格
//throw new argumentexception("s is not valid chinese string!");
}
system.text.encoding chs = system.text.encoding.getencoding(charset);
byte[] bytes = chs.getbytes(s);
string str = "";
for (int i = 0; i < bytes.length; i++)
{
str += string.format("{0:x}", bytes[i]);
if (fenge && (i != bytes.length - 1))
{
str += string.format("{0}", ",");
}
}
return str.tolower();
}
16进制转换成汉字
///<summary>
/// 从16进制转换成汉字
/// </summary>
/// <param name="hex"></param>
/// <param name="charset">编码,如"utf-8","gb2312"</param>
/// <returns></returns>
public static string unhex(string hex, string charset)
{
if (hex == null)
throw new argumentnullexception("hex");
hex = hex.replace(",", "");
hex = hex.replace("\n", "");
hex = hex.replace("\\", "");
hex = hex.replace(" ", "");
if (hex.length % 2 != 0)
{
hex += "20";//空格
}
// 需要将 hex 转换成 byte 数组。
byte[] bytes = new byte[hex.length / 2];
for (int i = 0; i < bytes.length; i++)
{
try
{
// 每两个字符是一个 byte。
bytes[i] = byte.parse(hex.substring(i * 2, 2),
system.globalization.numberstyles.hexnumber);
}
catch
{
// rethrow an exception with custom message.
throw new argumentexception("hex is not a valid hex number!", "hex");
}
}
system.text.encoding chs = system.text.encoding.getencoding(charset);
return chs.getstring(bytes);
}
复制代码 代码如下:
/// <summary>
/// 字符串转16进制字节数组
/// </summary>
/// <param name="hexstring"></param>
/// <returns></returns>
private static byte[] strtotohexbyte(string hexstring)
{
hexstring = hexstring.replace(" ", "");
if ((hexstring.length % 2) != 0)
hexstring += " ";
byte[] returnbytes = new byte[hexstring.length / 2];
for (int i = 0; i < returnbytes.length; i++)
returnbytes[i] = convert.tobyte(hexstring.substring(i * 2, 2), 16);
return returnbytes;
}
字节数组转16进制字符串
/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string bytetohexstr(byte[] bytes)
{
string returnstr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.length; i++)
{
returnstr += bytes[i].tostring("x2");
}
}
return returnstr;
}
从汉字转换到16进制
/// <summary>
/// 从汉字转换到16进制
/// </summary>
/// <param name="s"></param>
/// <param name="charset">编码,如"utf-8","gb2312"</param>
/// <param name="fenge">是否每字符用逗号分隔</param>
/// <returns></returns>
public static string tohex(string s, string charset, bool fenge)
{
if ((s.length % 2) != 0)
{
s += " ";//空格
//throw new argumentexception("s is not valid chinese string!");
}
system.text.encoding chs = system.text.encoding.getencoding(charset);
byte[] bytes = chs.getbytes(s);
string str = "";
for (int i = 0; i < bytes.length; i++)
{
str += string.format("{0:x}", bytes[i]);
if (fenge && (i != bytes.length - 1))
{
str += string.format("{0}", ",");
}
}
return str.tolower();
}
16进制转换成汉字
///<summary>
/// 从16进制转换成汉字
/// </summary>
/// <param name="hex"></param>
/// <param name="charset">编码,如"utf-8","gb2312"</param>
/// <returns></returns>
public static string unhex(string hex, string charset)
{
if (hex == null)
throw new argumentnullexception("hex");
hex = hex.replace(",", "");
hex = hex.replace("\n", "");
hex = hex.replace("\\", "");
hex = hex.replace(" ", "");
if (hex.length % 2 != 0)
{
hex += "20";//空格
}
// 需要将 hex 转换成 byte 数组。
byte[] bytes = new byte[hex.length / 2];
for (int i = 0; i < bytes.length; i++)
{
try
{
// 每两个字符是一个 byte。
bytes[i] = byte.parse(hex.substring(i * 2, 2),
system.globalization.numberstyles.hexnumber);
}
catch
{
// rethrow an exception with custom message.
throw new argumentexception("hex is not a valid hex number!", "hex");
}
}
system.text.encoding chs = system.text.encoding.getencoding(charset);
return chs.getstring(bytes);
}
上一篇: c# 调用.bat文件的实现代码
下一篇: c# 托盘双击不触发单击事件的实现方法