Convert HEX MAC-Address to WCHAR Array in C/C++ on a WinCE 6 Device using wprintf()

335 views Asked by At

The last days, I've been searching and reading through several dozens of questions on ere that sound similar to my problem, but I have not yet managed to get any solution to work.

Consider the following, simple scenario:

I am programming a C/C++-App for an HMI-Device running on Windows CE 6.0. For our custom sources to work, we have to use VS 05 as an environment. I have to store a MAC address to and read it from an .ini file and print it from time to time, hence I need to be able to convert the 6-byte MAC from hex to a string and back.

The environment forces me to use WCHAR instead of usual chars.

The MACs are saved in own structs, given by the bluetooth API:

typedef struct bd_addr_t
{
     uint8_addr[6];
}bd_addr;

So, what I've been trying to do is write the corresponding hex values into a buffer wchar array like so: (always 13 long, 12 for 6*2 digits in the MAC, 1 for '\0'delimiter)

void MacToString(bd_addr *address, WCHAR *macString)
{
    macString[12] = L'\0';
    for(int i = 5; i >= 0; i--)
    {
        wprintf(macString + i*2, L"%02X", address->addr[i]);
    }
}

I have also tried to access the String using &macString[i + 2] but I cannot seem to get anything into the string. Printing out only wprintf(L"%02X", address->addr[i]) gives me the correct string snippets so something with the way I am trying to store the WCHAR array seems to be incorrect.

How can I store the uint_8s as 2 digit hex values inside a WCHAR string?

2

There are 2 answers

2
rmfeldt On BEST ANSWER

In your example you use wprintf with a target buffer, I think you want swprintf. wprintf will use whatever is in your macString as a format and your format will be a parameter as it is written above.

0
derdomml On

I did not think of how the MACs are saved backwards in the struct, so I got it to work using the following code:

void MacToString(bd_addr *address, WCHAR *macString)
{
    int strCount = 0;
    macString[13] = L'\0';

    for(int i = 5; i >= 0; i--)
    {
        swprintf(&macString[strCount * 2], L"%02X", address->addr[i]);
        strCount++;
    }
}