473,624 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10278
<dj***@excite.i t> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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******@mkwah ler.net> writes:
<dj***@excite.i t> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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_Keit h) 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.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Mike Wahler" <mk******@mkwah ler.net> writes:
<dj***@excite.i t> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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)(unsi gned 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
25912
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, so I assume it's simplest to convert to lower/upper first. Possibly all strings will be from the latin-1 character set, so I could convert to 8-bit latin-1, map to lowercase, and convert back, but that seems rather cumbersome.
22
5474
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 codecvt, in Josuttis' "The Standard C++ Library", did not help, and I'm still awaiting delivery of Langer's "Standard C++ IOStreams and Locales". Thanks,
0
2579
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 the applications which hang off of them. Unicode database details -> (Windows 2K, SQL Server 2K, collation = Latin1_General_BIN). Non Unicode database details -> (Windows 2K, SQL Server 2K, collation = SQL_Latin1_General_CP1_CI_AS).
6
7953
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 data are stored using collation "Chinese_PRC_CI_AS", i.e. Simplified Chinese. So I thought we need to extract these data out to the new SQL Server which is using Unicode (I assume it means converting them to nchar, nvarchar type of fields for I...
10
8044
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
18
34111
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 Encoding.Convert, but that needs byte arrays. Thanks, /Ger
9
22840
by: mistral | last post by:
Is there some Unicode converter to convert javascript to Unicode format? thanks mistral
13
2957
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 objects." i'm on Unix. (linux, ubuntu edgy)
0
1570
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 8 bit encoding data ' Prototype of C function. ' extern "C" int _stdcall Uni2Eni(wchar_t * uni, unsigned char * eni, int size)
0
8236
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8173
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8621
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8475
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.