473,666 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert int/short/long to bytes[]

I can't find any functions in system.convert to convert
a 4-byte integer to 4 bytes array...
pls help
Nov 15 '05 #1
5 64454
Hi,
Here is an example for int to byte[4] conversion:

buf[0] = (byte) value;
buf[1] = (byte) value >> 8;
buf[2] = (byte) value >> 16;
buf[3] = (byte) value >> 24;

--
Andrew Gnenny
pulsar2003@/no-spam/email.ru (Please remove /no-spam/ for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
"babylon" <am***@hotmail. com> wrote in message
news:O2******** ******@TK2MSFTN GP12.phx.gbl...
I can't find any functions in system.convert to convert
a 4-byte integer to 4 bytes array...
pls help

Nov 15 '05 #2

"babylon" <am***@hotmail. com> wrote in message
news:O2******** ******@TK2MSFTN GP12.phx.gbl...
I can't find any functions in system.convert to convert
a 4-byte integer to 4 bytes array...
pls help

There isn't one in System.Convert, however, System.BitConve rter.GetBytes()
will do what your need.

Nov 15 '05 #3
thx!

any builtin-library for converting an int from little endian to big endian?
as I want to send my data over the network....
i can only find "System.Text.AS CIIEncoding.Big EndianUnicode", which is not
what i wanted...

thx!
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:TB4lb.6032 21$cF.271595@rw crnsc53...

"babylon" <am***@hotmail. com> wrote in message
news:O2******** ******@TK2MSFTN GP12.phx.gbl...
I can't find any functions in system.convert to convert
a 4-byte integer to 4 bytes array...
pls help


There isn't one in System.Convert, however, System.BitConve rter.GetBytes()
will do what your need.


Nov 15 '05 #4
got it
NetworkToHostOr der of IPAddress
"babylon" <am***@hotmail. com> wrote in message
news:Oj******** ******@TK2MSFTN GP12.phx.gbl...
thx!

any builtin-library for converting an int from little endian to big endian? as I want to send my data over the network....
i can only find "System.Text.AS CIIEncoding.Big EndianUnicode", which is not
what i wanted...

thx!
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:TB4lb.6032 21$cF.271595@rw crnsc53...

"babylon" <am***@hotmail. com> wrote in message
news:O2******** ******@TK2MSFTN GP12.phx.gbl...
I can't find any functions in system.convert to convert
a 4-byte integer to 4 bytes array...
pls help


There isn't one in System.Convert, however, System.BitConve rter.GetBytes() will do what your need.



Nov 15 '05 #5
>I can't find any functions in system.convert to convert
a 4-byte integer to 4 bytes array...

I have been using System.BitConve rt.GetBytes() in my code at work.
Something like this works for me:

byte[] bytes = System.BitConve rter.GetBytes(M yIntegerValue);

Nov 15 '05 #6

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

Similar topics

7
11075
by: Ben Kial | last post by:
Is there a Python program to convert Windows long filename, like "c:\Program Files" into the old DOS 8.3 format, like "c:\Progra~1"? Thanks in advance, Ben
7
13350
by: LaMoRt | last post by:
Hi I want to ask how to convert from long to short since i have some parameter problem with it? The problem is like below : short *speech; // Holds length of any given sample * speech = pMediaSample->GetActualDataLength(); va_g729a_encoder(short *speech, unsigned char *bitstream );
15
2037
by: David Geering | last post by:
Is there any standard as to what a long and int mean or is it compiler dependent? Or furthermore architecture dependent? I know a short short long int isn't actually a recognised type, but if you wanted to make sure it was 16-bit, 32-bit or 64-bit etc, can you rely on these type names alone? If not, is there any way to ensure you get a 64/32/16-bit int when you want one? Cheers, Dave.
1
16482
by: =?big5?B?qM6nuw==?= | last post by:
I already knew how to convert bytes to Double, use BitConverter.ToDouble(). but, How to convert double to bytes? for example: double a = 3,444; I want convert to bytes={ ?, ?, ?, ?, ?, ?, ?, ?}
8
13704
by: Phil Endecott | last post by:
Dear Experts, I'm surprised to find that std::max doesn't work (i.e. won't compile) if the arguments are not of exactly the same type, e.g. one is a short and the other is a long: #include <algorithm> int f(short s, long l) { return std::max(s,l);
5
6550
OuTCasT
by: OuTCasT | last post by:
Hi. I would like to know how to convert the short date format to the long date format for my application without changing the regional settings from short date to long date. ?
0
8866
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...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
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
8638
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
6191
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
5662
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();...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.