在 C 语言中如何将整数转换为 Unicode 字符串?

发布于 2024-08-23 10:54:24 字数 477 浏览 6 评论 0原文

我正在为嵌入式 USB 项目开发固件。我想要使​​用的生产编程器会自动将序列号写入设备闪存的指定内存地址。编程器将序列号存储为指定字节数的十六进制数字。例如,如果我告诉它在地址 0x3C00 处存储序列号 123456,我的内存看起来像这样:

0x3C00  -  00
0x3C01  -  01
0x3C02  -  E2
0x3C03  -  40
//(123456 in Hex = 1E240)

问题是,当我的主机应用程序从设备读取序列号时,它正在寻找 unicode 字符数组。所以我的序列号应该是......

{ '1','0',
  '2','0',
  '3','0',
  '4','0',
  '5','0',
  '6','0'}

我用C编写的固件中的So时,是否可以从闪存中检索十六进制序列号,将其编码为unicode char数组并将其存储在变量中内存?

I am working on the Firmware for an embedded USB project. The production programmer I would like to use automatically writes the Serial Number into the device flash memory at a specified memory address. The programmer stores the serial number as Hex digits in a specified number of bytes. For example, if I tell it to store the serial number 123456 at address 0x3C00 my memory looks like this:

0x3C00  -  00
0x3C01  -  01
0x3C02  -  E2
0x3C03  -  40
//(123456 in Hex = 1E240)

The problem is, when my host application reads the serial number from the device it is looking for a unicode char array. So my serial number should be ...

{ '1','0',
  '2','0',
  '3','0',
  '4','0',
  '5','0',
  '6','0'}

When the

So in my firmware, which I'm writing in C, is it possible to retrieve the hex serial number from flash, encode it into a unicode char array and store it in a variable in Ram?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦幻的味道 2024-08-30 10:54:24

看起来您想要:(

wsprintf(wchar_buffer, L"%d", serial_number)

假设您的序列号适合 32 位整数。无论如何,wsprintf 都会将您的序列号打印为 wchar (unicode) 字符串。

It seems like you want:

wsprintf(wchar_buffer, L"%d", serial_number)

(assuming that your serial number fits in a 32-bit integer. In any case, wsprintf will print your serial number as a wchar (unicode) string.

夏九 2024-08-30 10:54:24

您应该能够使用如下内容:

wchar_t buf[10];
swprintf(buf, L"%d", your_int_value);

根据 C 运行时库的具体实现,详细信息可能会有所不同。例如,您的 swprintf() 可能需要 buf 中的字符数:

swprintf(buf, sizeof(buf)/sizeof(buf[0]), L"%d", your_int_value);

You should be able to use something like this:

wchar_t buf[10];
swprintf(buf, L"%d", your_int_value);

The details may be different depending on your exact implementation of the C runtime library. For example, your swprintf() may expect the number of characters in buf:

swprintf(buf, sizeof(buf)/sizeof(buf[0]), L"%d", your_int_value);
仙女山的月亮 2024-08-30 10:54:24

如果序列号适合 32 位并且平台是大端并支持 Unicode、32 位整数和标准 C 库,那么正如其他答案所示,这非常简单。如果平台有 32 位整数和 8 位字符,但是是小端字节序和/或不支持 Unicode,并且如果序列号的长度可以变化,那么下面的代码可能会很有用,尽管它有点麻烦。

void extract_serial_number(unsigned char* address, unsigned int bytes, char* buffer) {
    unsigned int value = 0;
    char c, *start = buffer;
    while (bytes--) {                      /* read the serial number into an integer */
        value = value << 8;
        value |= *address++;
    }
    while (value > 0) {                    /* convert to 16 bit Unicode (reversed) */
        *buffer++ = '0' + value % 10;
        *buffer++ = '\0';
        value /= 10;
    }
    *buffer++ = '\0';
    *buffer++ = '\0';
    buffer -= 4;
    while (buffer > start) {               /* reverse the string */
        c = *buffer;
        *buffer = *start;
        *start = c;
        buffer -= 2;
        start += 2;
    }
}

If the serial number fits into 32 bits and the platform is big endian and supports Unicode, 32 bit ints and the standard C libraries then this is pretty straightforward as other answers have shown. If the platform has 32 bit ints and 8 bit chars but is little endian and/or doesn't support Unicode, and if the serial number can vary in length, then the below may be useful, though it's a little workmanlike.

void extract_serial_number(unsigned char* address, unsigned int bytes, char* buffer) {
    unsigned int value = 0;
    char c, *start = buffer;
    while (bytes--) {                      /* read the serial number into an integer */
        value = value << 8;
        value |= *address++;
    }
    while (value > 0) {                    /* convert to 16 bit Unicode (reversed) */
        *buffer++ = '0' + value % 10;
        *buffer++ = '\0';
        value /= 10;
    }
    *buffer++ = '\0';
    *buffer++ = '\0';
    buffer -= 4;
    while (buffer > start) {               /* reverse the string */
        c = *buffer;
        *buffer = *start;
        *start = c;
        buffer -= 2;
        start += 2;
    }
}
为你鎻心 2024-08-30 10:54:24

不确定此代码是否适合您的设备,但让我们尝试一下

char buffer[MAX_SIZE];
char unicodeBuffer[MAX_SIZE];
sprintf(buffer, "%d", i);
int len = strlen(buffer);
int i = 0;
for (int i = 0; i <= (len << 1) - 2; i++)
   unicodeBuffer[i] = i % 2 ? buffer[i] : 0;
unicodeBuffer[i + 1] = 0;
unicodeBuffer[i + 2] = 0;

not sure that this code will fit your device, but let's try

char buffer[MAX_SIZE];
char unicodeBuffer[MAX_SIZE];
sprintf(buffer, "%d", i);
int len = strlen(buffer);
int i = 0;
for (int i = 0; i <= (len << 1) - 2; i++)
   unicodeBuffer[i] = i % 2 ? buffer[i] : 0;
unicodeBuffer[i + 1] = 0;
unicodeBuffer[i + 2] = 0;
清风疏影 2024-08-30 10:54:24

如果您可以在嵌入式环境中访问 sprintf,那么这应该可以工作:

#define MAX_SIZE(type) ((CHAR_BIT * sizeof(type) - 1) / 3 + 2)

/* Destination UCS2 buffer size must be at least 2*MAX_SIZE(int) */
void int_to_ucs2(char *dest, int q)
{
    char buffer[MAX_SIZE(q)];
    char *src = buffer;

    sprintf(buffer, "%d", q);

    do {
        *dest++ = *src;
        *dest++ = 0;
    } while (*src++);
}

If you have access to sprintf in your embedded environment, then this should work:

#define MAX_SIZE(type) ((CHAR_BIT * sizeof(type) - 1) / 3 + 2)

/* Destination UCS2 buffer size must be at least 2*MAX_SIZE(int) */
void int_to_ucs2(char *dest, int q)
{
    char buffer[MAX_SIZE(q)];
    char *src = buffer;

    sprintf(buffer, "%d", q);

    do {
        *dest++ = *src;
        *dest++ = 0;
    } while (*src++);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文