PC Review


Reply
Thread Tools Rate Thread

Converting right alighed Bytes from integers

 
 
Govind
Guest
Posts: n/a
 
      26th Aug 2003
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.





 
Reply With Quote
 
 
 
 
=?ISO-8859-2?Q?Marcin_Grz=EAbski?=
Guest
Posts: n/a
 
      26th Aug 2003
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

 
Reply With Quote
 
Eric Cadwell
Guest
Posts: n/a
 
      26th Aug 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Converting integers to Big Endian Bytes Govind Microsoft Dot NET 7 27th Aug 2003 04:23 AM
Re: Converting integers to Big Endian Bytes Govind Microsoft Dot NET Framework 5 26th Aug 2003 05:31 PM
Converting right alighed Bytes from integers Govind Microsoft C# .NET 2 26th Aug 2003 04:42 PM
Converting right alighed Bytes from integers Govind Microsoft Dot NET 2 26th Aug 2003 04:42 PM
Converting right alighed Bytes from integers Govind Microsoft Dot NET Framework 2 26th Aug 2003 04:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:39 AM.