C#中的Byte,String,Int,Hex之间的转换函数。
2021-02-06 22:17
标签:空格 cte substring rri summary sci ret 数据还原 package /// return buffer; /// return sb.ToString().ToUpper(); /// string b = new string(c); string[] tmpary = hexstring.Trim().Split(‘ ‘); /// /** public static string Int2String(int str) public static int String2Int(string str) public static string StringToHexArray(string input) return sb.ToString().ToUpper(); #endregion } C#中的Byte,String,Int,Hex之间的转换函数。 标签:空格 cte substring rri summary sci ret 数据还原 package 原文地址:https://www.cnblogs.com/alps/p/11405320.html
/// The string containing the hex digits (with or without spaces).
///
public byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i {
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
}
}
/// The array of bytes to be translated into a string of hex digits.
///
public string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
{
sb.Append(Convert.ToString(b, 16).PadLeft(2, ‘0‘).PadRight(3, ‘ ‘));
}
}
/// 将一条十六进制字符串转换为ASCII
///
/// 一条十六进制字符串
///
public static string HexStringToASCII(string hexstring)
{
byte[] bt = HexStringToBinary(hexstring);
string lin = "";
for (int i = 0; i {
lin = lin + bt[i] + " ";
}
string[] ss = lin.Trim().Split(new char[] { ‘ ‘ });
char[] c = new char[ss.Length];
int a;
for (int i = 0; i {
a = Convert.ToInt32(ss[i]);
c[i] = Convert.ToChar(a);
}
return b;
}
/**/
///
/// 16进制字符串转换为二进制数组
///
/// 用空格切割字符串
///
public static byte[] HexStringToBinary(string hexstring)
{
byte[] buff = new byte[tmpary.Length];
for (int i = 0; i {
buff[i] = Convert.ToByte(tmpary[i], 16);
}
return buff;
}
///
/// 将byte型转换为字符串
///
/// byte型数组
///
private string ByteArrayToString(byte[] arrInput)
{
int i;
StringBuilder sOutput = new StringBuilder(arrInput.Length);
for (i = 0; i {
sOutput.Append(arrInput[i].ToString("X2"));
}
//将此实例的值转换为System.String
return sOutput.ToString();
}
/// 对接收到的数据进行解包(将接收到的byte型数组解包为Unicode字符串)
///
/// byte型数组
///
public string disPackage(byte[] recbytes)
{
string temp = "";
foreach (byte b in recbytes)
temp += b.ToString("X2") + " ";//ToString("X2") 为C#中的字符串格式控制符
return temp;
}
* int转byte[]
* 该方法将一个int类型的数据转换为byte[]形式,因为int为32bit,而byte为8bit所以在进行类型转换时,知会获取低8位,
* 丢弃高24位。通过位移的方式,将32bit的数据转换成4个8bit的数据。注意 &0xff,在这当中,&0xff简单理解为一把剪刀,
* 将想要获取的8位数据截取出来。
* @param i 一个int数字
* @return byte[]
*/
public static byte[] int2ByteArray(int i)
{
byte[] result = new byte[4];
result[0] = (byte)((i >> 24) & 0xFF);
result[1] = (byte)((i >> 16) & 0xFF);
result[2] = (byte)((i >> 8) & 0xFF);
result[3] = (byte)(i & 0xFF);
return result;
}
/**
* byte[]转int
* 利用int2ByteArray方法,将一个int转为byte[],但在解析时,需要将数据还原。同样使用移位的方式,将适当的位数进行还原,
* 0xFF为16进制的数据,所以在其后每加上一位,就相当于二进制加上4位。同时,使用|=号拼接数据,将其还原成最终的int数据
* @param bytes byte类型数组
* @return int数字
*/
public static int bytes2Int(byte[] bytes)
{
int num = bytes[3] & 0xFF;
num |= ((bytes[2] num |= ((bytes[1] num |= ((bytes[0] return num;
}
{
string S = Convert.ToString(str);
return S;
}
{
int a;
int.TryParse(str, out a);
int a1 = Convert.ToInt32(str);
return a1;
}
/*将int转为低字节在后,高字节在前的byte数组
b[0] = 11111111(0xff) & 01100001
b[1] = 11111111(0xff) & 00000000
b[2] = 11111111(0xff) & 00000000
b[3] = 11111111(0xff) & 00000000
*/
public byte[] IntToByteArray2(int value)
{
byte[] src = new byte[4];
src[0] = (byte)((value >> 24) & 0xFF);
src[1] = (byte)((value >> 16) & 0xFF);
src[2] = (byte)((value >> 8) & 0xFF);
src[3] = (byte)(value & 0xFF);
return src;
}
//将高字节在前转为int,低字节在后的byte数组(与IntToByteArray2想对应)
public int ByteArrayToInt2(byte[] bArr)
{
if (bArr.Length != 4)
{
return -1;
}
return (int)((((bArr[0] & 0xff) | ((bArr[1] & 0xff) | ((bArr[2] & 0xff) | ((bArr[3] & 0xff) }
{
char[] values = input.ToCharArray();
StringBuilder sb = new StringBuilder(input.Length * 3);
foreach (char letter in values)
{
// Get the integral value of the character.
int value = Convert.ToInt32(letter);
// Convert the decimal value to a hexadecimal value in string form.
string hexOutput = String.Format("{0:X}", value);
sb.Append(Convert.ToString(value, 16).PadLeft(2, ‘0‘).PadRight(3, ‘ ‘));
}
上一篇:Windows 10“数字权利激活”永久性激活!!!
下一篇:WPF 玫瑰图绘制
文章标题:C#中的Byte,String,Int,Hex之间的转换函数。
文章链接:http://soscw.com/index.php/essay/51906.html