0

I have a byte array of 6 elements which contains the MAC address of a WiFi chip. How do I convert this into a single value. For e.g. If the array is:

mac[0] = 208 
mac[1] = 181
mac[2] = 194
mac[3] = 193
mac[4] = 114
mac[5] = 219

How do I get a value like this: 208181194193114219 which in representation is essentially all the digits concatenated.

I tried AND'ing the individual mac IDs with 0xFFh and then bit-shifted them to the left but I see a value of 3250763216. This is the code:

uint32_t deviceID = 0;
for (int i = 0; i < 6; i++)
{
   deviceID += (mac[i] & 0xFFh) << (8 * i);
}
Serial.print("Device ID : "); Serial.println(deviceID);

1 Answer 1

1

You can do this:

#include <iostream>
#include <sstream>

int main() {
    std::stringstream ss;


    int mac[] = {208,181,194,193,114,219};

    for (unsigned i = 0; i < sizeof mac / sizeof mac[0]; ++i)
        ss << mac [i];

    int result;
    ss >> result;
    std::cout << result; //208181194193114219
}
Sign up to request clarification or add additional context in comments.

2 Comments

I need to know how to do this in C++.
sorry,check answer again.

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.