在 C 语言中如何将整数转换为 Unicode 字符串?
我正在为嵌入式 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来您想要:(
假设您的序列号适合 32 位整数。无论如何,
wsprintf
都会将您的序列号打印为 wchar (unicode) 字符串。It seems like you want:
(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.您应该能够使用如下内容:
根据 C 运行时库的具体实现,详细信息可能会有所不同。例如,您的
swprintf()
可能需要buf
中的字符数:You should be able to use something like this:
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 inbuf
:如果序列号适合 32 位并且平台是大端并支持 Unicode、32 位整数和标准 C 库,那么正如其他答案所示,这非常简单。如果平台有 32 位整数和 8 位字符,但是是小端字节序和/或不支持 Unicode,并且如果序列号的长度可以变化,那么下面的代码可能会很有用,尽管它有点麻烦。
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.
不确定此代码是否适合您的设备,但让我们尝试一下
not sure that this code will fit your device, but let's try
如果您可以在嵌入式环境中访问 sprintf,那么这应该可以工作:
If you have access to
sprintf
in your embedded environment, then this should work: