推 fengchu:學習了 08/08 19:53
※ 編輯: utf8 來自: 111.240.235.8 (08/23 00:46)
#include <stdio.h>
#include <stdint.h>
int main() {
char array[] =
"00:a2:39:84:37:24:ee:d1:de:25:04:25:09:95:40:"
"46:dd:e9:75:af:0f:b5:27:68:31:a4:d9:2a:fa:ae:"
"ec:94:e7:35:a9:a3:d7:4a:8d:66:6f:3b:19:95:3a:"
"df:f3:6e:47:5f:98:18:4a:08:df:e5:0a:fc:38:c6:"
"8a:bf:96:83:a4:96:4a:b8:8e:cb:d9:76:f5:98:f2:"
"95:84:da:c8:7d:65:71:0f:d4:24:64:18:d6:c8:32:"
"7d:ea:b9:bd:7e:be:46:b7:e2:a6:a0:49:01:f4:df:"
"8c:55:1e:cd:42:86:13:64:bb:e1:62:b2:3c:f1:59:"
"f0:3f:10:b4:e2:aa:6d:2a:e3:22:af:12:11:09:55:"
"9a:fa:8a:f0:be:1c:d6:7d:2e:10:37:1a:40:f3:df:"
"4e:a7:df:2b:de:1a:18:76:f2:7a:20:a3:dd:53:69:"
"6a:32:ed:31:e8:6f:11:93:49:5c:5f:85:7d:fa:8f:"
"7f:cb:83:45:7d:8f:fc:f6:c8:a9:cc:35:50:c2:6d:"
"9b:16:d1:22:12:18:06:c2:dc:55:0e:bc:38:57:ab:"
"3e:d9:4b:86:ce:e4:4c:d8:9d:14:44:9f:6c:38:3b:"
"68:6d:88:9d:c2:50:b0:e2:a9:94:e0:36:aa:a4:00:"
"e9:60:2e:69:0e:e6:a8:7d:77:f3:80:73:6f:16:4d:"
"18:53";
size_t offset = 0;
size_t count = sizeof(array) - 1;
int c;
uint32_t tmp[4];
union {
uint8_t u8[4];
uint32_t u32;
} out;
// if little endian
while(sscanf(array + offset, "%02x:%02x:%02x:%02x:%n", &tmp[3],
&tmp[2], &tmp[1], &tmp[0], &c) == 4 && offset + c <=
count) {
out.u8[3] = tmp[3];
out.u8[2] = tmp[2];
out.u8[1] = tmp[1];
out.u8[0] = tmp[0];
printf("%u\n", out.u32);
offset += c;
}
return 0;
}
※ 引述《liptonbin (我還存在耶)》之銘言:
: 請問一下
: 若是我有底下的一個array
: 想要把他轉成每4個為一組,
: 再印出十進位
: 恩, 我用冒號去判斷四個為一組
: 但是不知道若是用char array存好後,該怎麼轉存出10進位
: ex.
: 00:a2:39:84:37:24:ee:d1:de:25:04:25:09:95:40:
: 46:dd:e9:75:af:0f:b5:27:68:31:a4:d9:2a:fa:ae:
: ec:94:e7:35:a9:a3:d7:4a:8d:66:6f:3b:19:95:3a:
: df:f3:6e:47:5f:98:18:4a:08:df:e5:0a:fc:38:c6:
: 8a:bf:96:83:a4:96:4a:b8:8e:cb:d9:76:f5:98:f2:
: 95:84:da:c8:7d:65:71:0f:d4:24:64:18:d6:c8:32:
: 7d:ea:b9:bd:7e:be:46:b7:e2:a6:a0:49:01:f4:df:
: 8c:55:1e:cd:42:86:13:64:bb:e1:62:b2:3c:f1:59:
: f0:3f:10:b4:e2:aa:6d:2a:e3:22:af:12:11:09:55:
: 9a:fa:8a:f0:be:1c:d6:7d:2e:10:37:1a:40:f3:df:
: 4e:a7:df:2b:de:1a:18:76:f2:7a:20:a3:dd:53:69:
: 6a:32:ed:31:e8:6f:11:93:49:5c:5f:85:7d:fa:8f:
: 7f:cb:83:45:7d:8f:fc:f6:c8:a9:cc:35:50:c2:6d:
: 9b:16:d1:22:12:18:06:c2:dc:55:0e:bc:38:57:ab:
: 3e:d9:4b:86:ce:e4:4c:d8:9d:14:44:9f:6c:38:3b:
: 68:6d:88:9d:c2:50:b0:e2:a9:94:e0:36:aa:a4:00:
: e9:60:2e:69:0e:e6:a8:7d:77:f3:80:73:6f:16:4d:
: 18:53
: -------------------------------------------------------------------
: 分四個為一組
: 00a23984, 3724eed1, de250425, 09954046,....(16進位)
: ------------------------------------------------------------------
: 印出10進位
: 10631556, 925167313,3726967845,160776262,...(10進位)
: 謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)