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

How to convert a Byte array to a char array?

P: n/a
How do I convert a Byte array (unsigned char managed) to a
char array(unmanaged) with wide character taken into
account?
Nov 16 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Byte barray[] = new Byte[512];
__wchar_t* arr = new __wchar_t[barray->Length];
for(int i=0; i < barray->Length; i++)
arr[i] = barray[i];
delete arr;

--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.net.tw> wrote in message
news:2d****************************@phx.gbl...
How do I convert a Byte array (unsigned char managed) to a
char array(unmanaged) with wide character taken into
account?

Nov 16 '05 #2

P: n/a
-----Original Message-----
Byte barray[] = new Byte[512];
__wchar_t* arr = new __wchar_t[barray->Length];
for(int i=0; i < barray->Length; i++)
arr[i] = barray[i];
delete arr;


How do I further convert to a array declared as char
carray[]? The access of a char array through its pointer
has taken the wide character into account. i.e., if

char *cpp;
int n;
cpp = carray;

(cpp+n) will always point to a valid wide charater (n any
int).

Nov 16 '05 #3

P: n/a
Byte barray[] = new Byte[512];

//char here is 16 bits
char smallarray[512];
for(int i=0; i<barray->Length; i++)
smallarray[i] = barray[i];

//Char here is 32 bits
Char carray[] = Encoding::ASCII->GetChars(barray);
--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.net.tw> wrote in message
news:2f****************************@phx.gbl...
-----Original Message-----
Byte barray[] = new Byte[512];
__wchar_t* arr = new __wchar_t[barray->Length];
for(int i=0; i < barray->Length; i++)
arr[i] = barray[i];
delete arr;


How do I further convert to a array declared as char
carray[]? The access of a char array through its pointer
has taken the wide character into account. i.e., if

char *cpp;
int n;
cpp = carray;

(cpp+n) will always point to a valid wide charater (n any
int).


Nov 16 '05 #4

P: n/a
-----Original Message-----
Byte barray[] = new Byte[512];

//char here is 16 bits
char smallarray[512];
for(int i=0; i<barray->Length; i++)
smallarray[i] = barray[i];

//Char here is 32 bits
Char carray[] = Encoding::ASCII->GetChars(barray);


The byte array contains a mixture of
raw data of Ascii (8-bit) and Chinese characters (16-bit).
What I need is to convert the byte array into a char array
so that if I assigned a char pointer to the beginning of
the char array and I increment the char pointer, it will
point to a valid wide character which may be 8-bit or 16-
bit.

Byte barray[512];
char carray[512];
char *cpp;
//convert barray to caaray
cpp = carray;
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);

and so on ....


Nov 16 '05 #5

P: n/a
Kueishiong Tu wrote:
How do I convert a Byte array (unsigned char managed) to a
char array(unmanaged) with wide character taken into
account?


See: StringToHGlobalAnsi (System::Runtime::InteropServices::Marshal)
http://msdn.microsoft.com/library/en-
us/cpref/html/frlrfsystemruntimeinteropservicesmarshalclassstrin gtohglobala
nsitopic.asp

But conversion to ANSI is bad... because String is UNICODE... so if it
contains some unicode characters this might be lost.

Better convert to unicde: StringToHGlobalUni
or use PtrToStringChars (which is faster; if you do not need to modify the
string)

See also:
HOW TO: Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/?kbid=311259

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
Nov 16 '05 #6

P: n/a
There was en error in my prev post.

char is 8 bits (a byte) and Char is 16 bits (2 bytes)

--
Regards,
Nish [VC++ MVP]

"Nishant S" <ni**@nospam.asianetindia.com> wrote in message
news:up**************@tk2msftngp13.phx.gbl...
Byte barray[] = new Byte[512];

//char here is 16 bits
char smallarray[512];
for(int i=0; i<barray->Length; i++)
smallarray[i] = barray[i];

