473,748 Members | 2,685 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 34601
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
3247
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
51726
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
117998
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
32322
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
2889
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
42921
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
17372
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
59493
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
16310
by: deeas | last post by:
hi i receive bytes from server into microcontroller , how can i convert them to char
2
9083
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
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9381
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
9332
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
9254
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
8252
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
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
2791
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.