LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-04-2005, 02:37 PM   #1
davidh_uk
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?

TIA
 
Old 02-06-2005, 01:39 AM   #2
jlliagre
Moderator
 
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):
Code:
#include <iconv.h>

main()
{
  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);
  if(it!=(iconv_t)-1)
  {
    size_t c;
    char *source=ibuf;
    char *result=obuf;
    il=strlen(ibuf);
    if(iconv(it, (const char **)&ibuf, &il, &obuf, &ol)!=-1)
    {
      printf("%s -> %s\n", source, result);
    }
    else
    {
      perror("iconv");
    }
    iconv_close(it);
  }
  else
  {
    perror("iconv_open");
  }
}
Its output:

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

Original Poster
Rep: Reputation: 0
jlliagre
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:
http://www.lrz-muenchen.de/services/...c_6.html#SEC99
This is what I'll use instead.
Thanks
 
  


Reply


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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