473,238 Members | 1,780 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,238 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 4234
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kevinniu | last post by:
Hi everyone, In c#, what is the fastest way(include unsafe) to convert a array of bytes(which really contains the bytes of a double array) to a arry of double. thanks
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
3
by: Jan | last post by:
How can I convert Int32 (4 bytes long) value to array of bytes? I need only 4 bytes in result. example: long value 55695542 --> arr(0) == &HB6 arr(1) == &HD8 arr(2) == &H51 arr(3) == &H03
2
by: antonio matos | last post by:
hi people. I have to acess a database, and there are some fields that are binary. when i acess that field is read like an array of bytes. but in reality it's an array of doubles!!! how can...
2
by: Jaime Stuardo | last post by:
Hi all... I'm trying to retrieve a SQLXML query using VB.NET. When I programmed in VB 6.0, I used Stream object to accomplish this which was trivial. I cannot do the same thing in VB.NET. Here...
0
by: Jim Rasmussen | last post by:
I need some help in converting a byte array to pdf. Could someone give me an example of how in C#? Here is what I have so far, using System.IO; string sFile = "c:\testpdf.pdf"; FileStream fs =...
5
by: Star | last post by:
Hi all, Let's suppose we have this: byte buffer = new byte; buffer = 0x04; buffer = 0xF1; buffer = 0xB4;
3
by: dpsairam | last post by:
Hi, How to convert a byte array to image....i mean i have to convert a Byte array into image format.....please hellp.........i need the java code... Thanking You.
1
by: phpuser123 | last post by:
I want to convert my object to byte array and then next to an object and run a method..The codes are below: public class test_serialisation implements Serializable{ /** * */ private...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.