473,692 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert a Byte array to a char array?

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
15 34587
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.ne t.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
-----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
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(barra y);
--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.ne t.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
-----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(barra y);


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
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: StringToHGlobal Ansi (System::Runtim e::InteropServi ces::Marshal)
http://msdn.microsoft.com/library/en-
us/cpref/html/frlrfsystemrunt imeinteropservi cesmarshalclass stringtohglobal a
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: StringToHGlobal Uni
or use PtrToStringChar s (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
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.as ianetindia.com> wrote in message
news:up******** ******@tk2msftn gp13.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(barra y);
--
Regards,
Nish [VC++ MVP]

"Kueishiong Tu" <ks****@seed.ne t.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
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.ne t.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(barra y);


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
-----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::Uplo adData 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
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(barra y));
__wchar_t* array = (__wchar_t*)Mar shal::StringToH GlobalUni(
tmp).ToPointer( );
//...
// You can now use array (wide char array)
//...
Marshal::FreeCo TaskMem(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.ne t.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::Uplo adData 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3246
by: spammersarevermin | last post by:
I'm testing a VoIP application, and I can't figure out how to dump this into a Udpclient.send function. Thanks for any help, pointers... How do I convert the array below to VB.NET? -- unsigned char grq = { 0x03, 0x20, 0x00, 0x00, 0x06, 0x00, 0x08, 0x91, 0x4a, 0x00, 0x02, 0x00, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x72,
27
51710
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I have some legcay C code that needs to process a string taken from a textbox, then I need to re-display the string as the textbox->Text. I easily found how to convert from system::string to char but I can't figure out how to go the other way!!
7
117993
by: AlexFarokhyans | last post by:
Hello, I'm trying to convert String to Char array. I'm getting a string from user input text box and then I have char firstName. I need to convert the string that is in the text box to firstName. Thank you
2
32316
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in what order the data is stored so i try both ways. unsigned char array2 = { 0x23, 0x44, 0xc2, 0xde}; float *pfloat1, *pfloat2;
1
2881
by: vsk | last post by:
Hai, I need to know how to convert an unsigned char array into hexstring. can anyone help me in this regard?. Thanks.
4
42872
by: John Smith | last post by:
Hello, Suppose I have the following C# code which I want to convert to VB: for (int i = 0; i < nFieldLength; i++) Console.Write((char) sValue); sValue is a byte array. The problem is the typecast which I can't find an equivalent for in VB. I
4
17363
by: Man4ish | last post by:
HI , I am trying to convert string into char array of characters.but facing problem. #include <iostream> #include <string> using namespace std; int main() { string t="1,2,3,4,5,6";
3
59485
by: Brandon | last post by:
How do I convert a string to a char array? I am doing this so I can edit the string received from an sql query so I can remove unnecessary characters.
4
16284
by: deeas | last post by:
hi i receive bytes from server into microcontroller , how can i convert them to char
2
9065
by: Lohith r | last post by:
Hi Friends, I have a serious and irritating problem, please help mdContext->digest is an unsigned char Array with hexadecimal values so for (i = 0; i < 16; i++) printf ("%02x", mdContext->digest); prints 900150983cd24fb0d6963f7d28e17f72
0
8604
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9083
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8961
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8800
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8801
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5819
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.