我有一个字节数组的50
字节表示5
整数ascii字符值。每个整数值被表示为10
字节:c#将ascii值的字节数组转换为整型数组
byte[] receiveBytes = new byte[] {
20, 20, 20, 20, 20, 20, 20, 20, 20, 49, // 9 spaces then '1'
20, 20, 20, 20, 20, 20, 20, 20, 20, 50, // 9 spaces then '2'
20, 20, 20, 20, 20, 20, 49, 50, 51, 52, // 6 spaces then '1' '2' '3' '4'
20, 20, 20, 20, 20, 20, 53, 56, 48, 49, // 6 spaces then '5' '8' '0' '1'
20, 20, 20, 20, 20, 20, 20, 57, 57, 57}; // 7 spaces then '9' '9' '9'
请,通知,20
是space
和[48..57]
一个ASCII码是0..9
位ASCII码。
如何将字节数组转换为整型数组(int[] intvalues == [1, 2, 1234, 5801, 999]
)?
我也第一次尝试到字节数组转换为字符串,然后串到整数这样的:
string[] asciival = new string[10];
int[] intvalues = new int[5];
Byte[] receiveBytes = '20202020202020202049 //int value = 1
20202020202020202050 //int value = 2
20202020202049505152 //int value = 1234
20202020202053564849 //int value =5801
20202020202020575757';//int value = 999
asciival[0] = Encoding.ASCII.GetString(receiveBytes, 0, 10);
asciival[1] = Encoding.ASCII.GetString(receiveBytes, 10, 10);
intvalues[0] = int.Parse(asciival[0]);
intvalues[1] = int.Parse(asciival[1]);
但是是不是有一个更简单的字节数组复制到字符串数组的方式?
也许与http://stackoverflow.com/questions/6165171/convert-byte-复制array-to-int – GSP
如何制作10个字节宽的int? 'sizeof(int)== 4' –