I have extracted a MAC address into a char*
array such that each section of the array is a pair of char values.
mac[0] = "a1"
mac[1] = "b2"
...
mac[5] = "f6"
Basically I need to take the char arrays and convert them to an unsigned char such that the hex representation is the same as the original char values.
a1 in ascii -> 0xa1
What is the best way to convert char* to hex in C?
My C isn't the best, but this works using strtol(start, [stop], base)
#include <stdlib.h>
#include <stdio.h>
int main(char ** argv, int argc) {
char * input = "a1";
printf("%d\n", (unsigned char)strtol(input, NULL, 16));
}
#include <stdlib.h>
#include <stdio.h>
unsigned char mac_uchar[6];
for(i = 0;i < 6; ++i) {
mac_uchar = (unsigned char) strtol (&mac[n], (char **) NULL, 16);
}
I was slow in answering...
You can use sscanf for this purpose.
int mac_byte;
unsigned char byte;
sscanf(mac[0],"%x",&mac_byte);
byte = mac_byte & 0xFF;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.