十年河东,十年河西,莫欺少年穷
学无止境,精益求精
一个字节数组,如下:
List<byte> by2 = new List<byte>() {0x55,0x22 };//8789
其对应的十进制的值为:8789
从上图可知,C#中byte数组默认为低字节在前
再例如:
List<byte> by3 = new List<byte>() { 0x55, 0x22 ,0x11};//1122901
对应的十进制是:1122901
对应的16进制为:0x112255,如下:
经过上述两个案例,C#byte数组中是按照低字节在前的方式排列的。
C#中有什么方法转化字节流为Int吗?
List<byte> by2 = new List<byte>() {0x55,0x22 };//8789 int ResultInt16 = BitConverter.ToInt16(by2.ToArray(), 0); List<byte> by4 = new List<byte>() { 0x55, 0x22, 0x11, 0x01 };//1428295937 int ResultInt32 = BitConverter.ToInt32(by4.ToArray(), 0);
涉及到两个方法:BitConverter.ToInt16 和 BitConverter.ToInt32
但是::BitConverter.ToInt这个方法只能转化长度为2或者长度为4的字节数组,如果字节长度为3,则无法转化。
譬如:
List<byte> by3 = new List<byte>() { 0x55, 0x22 ,0x11};//1122901
因此,有必要做一个通用方法
在实际业务需求中,软硬件双方制定的通讯协议中经常为字节流指定是高字节在前?还是低字节在前?
因此,我总结的方法如下:
#region C# 字节转Int public static int GetValue(byte by) { return Convert.ToInt32(by); } #region 16进制转十进制 高字节在前 /// <summary> /// 16进制转十进制 高字节在前 /// </summary> /// <param name="Ary"></param> /// <returns></returns> public static int GetValue(List<byte> Ary) { double result = 0; var cm = 0; cm = Ary.Count; foreach (var item in Ary) { if (cm - 1 == 0) { result += item; } else { result += item * Math.Pow(16, (cm * 2 - 2)); cm--; } } return Convert.ToInt32(result); } /// <summary> /// 高字节在前 /// </summary> /// <param name="Ary"></param> /// <returns></returns> public static int GetValue(IEnumerable<byte> Ary) { double result = 0; var cm = 0; cm = Ary.Count(); foreach (var item in Ary) { if (cm - 1 == 0) { result += item; } else { result += item * Math.Pow(16, (cm * 2 - 2)); cm--; } } return Convert.ToInt32(result); } /// <summary> /// 16进制转十进制 具体参考十六进制的数值 /// </summary> /// <param name="Ary"></param> /// <returns></returns> public static int GetValue(List<int> Ary) { double result = 0; var cm = 0; cm = Ary.Count; foreach (var item in Ary) { if (cm - 1 == 0) { result += item; } else { result += item * Math.Pow(16, (cm * 2 - 2)); cm--; } } return Convert.ToInt32(result); } /// <summary> /// 16进制转十进制 /// </summary> /// <param name="Ary"></param> /// <returns></returns> public static int GetValue(IEnumerable<int> Ary) { double result = 0; var cm = 0; cm = Ary.Count(); foreach (var item in Ary) { if (cm - 1 == 0) { result += item; } else { result += item * Math.Pow(16, (cm * 2 - 2)); cm--; } } return Convert.ToInt32(result); } #endregion #region 16进制转十进制 低字节在前 /// <summary> /// 16进制转十进制 高字节在前 /// </summary> /// <param name="Ary"></param> /// <returns></returns> public static int GetValueLH(List<byte> Ary) { Ary.Reverse(); double result = 0; var cm = 0; cm = Ary.Count; foreach (var item in Ary) { if (cm - 1 == 0) { result += item; } else { result += item * Math.Pow(16, (cm * 2 - 2)); cm--; } } return Convert.ToInt32(result); } /// <summary> /// 低字节在前 /// </summary> /// <param name="Ary"></param> /// <returns></returns> public static int GetValueLH(IEnumerable<byte> Ary) { Ary.Reverse(); double result = 0; var cm = 0; cm = Ary.Count(); foreach (var item in Ary) { if (cm - 1 == 0) { result += item; } else { result += item * Math.Pow(16, (cm * 2 - 2)); cm--; } } return Convert.ToInt32(result); } /// <summary> /// 16进制转十进制 低字节在前 /// </summary> /// <param name="Ary"></param> /// <returns></returns> public static int GetValueLH(List<int> Ary) { Ary.Reverse(); double result = 0; var cm = 0; cm = Ary.Count; foreach (var item in Ary) { if (cm - 1 == 0) { result += item; } else { result += item * Math.Pow(16, (cm * 2 - 2)); cm--; } } return Convert.ToInt32(result); } /// <summary> /// 16进制转十进制 低字节在前 /// </summary> /// <param name="Ary"></param> /// <returns></returns> public static int GetValueLH(IEnumerable<int> Ary) { Ary.Reverse(); double result = 0; var cm = 0; cm = Ary.Count(); foreach (var item in Ary) { if (cm - 1 == 0) { result += item; } else { result += item * Math.Pow(16, (cm * 2 - 2)); cm--; } } return Convert.ToInt32(result); } #endregion #endregion
扩展:int数值转字节:
var by4 = BitConverter.GetBytes(8888);//长度四字节 var by2 = BitConverter.GetBytes(Convert.ToUInt16(8888));//长度两字节
C# base64 转 byte[]
static void Main(string[] args) { string btinfo = @"MDAwMDAwMDAwMDAwMDAwMAHgAMgB7wAATjcBAXEBqAADAGEM7QzoR0UPDOkM6wzrDOkM6QzsDOwM7QzrDOgM6gzpDOoM6QzpAkdFAAAAAAANAQEBAQEBAAAAAAAAAAAAAAA="; var Bys = Encoding.Default.GetBytes(btinfo); var Bse64Bys = Convert.FromBase64String(btinfo); Console.WriteLine("普通byte数组,8位表示一个普通字符,长度为:"+Bys.Length); Console.WriteLine("Base64byte数组,6位表示一个base64字符,长度为:" + Bse64Bys.Length); Console.ReadLine(); }
@天才卧龙的博客