Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6673571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:38:34+00:00 2026-05-26T03:38:34+00:00

I need to convert a string 00:11:22:33:44:55 to an uint8_t[6] representing a mac. I

  • 0

I need to convert a string “00:11:22:33:44:55” to an uint8_t[6] representing a mac.
I tried on my own, read somewhere char can be casted to uint8_t, but I’m kinda exhausted to try on my own. 🙁

Maybe there is a function in the kernel which does what I want.

If not, here is my code, what do I do wrong?

char * cleaned_mac =NULL;
char * extractMac(unsigned char * shared_user_buffer, size_t offset) {
    char * buffer = kmalloc(17, GFP_KERNEL);
    cleaned_mac = kmalloc(13, GFP_KERNEL);
    int i = 0;
    strncpy(buffer, shared_user_buffer + offset, 17);
    printk("BUFFER [%s]\n", buffer);
    while (*buffer && i < 12) {
        if (isxdigit(*buffer)) {
            printk("BUFFER [%c]\n", *buffer);
            cleaned_mac[i] = *buffer;
            printk("CLEANED BUFFER [%c]\n", *cleaned_mac);
            i++;
        }
        ++buffer;
    }
    cleaned_mac[12]=0x00;
    printk("CLEANED BUFFER [%s]\n", cleaned_mac);
    return cleaned_mac;
}

calling it like:

uint8_t * mac;
mac = extractMac(shared_user_buffer, strlen(tmq_server_prefix));
printk(KERN_DEBUG "MAC[%s]\n", mac);

printk(KERN_DEBUG "MAC[%02x:%02x:%02x:%02x:%02x:%02x]\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

so when I give “08:00:27:19:1f:02” in the function the result is:

Oct 13 17:41:28 client2 kernel: [ 1953.179271] CLEANED BUFFER [080027191f02]
Oct 13 17:41:28 client2 kernel: [ 1953.179273] MAC[080027191f02]
Oct 13 17:41:28 client2 kernel: [ 1953.179276] MAC[30:38:30:30:32:37]

So 08 became 30 and 38 ? Why is that?

Solution inspired from Dave (thank you):

uint8_t * cleaned_mac = NULL;
uint8_t * extractMac(unsigned char * shared_user_buffer, size_t offset) {
    char *c;
    char * buffer = kmalloc(17, GFP_KERNEL);
    int p = 0;
    const char * sep = ":";
    cleaned_mac = kmalloc(ETH_ALEN * sizeof(uint8_t), GFP_KERNEL);
    strncpy(buffer, shared_user_buffer + offset, 17);

    while ((c = strsep(&buffer, sep))) {
        cleaned_mac[p++] = simple_strtol(c, NULL, 16);
    }
    return cleaned_mac;
}

Usage then:

uint8_t *  mac;
mac = extractMac(shared_user_buffer, strlen(tmq_server_prefix));
        printk(KERN_DEBUG "---------------MAC [%02x:%02x:%02x:%02x:%02x:%02x]\n",
                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:38:35+00:00Added an answer on May 26, 2026 at 3:38 am

    Tokenize the string, and call strtol on each result

    char *c;
    int p = 0;
    for(c=strtok(buffer, ",");c;c=strtok(NULL, ","))
         mac[p++] = strtol(c, NULL, 16);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert a string to a char array in C; how can
Need to convert a string to normal datetime format so it can be correctly
I have a string string_function_test.I need to convert this string into stringFunctionTest. How can
I need convert a String to a decimal in C#, but this string have
I am working on a tool where I need to convert string values to
I need to convert a string to another which has removed anything before the
I need to convert a string of text containing a long url into the
I need to convert a string to a monetary format of {###}.###.###,## that is
I need to convert a timestamp string to java.util.Date . E.g.: MMDDYYHHMMSS to MM-DD-YY
I have an xml document object that I need to convert into a string.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.