Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an integer into a Unicode string in C?

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?

like image 623
PICyourBrain Avatar asked Mar 04 '10 19:03

PICyourBrain


People also ask

What is the Unicode code for C?

Unicode Character “C” (U+0043)

Does C support Unicode?

It can represent all 1,114,112 Unicode characters. Most C code that deals with strings on a byte-by-byte basis still works, since UTF-8 is fully compatible with 7-bit ASCII.

Does C use ASCII or Unicode?

As far as I know, the standard C's char data type is ASCII, 1 byte (8 bits). It should mean, that it can hold only ASCII characters.

What is an Unicode string?

Unicode is a standard encoding system that is used to represent characters from almost all languages. Every Unicode character is encoded using a unique integer code point between 0 and 0x10FFFF . A Unicode string is a sequence of zero or more code points.


2 Answers

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.

like image 85
JSBձոգչ Avatar answered Oct 05 '22 23:10

JSBձոգչ


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);
like image 40
Greg Hewgill Avatar answered Oct 05 '22 22:10

Greg Hewgill