473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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


Nov 15 '05 #1
7 17111
Sagaert Johan <RE*******************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
Sagaert Johan <RE*******************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
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 Johan wrote:
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Sagaert Johan <RE*******************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #4
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 Johan wrote:
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Sagaert Johan <RE*******************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #5
BinarySerializer is this in c# ?

"Rob Tillie" <Ro********@student.tul.edu> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
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 Johan wrote:
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Sagaert Johan <RE*******************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


Nov 15 '05 #6
James F. Bellinger <Na*************@do.not.spam.me> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #7
One way to write a structure to disk is just write
out each member, using BinaryWriter.

"Sagaert Johan" <RE*******************@hotmail.com> wrote in message news:<uj**************@tk2msftngp13.phx.gbl>...
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Sagaert Johan <RE*******************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #8

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

Similar topics

2
by: seia0106 | last post by:
Hello I am writing a function to read a binary file. Here is a part of code #include <fstream> .. .. BYTE *pData; long lDataLen; pms->GetPointer(&pData); lDataLen = pms->GetSize(); // Read...
4
by: drowned | last post by:
I'm having a problem understanding why the code in this little program I've got is behaving the way it does... if anyone can explain it, I'd be grateful: #include <iostream> using namespace...
4
by: conf | last post by:
long* long_pointer; long_pointer is used to read 4 bytes each time. Then, short* short_pointer = <short *>(long_pointer); to read 2 bytes from now on.
4
by: Harry | last post by:
Hi ppl Some problem regarding pointer: ULONG *addr; CHAR *ptr=NULL; addr=new ULONG; //calling a function and getting the addr value //ULONG is 4 bytes. I am getting the Ip address which...
20
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct...
19
by: Ramesh Tharma | last post by:
Hi, Is any one knows what's wrong with the following code, I was told that it will compile and run but it will crash for some values. Assume that variables are initilized. char* c; long*...
0
by: Sagaert Johan | last post by:
if i have a variable decllared as : char mychararray = new char; and i have some method that needs an byte how do i cast or convert this ? (byte) mychararray does not work
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
11
by: jois.de.vivre | last post by:
I am interfacing with a third party API (written in C, if that matters) that has an "event handler" function with the following definition: void event_handler(int event_code, unsigned long...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...

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.