0

I'm trying hard to convert char *macAddress into unsigned char mac[6] but without success so far. My input string looks like "00:10:6f:16:01:b3" and I want to devide it into mac[0] = 0x00, mac[1] = 0x10, etc..

I've tried to use memcpy like:

memcpy(&mac, (unsigned char)macAddress, sizeof mac);  

or same another ways like mac = (unsigned char *)macAddress and so on but nothing worked for me well.
Is there any correct way how to convert it without any precision loss?

6
  • 3
    (unsigned char)macAddress?? What makes you think you need or should cast a pointer to an unsigned char?? Commented Apr 20, 2017 at 21:19
  • memcpy is a C function. Where is you rminimal reproducible example and which language do you use? Remove the tag for the unrelated language. Commented Apr 20, 2017 at 21:22
  • What is in macAddress? Is it a string with colons like "00:10:6f:16:01:b3"? Commented Apr 20, 2017 at 21:28
  • Tell us what you're trying to do, not just what types the input and output have. Commented Apr 20, 2017 at 21:40
  • 2
    Four steps: 1) describe the structure of the data in your input; 2) describe the structure of the desired result; 3) describe the rule for transforming the data in your input into the desired result; 4) write the code. Commented Apr 20, 2017 at 21:40

1 Answer 1

2

You want to convert hexadecimal digits (i.e. the string "00") of the mac address to byte values (i.e. the value 0). The memcpy instead copies the value of the string digits (i.e. '0' is 48 or 0x30).

You can use sscanf for the correct conversion.

At least on Linux, you can also use the function ether_aton. See the man page.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.