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

G

Guest

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 = (char) line->Chars;
}

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

choongseng

Kueishiong said:
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 = (char) line->Chars;
}

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.
 
L

Luc E. Mistiaen

why not use the ToCharArray method like in

char buffer[] = line.ToCharArray() ;

/LM
choongseng said:
Kueishiong said:
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 = (char) line->Chars;
}

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.
 
J

Jon Skeet [C# MVP]

Kueishiong Tu said:
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 = (char) line->Chars;
}

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top