By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,669 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,669 IT Pros & Developers. It's quick & easy.

How do I copy the content of a string to a char array?

P: n/a
How do I copy the content of a string in one encoding (in my case big5) to a
char array of the same encoding?

I try the following

String line[] = S"123水泥";
char buffer[200];

for(int i=0; i<line->get_length(); i++)
{
buffer[i] = (char) line->Chars[i];
}

It works fine for the first 3 Ascii characters, but gets messed up for the
next 2 Chinese characters. What is wrong here?
Oct 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Kueishiong Tu wrote:
How do I copy the content of a string in one encoding (in my case big5) to a
char array of the same encoding?

I try the following

String line[] = S"123水泥";
char buffer[200];

for(int i=0; i<line->get_length(); i++)
{
buffer[i] = (char) line->Chars[i];
}

It works fine for the first 3 Ascii characters, but gets messed up for the
next 2 Chinese characters. What is wrong here?

Try to use WChar instead of Char.
Oct 17 '05 #2

P: n/a
why not use the ToCharArray method like in

char buffer[] = line.ToCharArray() ;

/LM
"choongseng" <ch********@gmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Kueishiong Tu wrote:
How do I copy the content of a string in one encoding (in my case big5)
to a char array of the same encoding?

I try the following String line[] = S"123??";
char buffer[200];

for(int i=0; i<line->get_length(); i++)
{
buffer[i] = (char) line->Chars[i];
}

It works fine for the first 3 Ascii characters, but gets messed up for
the next 2 Chinese characters. What is wrong here?

Try to use WChar instead of Char.

Oct 17 '05 #3

P: n/a
Kueishiong Tu <Ku**********@discussions.microsoft.com> wrote:
How do I copy the content of a string in one encoding (in my case big5) to a
char array of the same encoding?

I try the following

String line[] = S"123??";
char buffer[200];

for(int i=0; i<line->get_length(); i++)
{
buffer[i] = (char) line->Chars[i];
}

It works fine for the first 3 Ascii characters, but gets messed up for the
next 2 Chinese characters. What is wrong here?


Character arrays don't *have* separate encodings as such - characters
are *always* Unicode in .NET.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.