473,545 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string conversion

LuB
I have to convert a wide (16bit) char to a standard (8bit) char.

Please nevermind that I am using WideCharToMulti Byte ... but my
question is as follows:

Does this approach make any sense? My main thought is I hate allocating
a new char* buf ... and then simply assigning it again to the
std:;string out parameter. I'd prefer to REPLACE the underlying char*
in the std::string or somehow explicitly reallocate the std::string's
internal length and somehow pass it as a parameter to the conversion
function.

But I'm just not sure how to do that. Obviously, out.c_str() is const.
Maybe I'm asking about "an idiom" to write chars to a std::string using
c-style functions without reallocating and copying alot of memory
around. I hope I'm missing something obvious. Please take it easy on
me. :-)
void Convert(const std::wstring& in, const std::string& out)
{
// determine how large to make the new buffer
int bufSize = in->size() + 1;

// create the new buffer
char* buf = new char[bufSize];

// write to the new buffer
::WideCharToMul tiByte(CP_ACP, 0, (*this), -1, buf,
bufSize, NULL, NULL);

// assignment
out = buf;

// cleanup
delete buf;
}
Thanks in advance,

-Luther

Jul 6 '06 #1
7 2914
LuB

LuB wrote:
I have to convert a wide (16bit) char to a standard (8bit) char.

Please nevermind that I am using WideCharToMulti Byte ... but my
question is as follows:

Does this approach make any sense? My main thought is I hate allocating
a new char* buf ... and then simply assigning it again to the
std:;string out parameter. I'd prefer to REPLACE the underlying char*
in the std::string or somehow explicitly reallocate the std::string's
internal length and somehow pass it as a parameter to the conversion
function.

But I'm just not sure how to do that. Obviously, out.c_str() is const.
Maybe I'm asking about "an idiom" to write chars to a std::string using
c-style functions without reallocating and copying alot of memory
around. I hope I'm missing something obvious. Please take it easy on
me. :-)
void Convert(const std::wstring& in, const std::string& out)
{
// determine how large to make the new buffer
int bufSize = in->size() + 1;

// create the new buffer
char* buf = new char[bufSize];

// write to the new buffer
::WideCharToMul tiByte(CP_ACP, 0, (*this), -1, buf,
bufSize, NULL, NULL);

// assignment
out = buf;

// cleanup
delete buf;
}
Thanks in advance,

-Luther
Sorry ... too much refactoring. Here is a better function. Please
ignore the casts.
inline void ToMultiByteStan dard(const std::wstring& in,
std::string& out)
{
// determine how large to make the new buffer
int bufSize = (int)(LONG_PTR) (size_t)in.size () + 1;

// create the new buffer
char* buf = new char[bufSize];

// write to the new buffer
::WideCharToMul tiByte(CP_ACP, 0, in.c_str(), -1, buf,
bufSize, NULL, NULL);

// assignment
out = buf;

// cleanup
delete buf;
}

Jul 6 '06 #2
LuB wrote:
LuB wrote:
>I have to convert a wide (16bit) char to a standard (8bit) char.

Please nevermind that I am using WideCharToMulti Byte ... but my
question is as follows:

Does this approach make any sense? My main thought is I hate allocating
a new char* buf ... and then simply assigning it again to the
std:;string out parameter. I'd prefer to REPLACE the underlying char*
in the std::string or somehow explicitly reallocate the std::string's
internal length and somehow pass it as a parameter to the conversion
function.

But I'm just not sure how to do that. Obviously, out.c_str() is const.
Maybe I'm asking about "an idiom" to write chars to a std::string using
c-style functions without reallocating and copying alot of memory
around. I hope I'm missing something obvious. Please take it easy on
me. :-)
There's no such "idiom", there's no way to change the underlying buffer
(no public methods for doing that) in std::string, there cannot be,
since std::string isn't even required (AFAIK) to store its characters
in any particular way (i.e., it could store them in pieces rather than a
single contiguous buffer), how it is done differs between compiler/std
lib implementations .
>>

