473,738 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Ge tBytes(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 2679
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.Ge tBytes(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=BitConv erter.GetBytes( myInt);
Array.Reverse(b yteTab);

// 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.HostT oNetworkOrder(v al1);
int hostOrder = IPAddress.HostT oNetworkOrder(v al2);

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[] HostToNetworkOr der(double d)
{
byte[] data = BitConverter.Ge tBytes(d);
if (BitConverter.I sLittleEndian)
{
Array.Reverse(d ata);
}
return data;
}
private static double NetworkToHostOr der(byte[] data)
{
if (BitConverter.I sLittleEndian)
{
Array.Reverse(d ata);
}
return BitConverter.To Double(data, 0);
}
HTH,
Eric Cadwell
http://www.origincontrols.com

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

For 32 = the usual way is BitConverter.Ge tBytes(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
13924
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 something like: char chars = "Hello!"; byte bytes = (byte) chars;
25
6737
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 bytes. I've tried using bitwise operators, but they seem to convert the value into an integer first, and i've tried using the toString() method to convert it into a hex value so i can parse it, but that also seems to first convert it into an...
3
6890
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 using fprintf. The problem is that I don't know how to convert a field of 6 octets? Should I use a long variable? Thanks
2
9773
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
1527
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 there any way. Regards, Govind.
2
6984
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 2^14 but some enterprise IDs are now larger than that. Do I just contine the conversion on to additional bytes needed to house the OID in full? So and enterprise ID of 25000 would be sent as three bytes of the following:
21
2018
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 attributes (colls in a data base/ csv file, eg. height weight income etc.) to determine the most efficient 'type' to convert the attribute coll into for further processing and efficient storage... Example row from sampled file data: , ....]
1
1803
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 the function: void readByte(unsigned char * buffer, int binarr) { unsigned char mask = 1 << 7; bool mybit; int j =0; for ( int i =1; i <= sizeof(binarr) ; i++) { mybit = (bool) (buffer & mask);
8
2082
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 I'm going to start next month. The tilemap editor is working fine, but reading Guido's Van Rossum PYTHON TUTORIAL I found that my game map is "wasting" memory by using
0
8968
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9334
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
9259
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
9208
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
6750
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
6053
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
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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.