473,382 Members | 1,225 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,382 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.

Nov 15 '05 #1
2 1511
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

Nov 15 '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.


Nov 15 '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...
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...
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: 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...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.