void Convert(const std::wstring& in, const std::string& out)
{
// determine how large to make the new buffer
int bufSize = in->size() + 1;

// create the new buffer
char* buf = new char[bufSize];

// write to the new buffer
::WideCharToMul tiByte(CP_ACP, 0, (*this), -1, buf,
bufSize, NULL, NULL);
BTW I don't know what exactly WideCharToMulti Byte do, but judging by its
name it converts wchar_t sequences to multi-byte-encoded byte sequences.
Your calculation for bufSize may be wrong, because each wide character
may be represented by more than 1 byte. But you'll have to read Windows
documentation or ask in a relevant newgroup, because platform-specific
stuff is off-topic here.
D.
Jul 6 '06 #3
LuB

Davlet Panech wrote:
LuB wrote:
LuB wrote:
I have to convert a wide (16bit) char to a standard (8bit) char.

Please nevermind that I am using WideCharToMulti Byte ... but my
question is as follows:

Does this approach make any sense? My main thought is I hate allocating
a new char* buf ... and then simply assigning it again to the
std:;string out parameter. I'd prefer to REPLACE the underlying char*
in the std::string or somehow explicitly reallocate the std::string's
internal length and somehow pass it as a parameter to the conversion
function.

But I'm just not sure how to do that. Obviously, out.c_str() is const.
Maybe I'm asking about "an idiom" to write chars to a std::string using
c-style functions without reallocating and copying alot of memory
around. I hope I'm missing something obvious. Please take it easy on
me. :-)

There's no such "idiom", there's no way to change the underlying buffer
(no public methods for doing that) in std::string, there cannot be,
since std::string isn't even required (AFAIK) to store its characters
in any particular way (i.e., it could store them in pieces rather than a
single contiguous buffer), how it is done differs between compiler/std
lib implementations .
>

void Convert(const std::wstring& in, const std::string& out)
{
// determine how large to make the new buffer
int bufSize = in->size() + 1;

// create the new buffer
char* buf = new char[bufSize];

// write to the new buffer
::WideCharToMul tiByte(CP_ACP, 0, (*this), -1, buf,
bufSize, NULL, NULL);

BTW I don't know what exactly WideCharToMulti Byte do, but judging by its
name it converts wchar_t sequences to multi-byte-encoded byte sequences.
Your calculation for bufSize may be wrong, because each wide character
may be represented by more than 1 byte. But you'll have to read Windows
documentation or ask in a relevant newgroup, because platform-specific
stuff is off-topic here.
D.
Thanks Davlet,

-LuB

Jul 6 '06 #4
LuB posted:
I have to convert a wide (16bit) char to a standard (8bit) char.

Maybe something like:
#include <cstddef>
#include <cstring>

template<class DestT>
DestT * const ConvertString( char const *p_source, DestT (* const
Convertor)(char ) )
{
using std::size_t;

size_t const len = std::strlen(p_s ource);

DestT * const p_buffer = new DestT[len + 1];

DestT *p_dest = p_buffer;

while ( *p_dest++ = Convertor(*p_so urce++) );

return p_buffer;
}

wchar_t Convertor(char const c)
{
return c;
}

int main()
{
wchar_t * const p = ConvertString(" I like ice-cream.",
Convertor);

delete [] p;
}


--

Frederick Gotham
Jul 6 '06 #5
LuB

Frederick Gotham wrote:
LuB posted:
I have to convert a wide (16bit) char to a standard (8bit) char.


Maybe something like:
#include <cstddef>
#include <cstring>

template<class DestT>
DestT * const ConvertString( char const *p_source, DestT (* const
Convertor)(char ) )
{
using std::size_t;

size_t const len = std::strlen(p_s ource);

DestT * const p_buffer = new DestT[len + 1];

DestT *p_dest = p_buffer;

while ( *p_dest++ = Convertor(*p_so urce++) );

return p_buffer;
}

wchar_t Convertor(char const c)
{
return c;
}

int main()
{
wchar_t * const p = ConvertString(" I like ice-cream.",
Convertor);

delete [] p;
}
I thought about doing something like this. Its nice to see someone else
with a similar idea. I will have to profile both approaches and see
what kind of performance gain this approach has.

I'd hoped there was no black magic to something like converting chars
to wchars ... but Bruce Sutter and Scott Meyers always manage to
convince me otherwise ;-) I didn't know if UNICODE, UTF-8 and/or ANSI
were or should be involved in the thought process regarding the type of
approach you exemplified here. I hope it is just as easy as copying
over the integer value of each character.
Frederick Gotham
Many thanks,

-Luther

Jul 6 '06 #6

"LuB" <lu*********@ya hoo.comwrote in message
news:11******** *************@m 79g2000cwm.goog legroups.com...
>
Frederick Gotham wrote:
>LuB posted:
I have to convert a wide (16bit) char to a standard (8bit) char.


Maybe something like:
#include <cstddef>
#include <cstring>

template<class DestT>
DestT * const ConvertString( char const *p_source, DestT (* const
Convertor)(cha r) )
{
using std::size_t;

size_t const len = std::strlen(p_s ource);

DestT * const p_buffer = new DestT[len + 1];

DestT *p_dest = p_buffer;

while ( *p_dest++ = Convertor(*p_so urce++) );

return p_buffer;
}

