How to convert a Byte array to a char array?

K

Kueishiong Tu

How do I convert a Byte array (unsigned char managed) to a
char array(unmanaged) with wide character taken into
account?
 
N

Nishant S

Byte barray[] = new Byte[512];
__wchar_t* arr = new __wchar_t[barray->Length];
for(int i=0; i < barray->Length; i++)
arr = barray;
delete arr;
 
K

Kueishiong Tu

-----Original Message-----
Byte barray[] = new Byte[512];
__wchar_t* arr = new __wchar_t[barray->Length];
for(int i=0; i < barray->Length; i++)
arr = barray;
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).
 
N

Nishant S

Byte barray[] = new Byte[512];

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

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


--
Regards,
Nish [VC++ MVP]



Kueishiong Tu said:
-----Original Message-----
Byte barray[] = new Byte[512];
__wchar_t* arr = new __wchar_t[barray->Length];
for(int i=0; i < barray->Length; i++)
arr = barray;
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).
 
K

Kueishiong Tu

-----Original Message-----
Byte barray[] = new Byte[512];

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

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

Jochen Kalmbach

Kueishiong said:
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/frlrfsystemruntimeinteropservicesmarshalclassstringtohglobala
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
 
N

Nishant S

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 said:
Byte barray[] = new Byte[512];

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

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


--
Regards,
Nish [VC++ MVP]



Kueishiong Tu said:
-----Original Message-----
Byte barray[] = new Byte[512];
__wchar_t* arr = new __wchar_t[barray->Length];
for(int i=0; i < barray->Length; i++)
arr = barray;
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).

 
N

Nishant S

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 said:
-----Original Message-----
Byte barray[] = new Byte[512];

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

//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 ....
 
K

Kueishiong Tu

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

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

Nishant S

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 = barray;

//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.
 
K

Kueishiong Tu

-----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 = barray;

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


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
 
K

Kueishiong Tu

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

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

Nishant S

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
 
N

Nishant S

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
 
J

Jeffrey Klug

memcpy is probably faster.

Nishant S said:
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 said:
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 = (char) a;
}

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

 

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