接触了编程差不多刚满一年,遇到很多数制转换和字节转换的问题,每次总是要搜,索性自己整理下。
一、数值转换
1、十进制整数——>其他进制的字符串
public static string ToString(int value, int toBase);静态类Convert中ToString其中一个方法的定义
将 32 位带符号整数的值转换为其指定基的等效字符串表示形式
其中value:要转换的32位带符号整数
toBase:返回值的基数,必须是2、8、10或16
示例如下:
int decimalInt = 10;
//十进制(整型)——>二进制(字符串);八进制(字符串);十六进制(字符串)
Console.WriteLine(Convert.ToString(decimalInt, 2));
Console.WriteLine(Convert.ToString(decimalInt, 8));
Console.WriteLine(Convert.ToString(decimalInt, 16));//小写字母a
Console.WriteLine(Convert.ToString(decimalInt, 16).ToUpper());//大写字母A
Console.ReadKey();
2、其他进制的字符串——>十进制整数
public static int ToInt32(string value, int fromBase);静态类Convert中ToInt32其中一个方法的定义
将指定基数的数字的字符串表示形式转换为等效的 32 位有符号整数
其中 value:包含要转换的数字的字符串
fromBase: value 中数字的基数,它必须是 2、8、10 或 16
string binaryStr = "1010";
string octalStr = "24";
string hexadecimalStr = "FF";
//二进制(字符串);八进制(字符串);十六进制(字符串)——>十进制(整型)
Console.WriteLine(Convert.ToUInt32(binaryStr, 2));
Console.WriteLine(Convert.ToUInt32(octalStr, 8));
Console.WriteLine(Convert.ToUInt32(hexadecimalStr, 16));
Console.ReadKey();
3、二进制字符串、八进制字符串和十六进制字符串之间转换
先转换为十进制整数,然后再转换为目标的进制数,此处略。
4、字节——>其他进制的字符串
static void Main(string[] args)
{
byte binaryByte = 0b11111111;//前缀:0b或0B
byte decimalByte = 255; //十进制表示的字节前没有前缀
byte hexadecimalByte = 0xFF;//前缀0x或0X
//二进制(字节)——>二进制(字符串)、八进制(字符串)、十进制(字符串)、十六进制(字符串)
Console.WriteLine(Convert.ToString(binaryByte, 2));
Console.WriteLine(Convert.ToString(binaryByte, 8));
Console.WriteLine(Convert.ToString(binaryByte, 10));
Console.WriteLine(Convert.ToString(binaryByte, 16));
//十进制(字节)——>二进制(字符串)、八进制(字符串)、十进制(字符串)、十六进制(字符串)
Console.WriteLine(Convert.ToString(decimalByte, 2));
Console.WriteLine(Convert.ToString(decimalByte, 8));
Console.WriteLine(Convert.ToString(decimalByte, 10));
Console.WriteLine(Convert.ToString(decimalByte, 16));
//十六进制(字节)——>二进制(字符串)、八进制(字符串)、十进制(字符串)、十六进制(字符串)
Console.WriteLine(Convert.ToString(hexadecimalByte, 2));
Console.WriteLine(Convert.ToString(hexadecimalByte, 8));
Console.WriteLine(Convert.ToString(hexadecimalByte, 10));
Console.WriteLine(Convert.ToString(hexadecimalByte, 16));
Console.ReadKey();
}
二、十六进制字符串与字节数组之间的转换
在做项目的过程中,由于需要自定义通讯协议,所以常常用到十六进制字符串与字节数组之间的转换
static void Main(string[] args)
{
//声明一个十六进制的字节数组
byte[] hexByteArray = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
//声明两个不同的十六进制字符串
string hexString1 = "FFEEDDCCBBA";
string hexString2 = "FFEEDDCCBBAA";
//十六进制字符串转换为字节数组,并在控制台输出
Console.WriteLine("显示不出正确的字节表示"+HexStringToByteArray(hexString1));
Console.WriteLine("奇数个字符个数字节显示"+BitConverter.ToString(HexStringToByteArray(hexString1)));
Console.WriteLine("偶数个字符个数字节显示"+BitConverter.ToString(HexStringToByteArray(hexString2)));
//字节数组转换为十六进制字符串,并在控制台输出
//方案一
Console.WriteLine(ByteArrayToHexString(hexByteArray));
//方案二
Console.WriteLine(BitConverter.ToString(hexByteArray).Replace("-", string.Empty));
Console.ReadKey();
}
/// <summary>
/// 十六进制字符串转换为字节数组
/// </summary>
/// <param name="hexStr"></param>
/// <returns></returns>
public static byte[] HexStringToByteArray(string hexStr)
{
hexStr = hexStr.Replace(" ", "");//去除空格
if ((hexStr.Length % 2) != 0) //判断hexstring的长度是否为偶数
{
hexStr += "";
}
byte[] returnBytes = new byte[hexStr.Length / 2]; //声明一个长度为hexstring长度一半的字节组returnBytes
for (int i = 0; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(hexStr.Substring(i * 2, 2), 16);//将hexstring的两个字符转换成16进制的字节组
}
return returnBytes;
}
/// <summary>
/// 字节数组转换为十六进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string ByteArrayToHexString(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
}
正所谓条条大路通罗马,实现我们最终目标的可能有很多种方案,本人第一篇拙作,谈不上技术,权当自己的总结,如有不当之处请指出或补充。