//Char here is 32 bits
Char carray[] = Encoding::ASCII->GetChars(barray);
--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.net.tw> wrote in message
news:2f****************************@phx.gbl...
-----Original Message-----
Byte barray[] = new Byte[512];
__wchar_t* arr = new __wchar_t[barray->Length];
for(int i=0; i < barray->Length; i++)
arr[i] = barray[i];
delete arr;


How do I further convert to a array declared as char
carray[]? The access of a char array through its pointer
has taken the wide character into account. i.e., if

char *cpp;
int n;
cpp = carray;

(cpp+n) will always point to a valid wide charater (n any
int).


Nov 16 '05 #7

P: n/a
If you have Chinese characters then you cannot use a char array because a
char is only 8 bits. You need to use a Char (System::Char) or a __whcar_t
then.

BTW considering that a Byte is 8 bits I wonder how you can store chinese
characters in a Byte array unless you have a random kinda layout where
depending on whether the character you want to store is unicode or not, you
allot either one byte or two bytes for storing a character. Not a very
organized approach in my opinion.

--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.net.tw> wrote in message
news:3b****************************@phx.gbl...
-----Original Message-----
Byte barray[] = new Byte[512];

//char here is 16 bits
char smallarray[512];
for(int i=0; i<barray->Length; i++)
smallarray[i] = barray[i];

//Char here is 32 bits
Char carray[] = Encoding::ASCII->GetChars(barray);


The byte array contains a mixture of
raw data of Ascii (8-bit) and Chinese characters (16-bit).
What I need is to convert the byte array into a char array
so that if I assigned a char pointer to the beginning of
the char array and I increment the char pointer, it will
point to a valid wide character which may be 8-bit or 16-
bit.

Byte barray[512];
char carray[512];
char *cpp;
//convert barray to caaray
cpp = carray;
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);
printf("%c\n", *cpp); // valid 8-bit or 16-bit character
*(cpp++);

and so on ....

Nov 16 '05 #8

P: n/a
-----Original Message-----
If you have Chinese characters then you cannot use a char array because achar is only 8 bits. You need to use a Char (System::Char) or a __whcar_tthen.

BTW considering that a Byte is 8 bits I wonder how you can store chinesecharacters in a Byte array unless you have a random kinda layout wheredepending on whether the character you want to store is unicode or not, youallot either one byte or two bytes for storing a character. Not a veryorganized approach in my opinion.

--
Regards,
Nish [VC++ MVP]


The Byte array I got is from a Webclient::UploadData call
which put the data returned in a Byte array (which may
contain both one byte Ascii and two-byte Chinese Big-5
code). VC++ can handle char array which may store both
one-byte ascii and two=byte Big-5 characters. I have
several functions which work fine with this situation.
The problem is how to convert the Byte array to a char
array so I have use a char porinter to decode the message
one character at a time.
Nov 16 '05 #9

P: n/a
Hmmm

Okay I think one of the following is what you want :-

//this is to obtain a single byte char array
//might lose chinese characters

char smallarray[512];
for(int i=0; i<barray->Length; i++)
smallarray[i] = barray[i];

//this gets a wide char array
String* tmp = new String(Encoding::ASCII->GetChars(barray));
__wchar_t* array = (__wchar_t*)Marshal::StringToHGlobalUni(
tmp).ToPointer();
//...
// You can now use array (wide char array)
//...
Marshal::FreeCoTaskMem(array);

I think the second one is what you want. Please try it out and see if it
works for you.

--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.net.tw> wrote in message
news:30****************************@phx.gbl...
-----Original Message-----
If you have Chinese characters then you cannot use a

char array because a
char is only 8 bits. You need to use a Char

(System::Char) or a __whcar_t
then.

BTW considering that a Byte is 8 bits I wonder how you

can store chinese
characters in a Byte array unless you have a random

kinda layout where
depending on whether the character you want to store is

unicode or not, you
allot either one byte or two bytes for storing a

character. Not a very
organized approach in my opinion.

--
Regards,
Nish [VC++ MVP]


The Byte array I got is from a Webclient::UploadData call
which put the data returned in a Byte array (which may
contain both one byte Ascii and two-byte Chinese Big-5
code). VC++ can handle char array which may store both
one-byte ascii and two=byte Big-5 characters. I have
several functions which work fine with this situation.
The problem is how to convert the Byte array to a char
array so I have use a char porinter to decode the message
one character at a time.


Nov 16 '05 #10

P: n/a
System::Char can store both unicode as well as single byte characters.

--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.net.tw> wrote in message
news:3c****************************@phx.gbl...
-----Original Message-----
There was en error in my prev post.

char is 8 bits (a byte) and Char is 16 bits (2 bytes)


Does the 2-byte Char have a fixed length or it can adjust
to the data depending on what are stored (may be one-byte
or two-byte)?
By the way, do you know a convert routine that can
convert a Byte array to a Big-5 char array (a mixture of
one-byte and two-byte characers)?

regards,
Kueishiong Tu

Nov 16 '05 #11

P: n/a
-----Original Message-----
Hmmm

Okay I think one of the following is what you want :-

//this is to obtain a single byte char array
//might lose chinese characters

char smallarray[512];
for(int i=0; i<barray->Length; i++)
smallarray[i] = barray[i];

//this gets a wide char array
String* tmp = new String(Encoding::ASCII->GetChars (barray));__wchar_t* array = (__wchar_t*) Marshal::StringToHGlobalUni( tmp).ToPointer();
//...
// You can now use array (wide char array)
//...
Marshal::FreeCoTaskMem(array);

I think the second one is what you want. Please try it out and see if itworks for you.

--
Regards,
Nish [VC++ MVP]


The problem I have is that the decoding routine (already
available) expects the input to be a char array instead
of a wide char array. How do I get around this?
Does VC++ .net has the concept of locality so that you
have to set up the locality properly before it will
recognize the local language (something like sun solaris)?
I think this is done implicitly in Microsoft products.

Kueishiong Tu
Nov 16 '05 #12

P: n/a
I have found the solution to my problem. And the solution
is surprising easy. It is

Byte a[100];
char b[100];

for(int i=0; i<a->Length; i++)
{
b[i] = (char) a[i];
}

The resulting char array then works fine for both 1-byte
character and 2-byte character.
Nov 16 '05 #13

P: n/a
Considering that a Byte array *cannot* contain 2-byte characters, the last
sentence about how it's fine for both 1-byte and 2-byte characters is a moot
point

--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.net.tw> wrote in message
news:18****************************@phx.gbl...
I have found the solution to my problem. And the solution
is surprising easy. It is

Byte a[100];
char b[100];

for(int i=0; i<a->Length; i++)
{
b[i] = (char) a[i];
}

The resulting char array then works fine for both 1-byte
character and 2-byte character.

Nov 16 '05 #14

P: n/a
Considering that a Byte array *cannot* contain 2-byte characters, the last
sentence about how it's fine for both 1-byte and 2-byte characters is a moot
point

--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.net.tw> wrote in message
news:18****************************@phx.gbl...
I have found the solution to my problem. And the solution
is surprising easy. It is

Byte a[100];
char b[100];

for(int i=0; i<a->Length; i++)
{
b[i] = (char) a[i];
}

The resulting char array then works fine for both 1-byte
character and 2-byte character.

Nov 16 '05 #15

P: n/a
memcpy is probably faster.

"Nishant S" <ni**@nospam.asianetindia.com> wrote in message
news:uM*************@TK2MSFTNGP12.phx.gbl...
Considering that a Byte array *cannot* contain 2-byte characters, the last
sentence about how it's fine for both 1-byte and 2-byte characters is a moot point

--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.net.tw> wrote in message
news:18****************************@phx.gbl...
I have found the solution to my problem. And the solution
is surprising easy. It is

Byte a[100];
char b[100];

for(int i=0; i<a->Length; i++)
{
b[i] = (char) a[i];
}

The resulting char array then works fine for both 1-byte
character and 2-byte character.


Nov 16 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.