How to print 2-byte Unicode to a string

Discussion in 'C' started by davo666, Feb 21, 2009.

  1. davo666

    davo666 New Member

    Feb 21, 2009
    Likes Received:
    Trophy Points:

    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?


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

    xpi0t0s Mentor

    Aug 6, 2004
    Likes Received:
    Trophy Points:
    Senior Support Engineer
    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.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice