Search code examples
c++charstoreunsigned

Storing unsinged char?


I'm trying to get the mac address of a user, then store it in a string. I've got the mac address get function down, however I'm having a bit of trouble storing the final value of the mac address into a string. Can anybody help out?

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <rpc.h>
#include <rpcdce.h>
#include <string>
#pragma comment(lib, "rpcrt4.lib")

using namespace std;


static void PrintMACaddress(unsigned char MACData[])
{
    printf("%02X-%02X-%02X-%02X-%02X-%02X\n", MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
}

static void GetMACaddress(void)
{
    unsigned char MACData[6];

    UUID uuid;
    UuidCreateSequential(&uuid);

    for (int i=2; i<8; i++)
        MACData[i - 2] = uuid.Data4[i];

    PrintMACaddress(MACData);
}

int main()
{
    GetMACaddress();
    system("pause");
}

Basiclly, I need the final result into a string value Sorry if this is too broad.


Solution

  • You can use snprintf() function to convert to string:

    char buffer[1024];
    snprintf(buffer, sizeof(buffer), "%02X-%02X-%02X-%02X-%02X-%02X",
    MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
    
    std::string str = buffer;