简体   繁体   English

使用 wprintf() 在 WinCE 6 设备上将 HEX MAC 地址转换为 C/C++ 中的 WCHAR 数组

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

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.我正在为在 Windows CE 6.0 上运行的 HMI 设备编写 C/C++ 应用程序。 For our custom sources to work, we have to use VS 05 as an environment.为了使我们的自定义源工作,我们必须使用 VS 05 作为环境。 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.我必须将 MAC 地址存储到一个.ini 文件并从中读取它并不时打印它,因此我需要能够将 6 字节 MAC 从十六进制转换为字符串并返回。

The environment forces me to use WCHAR instead of usual chars.环境迫使我使用 WCHAR 而不是通常的字符。

The MACs are saved in own structs, given by the bluetooth API: MAC 保存在自己的结构中,由蓝牙 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)所以,我一直在尝试做的是将相应的十六进制值写入缓冲区 wchar 数组,如下所示:(总是 13 长,MAC 中 6*2 位为 12, '\0'分隔符为 1)

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.我也尝试使用&macString[i + 2]访问字符串,但我似乎无法将任何内容放入字符串中。 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.仅打印出wprintf(L"%02X", address->addr[i])为我提供了正确的字符串片段,因此我尝试存储 WCHAR 数组的方式似乎不正确。

How can I store the uint_8s as 2 digit hex values inside a WCHAR string?如何将 uint_8s 存储为 WCHAR 字符串中的 2 位十六进制值?

In your example you use wprintf with a target buffer, I think you want swprintf.在您的示例中,您将 wprintf 与目标缓冲区一起使用,我认为您需要 swprintf。 wprintf will use whatever is in your macString as a format and your format will be a parameter as it is written above. wprintf 将使用您的 macString 中的任何内容作为格式,并且您的格式将是上面所写的参数。

I did not think of how the MACs are saved backwards in the struct, so I got it to work using the following code:我没有想到 MAC 是如何在结构中向后保存的,所以我使用以下代码让它工作:

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++;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM