473,386 Members | 1,766 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,386 software developers and data experts.

how to convert narrow string to wide string and vice versa?

i'm using VC++6 IDE
i know i could use macros like A2T, T2A,
but is there any way more decent way to do this?

Sep 7 '06 #1
4 17716
thinktwice wrote:
i'm using VC++6 IDE
i know i could use macros like A2T, T2A,
but is there any way more decent way to do this?
Look up std::ctype::widen and std::ctype::narrow in the <locale>
header.

Regards,
Bart.

Sep 7 '06 #2

Bart wrote:
thinktwice wrote:
i'm using VC++6 IDE
i know i could use macros like A2T, T2A,
but is there any way more decent way to do this?

Look up std::ctype::widen and std::ctype::narrow in the <locale>
header.
These may not be much good for Unicode or other variable width
encodings - depends on how you use the resultant strings.

It's a tricky thing to deal with. If you properly understand what you
mean by 'narrow' and 'wide' strings the solution should present itself.
If you're not sure what the string content means then you're unlikely
to find the right solution in a library because you won't know how to
use the functions or their results properly.
K

Sep 7 '06 #3
Kirit Sælensminde wrote:
Bart wrote:
>thinktwice wrote:
>>i'm using VC++6 IDE
i know i could use macros like A2T, T2A,
but is there any way more decent way to do this?
Look up std::ctype::widen and std::ctype::narrow in the <locale>
header.

These may not be much good for Unicode or other variable width
encodings - depends on how you use the resultant strings.

It's a tricky thing to deal with. If you properly understand what you
mean by 'narrow' and 'wide' strings the solution should present itself.
If you're not sure what the string content means then you're unlikely
to find the right solution in a library because you won't know how to
use the functions or their results properly.
well, if you just want a quick ugly hack, then personally i've sometimes
used:

wstring wide(L"some wide character string");
string narrow(wide.begin(), wide.end());

But this is a cleaving axe for microsurgery: It depends on wide having
equivalent encoding codepoints to the charset in string, which is only
really tru if wstrings are unicode, contain only ISO-8859-1 characters
(0-255), and normal character encoding is ISO-8859-1 or similar. (char
type, depends on platform).

I would actually be interested in seeing what the "clean" solution for
converting is when you have, say, Unicode in wchar_t's and whatever
encoding the locale specifies in char's (ISO-8859-1, or maybe
windows-1252) :)

//deice [deice at deice dot cjb dot net]
//Arne Pajunen
Sep 7 '06 #4

Arne 'deice' Pajunen wrote:
Kirit Sælensminde wrote:
Bart wrote:
thinktwice wrote:
i'm using VC++6 IDE
i know i could use macros like A2T, T2A,
but is there any way more decent way to do this?
Look up std::ctype::widen and std::ctype::narrow in the <locale>
header.
These may not be much good for Unicode or other variable width
encodings - depends on how you use the resultant strings.

It's a tricky thing to deal with. If you properly understand what you
mean by 'narrow' and 'wide' strings the solution should present itself.
If you're not sure what the string content means then you're unlikely
to find the right solution in a library because you won't know how to
use the functions or their results properly.

well, if you just want a quick ugly hack, then personally i've sometimes
used:

wstring wide(L"some wide character string");
string narrow(wide.begin(), wide.end());

But this is a cleaving axe for microsurgery: It depends on wide having
equivalent encoding codepoints to the charset in string, which is only
really tru if wstrings are unicode, contain only ISO-8859-1 characters
(0-255), and normal character encoding is ISO-8859-1 or similar. (char
type, depends on platform).

I would actually be interested in seeing what the "clean" solution for
converting is when you have, say, Unicode in wchar_t's and whatever
encoding the locale specifies in char's (ISO-8859-1, or maybe
windows-1252) :)
The first step is to convert the UTF-16 (which is normal for wchar_t,
but I think there may be some platforms/compilers that use UTF-32) to
UTF-32. Then convert that down (often with a code table, but sometimes
algorithmically). Of course there's the open question of what to do
with characters that don't/can't map. In some applications you can use
a variety of character encodings (as distinct to character sets). For
example, if you're using ISO-8859-1 in XML/HTML you can use the forms
XML/HTML defines for this.

A full answer depends on what you are using the string for which is why
it's so hard to answer. For some things your solution is perfectly
valid - it's fine for the many parts of internet protocols which are
defined to use ASCII characters only.

For our framework we're looking at using ICU to do the conversions, but
haven't had much of a chance to play with it yet. As nearly 100% of the
interactions we do are through HTTP then we just use UTF-8 and that
solves nearly the whole problem. We have found it useful to define our
own std::wstring like class that uses UTF-32 as the single character
interface points (operator[] and at() etc.) but uses UTF-16 for
character sequences. Things like substr() use the correct position and
count based on the number of UTF-32 characters _not_ the number of
UTF-16 code points so applications can't chop in half some characters.
K

Sep 7 '06 #5

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

Similar topics

6
by: Byron | last post by:
Hello, I am a newbie and would like to know if it is possible to convert a string back to a dictionary? For example, I can convert a dictionary to a string by doing this: >>> names =...
3
by: Rodusa | last post by:
I am looking for an example of how to save a DataTable into Base64 and vice-versa. I tried using Convert.ToBase64String(), but it says that it cannot convert DataTable to byte. Any help or pointing...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
5
by: sarab | last post by:
Hi, I have a managed C++ app in which i need to convert String * to char *. How can i achive this?. regards -sarab
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
6
yabansu
by: yabansu | last post by:
Hi all, I think most of you probably know the two .NET framework functions, namely Encoding.GetBytes(string) and Encoding.GetString(byte), to convert string into byte array and vice versa. Now,...
1
by: Rui Maciel | last post by:
I'm trying to use the getwc() function to get individual characters from a stream but I've stumbled on a type conversion problem. The getwc() function returns a type wint_t instead of a wchar_t and...
0
by: nudrat | last post by:
Hi, My requirement are : 1) In one PC in LAN Linus is installed which is used for net surfing.... rest of the systems have xp including exchange server. 2) We want to convert all email...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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$) { } ...
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...
0
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,...
0
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...
0
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...

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.