sprintf for utf8 formated characters

Discussion in 'C++' started by Mandragon03@gmail.com, Apr 14, 2008.

  1. Guest

    Anyone know if the standard sprintf supports utf8 characters that
    extend beyond the normal ascii characters?

    Thanks!
     
    , Apr 14, 2008
    #1
    1. Advertising

  2. Guest

    On Apr 14, 2:39 pm, Michal Nazarewicz <> wrote:
    > writes:
    > > Anyone know if the standard sprintf supports utf8 characters that
    > > extend beyond the normal ascii characters?

    >
    > That depends on what you mean by "support". If you do thing like:
    >
    > sprintf(buf, "%s", some_string);
    >
    > (but never do that unless you are sure buf has enough space) or
    > something along the lines of:
    >
    > sprintf(buf, format, arg1, arg2 /* ... */);
    >
    > (of course be sure format is valid and buf has enough space) and all
    > strings are UTF-8 encoded you'll get UTF-8 encoded string in the end.
    > This is guaranteed because UTF-8 is designed in such a way that NUL
    > bytes never occur in sequences encoding other characters.
    >
    > --
    > Best regards, _ _
    > .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
    > ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
    > ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--


    Thanks for the reply.

    What is still left unanswered is whether I can put utf-8 strings (ie
    they have characters that take up to 4 bytes of space) and sprint f
    that into a string without screwing up the byts of data. So something
    like this:

    unsigned int myVar= 0xDB0;

    convertMyVarToUTF8(myVar);

    char buff[512];

    sprintf( buff, "Long string with %u", myVar);

    is there a legitimate UTF-8 string in buff at this point?

    Thanks!

    Mandragon
     
    , Apr 15, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    On 15 avr, 23:02, wrote:
    > On Apr 14, 2:39 pm, Michal Nazarewicz <> wrote:


    [...]
    > What is still left unanswered is whether I can put utf-8 strings (ie
    > they have characters that take up to 4 bytes of space) and sprint f
    > that into a string without screwing up the byts of data. So something
    > like this:


    > unsigned int myVar= 0xDB0;


    > convertMyVarToUTF8(myVar);


    > char buff[512];


    > sprintf( buff, "Long string with %u", myVar);


    > is there a legitimate UTF-8 string in buff at this point?


    If the native encoding of narrow character strings is ASCII, or
    an encoding which uses ASCII for its lower 128 code points, yes.
    Because "%u" will only generated characters in the range
    [0-9a-f], and all of those characters have the same encoding in
    ASCII and in UTF-8.

    However, I suspect that the function convertMyVarToUTF8 is
    supposed to do something. But I don't see what, and I don't see
    what it could do which would affect the results here.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 15, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andee Weir
    Replies:
    10
    Views:
    57,075
    AltaGid
    Aug 13, 2007
  2. stefoid
    Replies:
    14
    Views:
    813
    ddimitrov
    Jul 6, 2006
  3. Replies:
    3
    Views:
    362
    Keith Thompson
    Mar 17, 2006
  4. walterbyrd
    Replies:
    13
    Views:
    1,301
    walterbyrd
    May 13, 2009
  5. gry
    Replies:
    2
    Views:
    737
    Alf P. Steinbach
    Mar 13, 2012
Loading...

Share This Page