Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How to print 2-byte Unicode to a string (http://www.go4expert.com/forums/print-2-byte-unicode-string-t16290/)

davo666 21Feb2009 11:21

How to print 2-byte Unicode to a string
 
Hey,

i need to print unicode into a string but i can only access the stream one byte at a time, how can i make a 2byte char array?

thanks\

also, say if i truncate it to one byte, what would happen, when would my program stuff up?

xpi0t0s 21Feb2009 13:58

Re: How to print 2-byte Unicode to a string
 
Reading the stream one byte at a time doesn't mean you can't make a 2 byte char array, just read the whole thing in byte by byte, and you can either populate the array a byte at a time or you could store the first byte in a temporary 2-byte variable somewhere until you get the 2nd byte, then write the contents of that variable into the array.

It's impossible to say what would happen if you truncated the array to one byte, it depends what your program does with it. It also depends on how your memory management routines work; if malloc(1) actually allocatess 1 byte or if it allocates memory in chunks, e.g. in multiples of 16 bytes. But yes I'd say there's a good chance of your program exhibiting undefined behaviour because a 2-byte char array using standard string semantics (i.e. a terminating NULL) must be at least four bytes long, for one character and its terminating NULL, and in any event must be a multiple of 2 bytes.

Also it depends what you mean by "it", because if you mean one of the entries in the array then the whole lot after that would be out of sync and the program would not read the correct values from the array. Whether or not it "stuffs up" depends on the code and is impossible to predict.


All times are GMT +5.5. The time now is 15:35.