0

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?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
derdomml
  • 43
  • 7

2 Answers2

0

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.

rmfeldt
  • 101
  • 1
  • 4
  • With this change in my for loop, it now looks like this: `for(int i = 5; i >= 0; i--) { swprintf(&macString[i * 2], L"%02X", address->adrr[i]); }` But my WCHAR Array still seems to stay empty. What am I missing? It does also not work when accessing the array using `macString + i * 2` – derdomml Dec 08 '20 at 09:54
  • Update: I have to correct myself. I now have only the last octet with the value of `address->addr[0]`, but not the others. I also seem not to be able to debug the code because VS is telling me I do not have source code available for the swprintf part... – derdomml Dec 08 '20 at 10:01
0

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++;
    }
}
derdomml
  • 43
  • 7