Thread: conversion from char string e.q. "AA" to UTF-8 format

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    22

    conversion from char string e.q. "AA" to UTF-8 format

    hi frnds..

    how to convert ascii character string to utf-8 format. does c library in linux give any function for this conversion?

    Thxs in advance.

    Shibu.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    All ASCII encoded characters are UTF-8 encoded characters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Not the C Standard librairy because UTF-8 is not part of the C librairy. C only provides a special type for dealing with 16 bit chars. Those wide characters do not actually have to be Unicode however.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    IF your string is actually ASCII (that is, no values above 127) then it's already UTF-8 as Salem said.

    If your string contains characters 128-255, it's not an ASCII string, and then you need to know which character set (Latin1, Shift-JIS, etc.) that the source string is in. I am not sure if there are any linux-specific functions for doing code page conversion, but it wouldn't be too hard to code a lookup table if your input character set was always the same.
    Last edited by Cat; 09-04-2006 at 02:45 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    22
    I have a plain string passed to a gtk function. string is "acdrrt". Now as responded this should be in UTF-8 but gtk function is reporting to me it is not in UTF-8.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Could you post the code? Is there a 'L' following the string to indicate that the string is unicode? What happens if you pass the string constant directly? I bet it's how you store the string.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    22
    my code is given below

    strcpy(a,"AAA");
    glist=glist_append(glist,a);

    but if I give
    glist=glist_append(glist,"AAA")
    it works fine.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Right, so how is a defined?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    a is defined as

    char a[15];

    strcpy(a,"AAA");
    glist=glist_append(glist,a);

    here it reports invalid UTF-8 string passed to function
    Last edited by shibu; 09-07-2006 at 12:42 AM.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Odd . . . are you having the same problem? What language do you speak? Do you think that you're in a different locale?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  3. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  4. String -> unsigned char array conversion....
    By SublicK in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2001, 12:18 PM