wchar_t Convertor(char const c)
{
return c;
}

int main()
{
wchar_t * const p = ConvertString(" I like ice-cream.",
Convertor);

delete [] p;
}

I thought about doing something like this. Its nice to see someone else
with a similar idea. I will have to profile both approaches and see
what kind of performance gain this approach has.

I'd hoped there was no black magic to something like converting chars
to wchars ... but Bruce Sutter and Scott Meyers always manage to
convince me otherwise ;-) I didn't know if UNICODE, UTF-8 and/or ANSI
were or should be involved in the thought process regarding the type of
approach you exemplified here. I hope it is just as easy as copying
over the integer value of each character.
Something like this might work for wchar_t to char (which the OP asked
about)
as long as the wide chars are defined Unicode characters with values less
than
x100. However it can not work for char to wchar_t for characters with values
>
0x7F && < 0xA0, since I believe this range is undefined in Unicode.

(At least assuming the conversion from char to wchar_t doesn't do the
Unicode
mapping.)
Jul 6 '06 #7
In article <HU************ *******@news.in digo.ie>, fg*******@SPAM. com
says...

[ ... ]

Semi-chaing the subject -- just a couple comments on the code itself.
#include <cstddef>
#include <cstring>

template<class DestT>
DestT * const ConvertString( char const *p_source, DestT (* const
Convertor)(char ) )
Unless there was a _really_ good reason to do otherwise, I'd use a
container of some sort instead of returning a raw pointer. If you
have some reason not to use a wstring, then you could surely use a
std::vector<Des tTinstead.
{
using std::size_t;

size_t const len = std::strlen(p_s ource);

DestT * const p_buffer = new DestT[len + 1];

DestT *p_dest = p_buffer;

while ( *p_dest++ = Convertor(*p_so urce++) );

return p_buffer;
First I'd note that this function appears to be pretty much
equivalent to one of the overloads of ctype::widen, so there may not
be any reason to write it at all. Going from memory, the only obvious
difference is that ctype::widen expects you to pre-allocate the
destination memory.

If you weren't going to do that, I'd still consider something like
this:

size_t len = strlen(p_source );
std::vector<Des tTp_buffer(len) ;
std::transform( p_source, p_source+len, Convertor);
return p_buffer;
>
wchar_t Convertor(char const c)
{
return c;
}
If you decide to retain the ConvertString function above, you'd
probably still want to use the single-character overload of
ctype::widen to do the conversion itself. At least in theory it
should know how to handle things like converting a character with its
high-bit set to the appropriate value in the target type.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 9 '06 #8

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

Similar topics

10
3598
by: Marcin Kalicinski | last post by:
Why string literals are regarded as char * not as const char *? (1) void f(char *); (2) void f(const char *); f("foo") will call version (1) of function f. I understand that the exact type of literal "foo" is char, which by means of standard conversion becomes char *. Still, there's something going wrong here, because this allows...
2
3080
by: Thomas Matthews | last post by:
Hi, I'm working with Borland C++ Builder 6.2. My project uses the std::string class. However, Borland in its infinite wisdom has its own string class, AnsiString. To make my life easier, I want to create a function, preferably a conversion operator, to convert from a std::string to an AnsiString. Please note that I don't want to...
12
2185
by: ABeck | last post by:
Hello List, I have ar more or less academical question. Can there arise runtime errors in a program, if the include of <string.h> has been forgotten? If all the arguments to the functions of <string.h> are correct, is it possible that an error occurs, and what an error might this be? Regards
6
13348
by: Marco Herrn | last post by:
Hi, I need to serialize an object into a string representation to store it into a database. So the SOAPFormatter seems to be the right formatter for this purpose. Now I have the problem that this formatter writes into a stream. And I am not used enough to C# to convert this to a string. I tried the following code: MemoryStream stream=...
6
2197
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
4
5523
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm converting the binary blocks to strings first, then converting the resulting string to base64. This seems highly inefficient and I'd like to just go...
10
13617
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem like c# is breaking its own rules? This works: int b = 32; string a = "ABC " + b; Result: a = "ABC 32"
5
5950
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For compression string ->(Unicode Conversion) byte -(Compression + Unicode Conversion) string
3
9515
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++ code (ie. wrappers etc). In Managed C++ there was an automatic conversion between const char* and String^. This was useful to us for two reasons: 1....
10
9066
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is Unicode, UCS-2LE(BMP) in Win32, and UCS4 in Linux?). I have 2 ways to do the job:
0
7499
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...
0
7943
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...
1
7456
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...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
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...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
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...

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.