how to convert int32 to byte array

M

mehafi

hi,
I need to write a few integer numbers to file. A method write from
FileStream writes an array of bytes. So how to conwert my integers to
byte array?
I've tried Convert class, but it hasn't proper methods.
I've tried also TypeDescriptor.GetConverter ... but got message "cant
convert byte[] to byte[]"

Could someone write me a line of code which will ilustrate this?
thanks in advance
mehafi
 
J

Jon Skeet [C# MVP]

mehafi said:
I need to write a few integer numbers to file. A method write from
FileStream writes an array of bytes. So how to conwert my integers to
byte array?

The easiest way would be to use BinaryWriter.
I've tried Convert class, but it hasn't proper methods.

Have a look at BitConverter.
 
N

Nicholas Paldino [.NET/C# MVP]

mehafi,

You will want to use the BitConverter class, specifically, the GetBytes
method.
 
Q

qglyirnyfgfo

Hey Pete,

This is something that always trips me.

Does the Microsoft .Net framework runs on little and big Indian
platforms (byte order)? If that is the case, does the binary writer
class accounts for this?
 
J

Jon Skeet [C# MVP]

This is something that always trips me.

Does the Microsoft .Net framework runs on little and big Indian
platforms (byte order)? If that is the case, does the binary writer
class accounts for this?

I believe the version in .NET itself always runs little endian.

My MiscUtil library has a version (of both BinaryWriter and
BitConverter) which allows you to specify the endianness:

http://pobox.com/~skeet/csharp/miscutil
 
I

Ignacio Machin ( .NET/ C# MVP )

Hey Pete,

This is something that always trips me.

Does the Microsoft .Net framework runs on little and big Indian
platforms (byte order)? If that is the case, does the binary writer
class accounts for this?






- Show quoted text -

Hi,

I would guess that the framework always run in the same "endianess",
as it's self contained it's ok if everything is inside the framework,
once you have to interop though I guess that the framework version
that is running in a different endianess will take care of marshall
back and forth to the correct endianess.

AFAIK that is all theoretical, .NET only runs in x86 IIRC
 
Q

qglyirnyfgfo

Nice,

So basically, you are saying that if I instruct the BinaryWriter to
write the integer 123456 (11110001001000000) it will always use:

First byte [0] to store 64 (01000000)
Second byte [1] to store 226 (011100010)

No matter if I am running on a little indian processor or big indean
processor right? And of course that the BinaryReader will always read
it the same way not matter what.

PS: Apparently, my company is telling me that your site has been
deemed unsuitable for business use so you have been blocked :)

Thanks
 
J

Jon Skeet [C# MVP]

So basically, you are saying that if I instruct the BinaryWriter to
write the integer 123456 (11110001001000000) it will always use:

<snip>

I believe that's true for the Microsoft .NET implementation. I don't
know if it's true for all other CLI implementations such as Mono, on
all processors.

Naturally I prefer my own library so you can be absolutely explicit :)
PS: Apparently, my company is telling me that your site has been
deemed unsuitable for business use so you have been blocked :)

<sigh>

Try http://yoda.arachsys.com/csharp/miscutil

Jon
 
C

Chris Dunaway

PS: Apparently, my company is telling me that your site has been
deemed unsuitable for business use so you have been blocked :)

Wow. I wonder what they'd do to you if you bought his book and placed
it on your desk?? I shudder to think! :)

Chris
 
J

Jon Skeet [C# MVP]

Wow. I wonder what they'd do to you if you bought his book and placed
it on your desk?? I shudder to think! :)

There is a vaguely understandable reason for this, even if it's not
exactly pleasant. pobox.com is a redirection site, effectively - I use
them to redirect both my mail and web links, so that I can move them
elsewhere at a later date should I wish. (I change my email delivery
address every so often - it's much less likely that I will change web
provider, especially as a lot of people have linked to the "real" site
now anyway.)

Filter software tends to think of pobox.com as a webmail site, and
those are often blocked by companies. Sad but true.

Basically you can currently treat http://pobox.com/~skeet as
http://yoda.arachsys.com - but I'll always try to make sure that the
former works, while the latter may *one day* not.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top