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

Converting right alighed Bytes from integers

Hi All,
I want to Convert 32 bit integers to byte in right alighed format .

For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but
i want right aligned like 00 00 xx xx.Is there any way.

Regards,

Govind.

Jul 19 '05 #1
2 2644
Hi,

Govind wrote:
Hi All,
I want to Convert 32 bit integers to byte in right alighed format .

For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but
i want right aligned like 00 00 xx xx.Is there any way.


// sample

int myInt=456789;
byte[] byteTab=BitConverter.GetBytes(myInt);
Array.Reverse(byteTab);

// now your byte table is reversed

Regards

Marcin

Jul 19 '05 #2
IPAddress class in System.Net namespace also has functions for swapping the
byte orders of short, int, and long.

int netOrder = IPAddress.HostToNetworkOrder(val1);
int hostOrder = IPAddress.HostToNetworkOrder(val2);

For any other type you have to revervse the Byte[] yourself as Marcin
indicated. You want to be carefull that the machine uses the expected byte
ordering that you are converting to. This is handled for you in IPAddress
class.
For instance if you reverse the array, be sure to check the byte order of
the machine first. You may not need to reverse it - "network byte order" is
Big Endian.

Here's a sample of converting a double to and from network order:

private static byte[] HostToNetworkOrder(double d)
{
byte[] data = BitConverter.GetBytes(d);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(data);
}
return data;
}
private static double NetworkToHostOrder(byte[] data)
{
if (BitConverter.IsLittleEndian)
{
Array.Reverse(data);
}
return BitConverter.ToDouble(data, 0);
}
HTH,
Eric Cadwell
http://www.origincontrols.com

"Govind" <go******@interworldgroup.com> wrote in message
news:OP**************@TK2MSFTNGP12.phx.gbl...
Hi All,
I want to Convert 32 bit integers to byte in right alighed format .

For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is there any way.

Regards,

Govind.


Jul 19 '05 #3

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

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
3
by: Golan | last post by:
Hello, I have a hexa file which I need to convert to decimal. I use memcpy into variables (char for one octet, short for 2 octets and int for 4 octets) and then print the value into the file by...
2
by: Mehta Shailendrakumar | last post by:
Hi, I am sending this question again as new question rather than reply to old question Please refer below: struct raw_data { unsigned char x; unsigned char y; }; union full_data
2
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is...
2
by: Jason James | last post by:
Guys, can anyone confirm the process on converting the OID into an array of bytes for sending to the SNMP device. The code I have seems to only work for values in the OID that are less than...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
1
by: freeurmind | last post by:
hello, i'm writing a function that translate Bytes of type unsigned char to integer array of 1's and 0's, i'm taking a message of 33 Bytes and translating them in an int array of 264 integers, here's...
8
by: Santiago Romero | last post by:
Hi :) First of all, I must apologize for my poor english :) I'm starting with python and pygame and for testing (and learning) purposes I wrote an small "Map Editor" for a small game project...
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
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
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...
0
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...

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.