Beginners question : casting char[] to byte[] how ?

  • Thread starter Thread starter Sagaert Johan
  • Start date Start date
S

Sagaert Johan

if i have a variable decllared as :
char[] mychararray = new char[50];

and i have some method that needs an byte[] how do i cast or convert this ?

(byte[]) mychararray does not work
 
Sagaert Johan said:
if i have a variable decllared as :
char[] mychararray = new char[50];

and i have some method that needs an byte[] how do i cast or convert this ?

Well, how do you want to convert each char to a byte? A char is a 16
bit value, and a byte is an 8 bit value - what do you want to do with
the "spare" 8 bits for each element?
(byte[]) mychararray does not work

No - because it's *not* a byte array.
 
Look up Serialization on MSDN.
In short, you specifiy your object as serializable with a [Serializable]
attribute, then write it to a file using BinarySerializer or another one.

Greetz,
-- Rob.

Sagaert said:
Okay right , i am used to work with 8&16 bit embedded where a byte
is just an unsigned char.

So i should copy each item i think ?

Another question ..

In C for writing a structure to a file i used a cast
fwrite((void * ) &mystruct,sizeof(mystruct),1,myfilehandle);

how do i write a struct to disk in c#




Jon Skeet said:
Sagaert Johan said:
if i have a variable decllared as :
char[] mychararray = new char[50];

and i have some method that needs an byte[] how do i cast or
convert this ?

Well, how do you want to convert each char to a byte? A char is a 16
bit value, and a byte is an 8 bit value - what do you want to do with
the "spare" 8 bits for each element?
(byte[]) mychararray does not work

No - because it's *not* a byte array.
 
BinarySerializer is this in c# ?

Rob Tillie said:
Look up Serialization on MSDN.
In short, you specifiy your object as serializable with a [Serializable]
attribute, then write it to a file using BinarySerializer or another one.

Greetz,
-- Rob.

Sagaert said:
Okay right , i am used to work with 8&16 bit embedded where a byte
is just an unsigned char.

So i should copy each item i think ?

Another question ..

In C for writing a structure to a file i used a cast
fwrite((void * ) &mystruct,sizeof(mystruct),1,myfilehandle);

how do i write a struct to disk in c#




Jon Skeet said:
if i have a variable decllared as :
char[] mychararray = new char[50];

and i have some method that needs an byte[] how do i cast or
convert this ?

Well, how do you want to convert each char to a byte? A char is a 16
bit value, and a byte is an 8 bit value - what do you want to do with
the "spare" 8 bits for each element?

(byte[]) mychararray does not work

No - because it's *not* a byte array.
 
James F. Bellinger said:
Actually, if you just want to convert char[] into byte[]...

If it just needs to be ASCII...

System.Text.Encoding.ASCII.GetBytes(yourCharArray)

If you just want it to cast characters to bytes, ignoring
characters whose value is 256 or higher (in other words, just
ANSI characters):

System.Text.Encoding.GetEncoding("windows-1252").GetBytes(yourCharArray)

No, that won't work - Windows-1252 has a block between 129 and 140 (I
think - something like that) which converts to Unicode characters way
over 256. If you use ISO Latin 1 instead, that will convert and just
ignore the top 16 bits.

See http://www.pobox.com/~skeet/csharp/unicode.html for more details.
 
One way to write a structure to disk is just write
out each member, using BinaryWriter.

Sagaert Johan said:
Okay right , i am used to work with 8&16 bit embedded where a byte is just
an unsigned char.

So i should copy each item i think ?

Another question ..

In C for writing a structure to a file i used a cast
fwrite((void * ) &mystruct,sizeof(mystruct),1,myfilehandle);

how do i write a struct to disk in c#




Jon Skeet said:
Sagaert Johan said:
if i have a variable decllared as :
char[] mychararray = new char[50];

and i have some method that needs an byte[] how do i cast or convert
this ?

Well, how do you want to convert each char to a byte? A char is a 16
bit value, and a byte is an 8 bit value - what do you want to do with
the "spare" 8 bits for each element?
(byte[]) mychararray does not work

No - because it's *not* a byte array.
 
Back
Top