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

Value Type Conversion to Array

Just wondering if there's a quick way to convert types. I quite often have
things like an int that i want to convert the type to a byte array of length
4.

I guess the only way to do it is using unsafe code but even when doing that
I still have to copy the information to the array.

//this is what I would like to make work
int myInt = 244;
byte[] intAsBytes;

unsafe {
intAsBytes = &myInt;
}

//this is what I have to do
int myInt = 1;
byte[] intAsBytes = new byte[4];

unsafe {
byte* pointer = (byte*)&myInt;
for (int i = 0; i < 4; i++)
intAsBytes[i] = *pointer++;
}
Nov 18 '05 #1
2 1002
Yes, David. Use:

byte[] bytes = BitConverter.GetBytes(myInt);

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 19 Nov 2004 13:47:10 -0800, "David Kyle" <dk***@chloemag.com>
wrote:
Just wondering if there's a quick way to convert types. I quite often have
things like an int that i want to convert the type to a byte array of length
4.

I guess the only way to do it is using unsafe code but even when doing that
I still have to copy the information to the array.

//this is what I would like to make work
int myInt = 244;
byte[] intAsBytes;

unsafe {
intAsBytes = &myInt;
}

//this is what I have to do
int myInt = 1;
byte[] intAsBytes = new byte[4];

unsafe {
byte* pointer = (byte*)&myInt;
for (int i = 0; i < 4; i++)
intAsBytes[i] = *pointer++;
}


Nov 18 '05 #2
Awesome! Works like a charm! Thanks!

Cheers!

David Kyle
www.chloemag.com

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:it********************************@4ax.com...
Yes, David. Use:

byte[] bytes = BitConverter.GetBytes(myInt);

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 19 Nov 2004 13:47:10 -0800, "David Kyle" <dk***@chloemag.com>
wrote:
Just wondering if there's a quick way to convert types. I quite often
have
things like an int that i want to convert the type to a byte array of
length
4.

I guess the only way to do it is using unsafe code but even when doing
that
I still have to copy the information to the array.

//this is what I would like to make work
int myInt = 244;
byte[] intAsBytes;

unsafe {
intAsBytes = &myInt;
}

//this is what I have to do
int myInt = 1;
byte[] intAsBytes = new byte[4];

unsafe {
byte* pointer = (byte*)&myInt;
for (int i = 0; i < 4; i++)
intAsBytes[i] = *pointer++;
}

Nov 18 '05 #3

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

Similar topics

19
by: Method Man | last post by:
I understand that arrays and structs can't be passed by value into and out of functions since they can be arbitrarily big. My question is: Why are types allowed to be passed by value? Couldn't my...
9
by: ckerns | last post by:
I want to loop thru an array of controls,(39 of them...defaults = 0). If value is null or non-numeric I want to assign the value of "0". rowString = "L411" //conrol name if (isNaN(eval...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
3
by: pgconnolly | last post by:
/* foreach does implicit type conversion on elements of a params argument or Generic.List. * This is not good. * Examples of evil follow... */ using System; // I love it when C# is strict...
23
by: Tomás | last post by:
Anything wrong with the following code?: #include <cstdlib> int main() { for (unsigned i = 0; i != 1000; ++i) { int *p = reinterpret_cast<int*>( std::rand() );
40
by: Zach | last post by:
Can someone please explain what this means and illustrate the difference with some code. Thanks, Zach
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.