K
Kueishiong Tu
How do I convert a Byte array (unsigned char managed) to a
char array(unmanaged) with wide character taken into
account?
char array(unmanaged) with wide character taken into
account?
-----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;
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).
-----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);
Kueishiong said:How do I convert a Byte array (unsigned char managed) to a
char array(unmanaged) with wide character taken into
account?
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).
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 ....
-----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.
-----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.
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.