我有一個字節數組的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' –