convert String MAC address to char[6] in C

convert string to mac address in c
c++ parse mac address
mac address as integer
sscanf
mac address encoding
convert mac address to binary
convert mac to ip
mac address hexadecimal
char mac[] = "00:13:a9:1f:b0:88";
int a[6];
sscanf(mac, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], 
&a[5]);

generally, it worked. But when mac contains something like "0(x)" it breaks

for example

char mac[] = "01:13:a9:1f:b0:88"; // 01 became 00 in above code 

any trick?


This due to memory issues which caused by other parts of the program

**Keeping here for inspiration **

simple solution:

char mac[] = "00-13-a9-1f-b0-88";
int a[6];
sscanf(mac, "%x-%x-%x-%x-%x-%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);

Then copy values of ‘a’ to char array.

How do you convert a MAC address (in an array) to string in C , simple solution: char mac[] = "00-13-a9-1f-b0-88"; int a[6]; sscanf(mac, "%x-%x-%​x-%x-%x-%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);. Then copy values of 'a' to  A media access control address (MAC address) of a device is a unique identifier assigned to a network interface controller (NIC). For communications within a network segment, it is used as a network address for most IEEE 802 network technologies, including Ethernet, Wi-Fi, and Bluetooth.

sscanf knows to ignore leading zeros.

I've tried your code on windows with visual studio, and on linux with gcc and it works fine. I suggest you check again your results, as your program seems to work fine.

if you want your 'a' array be printed the same as the input use printf with %02x modifier

common.c, sscanf knows to ignore leading zeros. I've tried your code on windows with visual studio, and on linux with gcc and it works fine. I suggest you  Stack Overflow Public questions and answers; convert String MAC address to char[6] in C. Ask Question Concatenating a char into a string. 325.

Your code works fine. Check this

int main()
{
  char mac1[] = "0x2:0x13:0xa9:0x1f:0xb0:0x88";
  char mac2[] = "02:13:a9:1f:b0:88";
  int a[6];

  sscanf(mac1, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
  printf("%02x:%02x:%02x:%02x:%02x:%02x\n", a[0], a[1], a[2], a[3], a[4], a[5]);

  sscanf(mac2, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
  printf("%02x:%02x:%02x:%02x:%02x:%02x\n", a[0], a[1], a[2], a[3], a[4], a[5]);
}

Output:

02:13:a9:1f:b0:88
02:13:a9:1f:b0:88

Convert MAC Address String into Bytes, You could do this: char macStr[18]; int array[6] = {0x00, 0x0d, 0x3f, 0xcd, 0x02, 0x5f}; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",  This article discusses several ways to convert from System::String* to char* by using the following: Managed extensions for C++ in Microsoft Visual C++ .NET 2002 and in Microsoft Visual C++ .NET 2003; C++/CLI in Microsoft Visual C++ 2005 and in Microsoft Visual C++ 2008; Method 1 PtrToStringChars gives you an interior pointer to the actual

wpa_supplicant: common.c File Reference, #include "includes.h" #include "common.h" static int hex2num(char c) { if (c >= '0' && c Convert ASCII string to MAC address (colon-delimited format) * @txt: MAC Buffer for the MAC address (ETH_ALEN = 6 bytes) * Returns: 0 on success,  string representation of MAC to u_char* for compare I'm storing packet headers in a struct* "arpheader" that has a u_char* "sha" with a length 6 that is the source hardware address from an ARP packet.

src/utils/common.c File Reference, The code snippet converts MAC Address String Format into Bytes. iConunter < 6; ++iConunter) { unsigned int iNumber = 0; char ch; //Convert letter into lower case. ch "%02x%c%02x%c%02x%c%02x%c%02x%c%02x", 

How would you extract the bytes of a MAC address string into an , hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format) Buffer for the MAC address (ETH_ALEN = 6 bytes) Returns: Characters used 

Comments
  • It works fine, look at ideone.com/ZPvXVp
  • The code you posted works, 01 becomes 1 or 0x01 and gets written into a[0].
  • Your question title doesn't match the question. char[6] should be int[6].
  • Please post a minimal reproducible example and expected vs. actual output.
  • Did you google for any duplicates? This gets asked if not once per week then at least once a month
  • "sscanf knows to ignore leading zeros" is true with the %x and %d format specifiers but not with %i.
  • Yes, you are right. for %i, leading zero gives different behavior. here is a cite from the sscanf man page: %i Matches an optionally signed integer; the next pointer must be a pointer to int. The integer is read in base 16 if it begins with 0x or 0X, in base 8 if it begins with 0, and in base 10 otherwise. Only characters that correspond to the base are used.