473,388 Members | 1,322 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,388 software developers and data experts.

int to byte []

I'd like to take the value of an int and put the value into a byte array.
Basically the hexidecimal representation of an integer, with out having to
convert the integer to a string and then to a byte.

for example:
int myInt = 800;
// I realize this function doesn't exist but this is what I want to
accomplish
byte[] myByteArray = myInt.GetBytes();
Nov 15 '05 #1
8 40337
Hello,

In C, you can do this using a union. C# doesn't have these, but you can get
the same effect using a struct and controlling the field layout yourself.
For example:

[StructLayout(LayoutKind.Explicit)]
struct IntBytes {
[FieldOffset(0)] public int Integer;
[FieldOffset(0)] public byte Byte0;
[FieldOffset(1)] public byte Byte1;
[FieldOffset(2)] public byte Byte2;
[FieldOffset(3)] public byte Byte3;

public IntBytes(int value) {
Byte0 = Byte1 = Byte2 = Byte3 = 0; //the compiler insists these are
explicitly initialised
Integer = value;
}

public byte[] GetBytes() {
return new byte[] {Byte0, Byte1, Byte2, Byte3};
}
}

You could then use it like this:
int myInt = 800;
IntBytes ib = new IntBytes(myInt);
byte[] bytes = ib.GetBytes();

I'm a C# newbie so that may not be the best code, but it does seem to work.
Another approach would be to create a MemoryStream around a byte array, then
use a BinaryWriter to write the integer to the stream (in effect, to the
array), like this:

byte[] bytes = new byte[4];
BinaryWriter writer = new BinaryWriter(new MemoryStream(bytes));
writer.Write(myInt);
writer.Close();

I hope that helps.
"steve" <cs********@yahoo.com> wrote in message
news:eb****************@TK2MSFTNGP09.phx.gbl...
I'd like to take the value of an int and put the value into a byte array.
Basically the hexidecimal representation of an integer, with out having to
convert the integer to a string and then to a byte.

for example:
int myInt = 800;
// I realize this function doesn't exist but this is what I want to
accomplish
byte[] myByteArray = myInt.GetBytes();

Nov 15 '05 #2
Hello,

In C, you can do this using a union. C# doesn't have these, but you can get
the same effect using a struct and controlling the field layout yourself.
For example:

[StructLayout(LayoutKind.Explicit)]
struct IntBytes {
[FieldOffset(0)] public int Integer;
[FieldOffset(0)] public byte Byte0;
[FieldOffset(1)] public byte Byte1;
[FieldOffset(2)] public byte Byte2;
[FieldOffset(3)] public byte Byte3;

public IntBytes(int value) {
Byte0 = Byte1 = Byte2 = Byte3 = 0; //the compiler insists these are
explicitly initialised
Integer = value;
}

public byte[] GetBytes() {
return new byte[] {Byte0, Byte1, Byte2, Byte3};
}
}

You could then use it like this:
int myInt = 800;
IntBytes ib = new IntBytes(myInt);
byte[] bytes = ib.GetBytes();

I'm a C# newbie so that may not be the best code, but it does seem to work.
Another approach would be to create a MemoryStream around a byte array, then
use a BinaryWriter to write the integer to the stream (in effect, to the
array), like this:

byte[] bytes = new byte[4];
BinaryWriter writer = new BinaryWriter(new MemoryStream(bytes));
writer.Write(myInt);
writer.Close();

I hope that helps.
"steve" <cs********@yahoo.com> wrote in message
news:eb****************@TK2MSFTNGP09.phx.gbl...
I'd like to take the value of an int and put the value into a byte array.
Basically the hexidecimal representation of an integer, with out having to
convert the integer to a string and then to a byte.

for example:
int myInt = 800;
// I realize this function doesn't exist but this is what I want to
accomplish
byte[] myByteArray = myInt.GetBytes();

Nov 15 '05 #3
byte[] myByteArray = System.BitConverter.GetBytes(myInt);
"steve" <cs********@yahoo.com> wrote in message
news:eb****************@TK2MSFTNGP09.phx.gbl...
I'd like to take the value of an int and put the value into a byte array.
Basically the hexidecimal representation of an integer, with out having to
convert the integer to a string and then to a byte.

for example:
int myInt = 800;
// I realize this function doesn't exist but this is what I want to
accomplish
byte[] myByteArray = myInt.GetBytes();

Nov 15 '05 #4
byte[] myByteArray = System.BitConverter.GetBytes(myInt);
"steve" <cs********@yahoo.com> wrote in message
news:eb****************@TK2MSFTNGP09.phx.gbl...
I'd like to take the value of an int and put the value into a byte array.
Basically the hexidecimal representation of an integer, with out having to
convert the integer to a string and then to a byte.

for example:
int myInt = 800;
// I realize this function doesn't exist but this is what I want to
accomplish
byte[] myByteArray = myInt.GetBytes();

Nov 15 '05 #5
Hello

The .NET framework has the BitConverter class, that can convert any
primitive type to a byte array and vice versa.

int myInt = 800;
byte[] myByteArray = System.BitConverter.GetBytes(myInt);

Best regards,
Sherif

"steve" <cs********@yahoo.com> wrote in message
news:eb**************@TK2MSFTNGP09.phx.gbl...
I'd like to take the value of an int and put the value into a byte array.
Basically the hexidecimal representation of an integer, with out having to
convert the integer to a string and then to a byte.

for example:
int myInt = 800;
// I realize this function doesn't exist but this is what I want to
accomplish
byte[] myByteArray = myInt.GetBytes();

Nov 15 '05 #6
Hello

The .NET framework has the BitConverter class, that can convert any
primitive type to a byte array and vice versa.

int myInt = 800;
byte[] myByteArray = System.BitConverter.GetBytes(myInt);

Best regards,
Sherif

"steve" <cs********@yahoo.com> wrote in message
news:eb**************@TK2MSFTNGP09.phx.gbl...
I'd like to take the value of an int and put the value into a byte array.
Basically the hexidecimal representation of an integer, with out having to
convert the integer to a string and then to a byte.

for example:
int myInt = 800;
// I realize this function doesn't exist but this is what I want to
accomplish
byte[] myByteArray = myInt.GetBytes();

Nov 15 '05 #7
That works perfectly. Thanks.

"Stephen Martin" <sm*****@removethis.emsoft.andthis.ca> wrote in message
news:eI****************@TK2MSFTNGP12.phx.gbl...
byte[] myByteArray = System.BitConverter.GetBytes(myInt);

Nov 15 '05 #8
That works perfectly. Thanks.

"Stephen Martin" <sm*****@removethis.emsoft.andthis.ca> wrote in message
news:eI****************@TK2MSFTNGP12.phx.gbl...
byte[] myByteArray = System.BitConverter.GetBytes(myInt);

Nov 15 '05 #9

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

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
16
by: johannblake | last post by:
I have a variable that is 1 bit wide. I also have a variable that is a byte. I want to shift the bits out of the byte into the bit variable (one at a time) but am not sure how to do this or whether...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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...

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.