Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-04-2005, 02:37 PM   #1
LQ Newbie
Registered: Jan 2005
Location: London, england
Distribution: Debian
Posts: 9

Rep: Reputation: 0
in linux & c/c++: how do I convert an ascii string to utf8 & vice versa?

I'm using the libxml gnome libraries to try and convert ascii strings to utf8 and vice versa but I cant get it to work. Here's the test code;

#include <assert.h>
#include <getopt.h>
#include <glib-2.0/glib.h> //for all that cool stuff..
#include <exception>
#include <iostream>
#include <getopt.h>

using namespace std;

wstring makewidestring(string narrowstr)
int n;
wstring _widestr;
n = strlen(narrowstr.c_str()) * sizeof(wchar_t);
; //make string wide
if (mbstowcs((wchar_t *)_widestr.c_str(), narrowstr.c_str(), n)<=0)
throw "Regex::Regex failed to make match wide target string ";
wcout <<"stub1"<<_widestr <<":"<<endl;
return wstring(_widestr);
int main(int argc, char **argv)
string nstr;
wstring wstr;

nstr = "narrow string";
wstr = makewidestring(nstr);
cout <<"narrow string:'" << nstr <<"'<-" << endl;
wcout <<"wide string:'" << wstr <<"'<-" << endl;

Can anyone hit me with the clue stick? is mbstowcs the function to use? hows it done?

Old 02-06-2005, 01:39 AM   #2
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,761

Rep: Reputation: 459Reputation: 459Reputation: 459Reputation: 459Reputation: 459
You'd better off with iconv to convert from character sets.

Here is some sample code, (edited while under ISO-8859-1 encoding):
#include <iconv.h>

  iconv_t it;
  size_t il=16;
  char *ibuf=calloc(il, 1);
  size_t ol=64;
  char *obuf=malloc(64);
  char *toCode="UTF8";
  char *fromCode="ISO-8859-1";

  strncpy(ibuf, " va o ?", il);
  it=iconv_open(toCode, fromCode);
    size_t c;
    char *source=ibuf;
    char *result=obuf;
    if(iconv(it, (const char **)&ibuf, &il, &obuf, &ol)!=-1)
      printf("%s -> %s\n", source, result);
Its output:

 va o ? -> ç  va où ?
Old 02-06-2005, 05:55 PM   #3
LQ Newbie
Registered: Jan 2005
Location: London, england
Distribution: Debian
Posts: 9

Original Poster
Rep: Reputation: 0
That was a real eye opener - I hadn't appreciated how to use iconv, and its advantages over mbswhatever. Based on your post I Googled & found this documentation:
This is what I'll use instead.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
convert Traditional to Simplified Chinese & vice versa tcma Linux - Software 4 02-06-2014 09:21 PM
how to convert pdf to pdb and vice versa dr_zayus69 Linux - Software 1 07-17-2005 05:40 PM
copy&paste mozilla -> openoffice and vice versa mho Linux - Software 1 09-27-2003 03:51 PM
Convert Celius to fahreheit and vice versa bluewolf Linux - Newbie 2 02-07-2003 10:49 AM
Share Windows HD with MD & vice-versa lyllo Linux - Hardware 2 02-02-2003 06:03 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:29 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration