473,654 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4263
mehafi,

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

"mehafi" <me****@gmail.c omwrote in message
news:da******** *************** ***********@34g 2000hsh.googleg roups.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.c omwrote:
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.c omwrote:
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.co m>
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**********@m ailinator.comwr ote:
So basically, you are saying that if I instruct the BinaryWriter to
write the integer 123456 (11110001001000 000) 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...@ma ilinator.com wrote:
So basically, you are saying that if I instruct the BinaryWriter to
write the integer 123456 (11110001001000 000) 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...@ma ilinator.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...@ma ilinator.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
18824
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
10184
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 the data converted from the byte array into a string , is not what i expect. example: - the data returned from the socket is (byte array) "Usuario Sin Atribuciones Para Esta Función"
15
34583
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
9078
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
9351
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 i convert from a byte array to a double array?
2
2641
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 the most similar object is the OleDbDataReader. I have the following method, ¿is it correct what I am doing? By doing oRdr.GetBytes(0, 0, bBytes, 0, 1000) I'm limiting only to 1000 bytes. How can I know the length of the stream?
0
4322
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 = File.Create(sFile); BinaryWriter bw = new BinaryWriter(fs);
5
13704
by: Star | last post by:
Hi all, Let's suppose we have this: byte buffer = new byte; buffer = 0x04; buffer = 0xF1; buffer = 0xB4;
3
11576
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
13346
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 static final long serialVersionUID = 1L;
0
8372
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8285
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.