を使用して、MAC アドレスを形式00:11:22:33:44:55
から 6 バイト配列に変換する関数がありますstrtol()
。現在、私の関数は基本的に正常に動作していますが、最初の変換を過ぎて適切なエラーチェックを行う方法を知りたいです。これまでのところ、私のコードは次のようになります。
char* pEnd = NULL;
errno = 0;
// wanmac shall be formatted like: "00:11:22:33:44:55"
// Check for valid index into switch_wan_macs array
if (idx<0 || idx> MAX_WAN_PORTS){
return BCM_E_FAIL;
}
// set wan_mac
//interpret the 6 bytes of the mac with base 16 (hex) while omitting the colons (move next pointer up by 1)
switch_wan_macs[idx].wan_mac[0] = strtol (wan_mac, &pEnd, 16);
if (pEnd == wan_man || errno == ERANGE) {
printf("Conversion of MAC string %s failed\n", wan_mac);
return BCM_E_FAIL;
}
switch_wan_macs[idx].wan_mac[1] = strtol (pEnd+1, &pEnd, 16);
switch_wan_macs[idx].wan_mac[2] = strtol (pEnd+1, &pEnd, 16);
switch_wan_macs[idx].wan_mac[3] = strtol (pEnd+1, &pEnd, 16);
switch_wan_macs[idx].wan_mac[4] = strtol (pEnd+1, &pEnd, 16);
switch_wan_macs[idx].wan_mac[5] = strtol (pEnd+1, NULL, 16);
return BCM_E_NONE;
その後の手順で、残りの文字列を元の文字列に実際に変換できます。一時的な配列を使用するのが最善char pEnd[6]
ですか?