471,354 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

how to convert int32 to byte array

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
Jun 27 '08 #1
7 3955
mehafi,

You will want to use the BitConverter class, specifically, the GetBytes
method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mehafi" <me****@gmail.comwrote in message
news:da**********************************@34g2000h sh.googlegroups.com...
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

Jun 27 '08 #2
On Mon, 02 Jun 2008 12:53:38 -0700, mehafi <me****@gmail.comwrote:
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?
The easiest approach may be just to use the BinaryWriter class, which can
write to a stream the binary representations for a variety of input,
including Int32.

Alternatively, you can use the BitConverter class to generate appropriate
byte arrays that you can then write yourself.

Pete
Jun 27 '08 #3
mehafi <me****@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #4
On Mon, 02 Jun 2008 15:20:23 -0700, <qg**********@mailinator.comwrote:
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.
I believe that is true. It is in fact what the documentation says. :)

If you need architecture-dependent behavior, you can use BitConverter.

Pete
Jun 27 '08 #5
On Jun 2, 11:20 pm, qglyirnyf...@mailinator.com wrote:
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
Jun 27 '08 #6
On Jun 2, 5:20 pm, qglyirnyf...@mailinator.com wrote:
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
Jun 27 '08 #7
On Jun 3, 3:09 pm, Chris Dunaway <dunaw...@gmail.comwrote:
On Jun 2, 5:20 pm, qglyirnyf...@mailinator.com wrote:
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! :)
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
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by kevinniu | last post: by
6 posts views Thread by Ricardo Quintanilla | last post: by
15 posts views Thread by Kueishiong Tu | last post: by
2 posts views Thread by Jaime Stuardo | last post: by

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.