473,378 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Unicode conversion

How can I convert char in wchar_t?
And how can I convert wchar_t in char?

Thanks to anyone

Nov 15 '05 #1
4 10237
<dj***@excite.it> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
How can I convert char in wchar_t?
wchar_t w = 0;
char c = 'A';

w = c;

However, this has nothing to do with Unicode
(which is not topical here anyway).
And how can I convert wchar_t in char?


c = (char)w;

... however, data may be lost, as type 'char'
might not have the same range as 'wchar_t'.

IMO you'll need to think more about exactly what
problem you're trying to solve, and forumlate
a much more specific question(s). Then if it's
about the C *language*, ask here. If it's not,
find a newsgroup where it's topical.

Note that Unicode questions are not C questions.

-Mike
Nov 15 '05 #2
"Mike Wahler" <mk******@mkwahler.net> writes:
<dj***@excite.it> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
How can I convert char in wchar_t?


wchar_t w = 0;
char c = 'A';

w = c;

However, this has nothing to do with Unicode
(which is not topical here anyway).
And how can I convert wchar_t in char?


c = (char)w;

.. however, data may be lost, as type 'char'
might not have the same range as 'wchar_t'.

[...]

The cast is superfluous. char and wchar_t are both integer types, so
an assignment includes an implicit conversion.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #3

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Mike Wahler" <mk******@mkwahler.net> writes:
<dj***@excite.it> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
How can I convert char in wchar_t?


wchar_t w = 0;
char c = 'A';

w = c;

However, this has nothing to do with Unicode
(which is not topical here anyway).
And how can I convert wchar_t in char?


c = (char)w;

.. however, data may be lost, as type 'char'
might not have the same range as 'wchar_t'.

[...]

The cast is superfluous. char and wchar_t are both integer types, so
an assignment includes an implicit conversion.


That was an attempt to pre-empt a possible compiler warning. :-)

-Mike
Nov 15 '05 #4
dj***@excite.it wrote:
How can I convert char in wchar_t?
And how can I convert wchar_t in char?

Thanks to anyone


Basically, set the LC_CTYPE of the locale to tell the implementation
what encoding your char string is in, then use wcstombs and mbstowcs
functions to perform the conversion.

The following code demonstrates their use.

(Can an expert here please check my code? It seems to work fine, but
there may be off-by-one errors in the memory allocation and/or error
checking.)

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>

char * /// convert wchar_t string to char in given encoding
wchar_t_string_to_char(const wchar_t *src,
const char *encoding)
{
if(setlocale(LC_CTYPE, encoding) == NULL)
{
fprintf(stderr, "requested encoding unavailable\n");
return NULL;
}
size_t n = wcstombs(NULL, src, 0);
char *dst = malloc(n + 1);
if(dst == NULL)
{
fprintf(stderr, "memory allocation failed\n");
return NULL;
}
if(wcstombs(dst, src, n + 1) != n)
{
fprintf(stderr, "conversion failed\n");
free(dst);
return NULL;
}
return dst;
}

wchar_t * /// convert char string in given encoding to wchar_t
char_string_to_wchar_t(const char *src,
const char *encoding)
{
if(setlocale(LC_CTYPE, encoding) == NULL)
{
fprintf(stderr, "requested encoding unavailable\n");
return NULL;
}
size_t n = mbstowcs(NULL, src, 0);
wchar_t *dst = malloc((n + 1) * sizeof *dst);
if(!dst)
{
fprintf(stderr, "memory allocation failed\n");
return NULL;
}
if(mbstowcs(dst, src, n + 1) != n)
{
fprintf(stderr, "conversion failed\n");
free(dst);
return NULL;
}
return dst;
}

int /// test the above functions
main(void)
{
char utf8[] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD, 0};
wchar_t unicode[] = {0x4F60, 0x597D, 0};

const char *encoding = "en_US.UTF-8"; // or try en_US.ISO-8859-1 etc.

char *converted1 = wchar_t_string_to_char(unicode, encoding);
wchar_t *converted2 = char_string_to_wchar_t(utf8, encoding);

if(converted1)
{
printf("Unicode converted to UTF8: ");
for(char *p = converted1; *p; p++)
printf("%X ", (unsigned)(unsigned char)*p);
free(converted1);
}

if(converted2)
{
printf("\nUTF8 converted to Unicode: ");
for(wchar_t *p = converted2; *p; p++)
printf("%X ", (unsigned)*p);
free(converted2);
}

putchar('\n');
return 0;
}
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Hallvard B Furuseth | last post by:
Has someone got a Python routine or module which converts Unicode strings to lowercase (or uppercase)? What I actually need to do is to compare a number of strings in a case-insensitive manner,...
22
by: Keith MacDonald | last post by:
Hello, Is there a portable (at least for VC.Net and g++) method to convert text between wchar_t and char, using the standard library? I may have missed something obvious, but the section on...
0
by: Jonathan | last post by:
I have a unicode database and I basically wish to publish out certain data (via views) from it to a non unicode database. Unfortunately we can not change the type of either of the databases due to...
6
by: New MSSQL DBA | last post by:
Hi all, we are now planning to upgrade our application from a non-unicode version to a unicode version. The application's backend is a SQL Server 2000 SP3. The concern is, existing business...
10
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
9
by: mistral | last post by:
Is there some Unicode converter to convert javascript to Unicode format? thanks mistral
13
by: gabor | last post by:
hi, from the documentation (http://docs.python.org/lib/os-file-dir.html) for os.listdir: "On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode...
0
by: santhescript01 | last post by:
Unicode to non unicode conversion problem -------------------------------------------------------------------------------- Hi All, I am using C dll in macro which converts Unicode data to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.