unsigned char to String

Posted on 2006-05-17
Last Modified: 2008-02-01
  I want to combine the unsigned char into a string array, how can I do that?  

void outputMAC(unsigned char MAC[])
    printf("Your MAC Address Is: %02X-%02X-%02X-%02X-%02X-%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
//       finalmac=MAC[0]+MAC[1]+MAC[2]+MAC[3]+MAC[4]+MAC[5];


MAC[0]=00 in HEX
MAC[1]=04 in HEX
MAC[2]=75 in HEX
MAC[2]=82 in HEX
MAC[2]=26 in HEX
MAC[2]=EA in HEX

how can I reformat them into array that store "0004758226EA" into arrary

because i need to perform xor with another array


0 0 0 4
1 6 4 A   xor

Pls help

Question by:goretti
    LVL 8

    Accepted Solution

    I'm not sure if I really see what you mean, but doing:

     sprintf(TheString, "%02X%02X%02X%02X%02X%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);

    should set TheString to "0004758226EA". What is the declaration of your a array? Note that if you want:

    7 xor 5 to become 2   (111 xor 101 -> 010)

    you can't use:

    "7" xor "5" as strings, because the ascii code for "7" is 55, so you will be doing:

    55 xor 53 and getting 6 (100101 xor 100011 -> 000110)

    I think what you need might be some shift operations to separate the 2 digit MAC values into single digit values:

    NewMac[0] = Mac[0] >> 4;
    NewMac[1] = Mac[0] - NewMac[0] << 4;
    NewMac[2] = Mac[1] >> 4;
    NewMac[3] = Mac[1] - NewMac[2] << 4;

    or even quicker, add the a values instead:

    NewA[0] = a[0] << 4 + a[1];
    NewA[1] = a[2] << 4 + a[3];

    Hope this helps you along! If not, explain a little more what you want to do. It's always easier to get a good answer when you write exactly what you need to _do_, not only your idea of how to approach it :-) For this question, that would mean telling exactly what kind of result you need and possibly also what you will be using it for next...
    LVL 8

    Expert Comment

    I just realized this question is a continuation of, would have been nice if you pointed that out in your question :-)

    For the exact problem you adress there, I think:

    sprintf(TheResult, "%01X%02X", (MAC[4] & 0xF)^rgValue[9], MAC[5]^(rgValue[10] << 4 + rgValue[11]));

    is quite efficient, but maybe not so easy to follow at first glance...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now