Swapping bytes in a double 
Author Message
 Swapping bytes in a double

Let's say I want to read in a double value from a file,
but the bytes are swapped... is there a way to "safely"
swap the bytes in the double value?

For example, if I needed to read in and swap the bytes of
32 bit integer, I could do the following:

internal static uint ReadUInt32Swap(BinaryReader br)
{
  uint outputValue;
  outputValue = (uint) (br.ReadByte() * 16777216);
  outputValue = (uint) (outputValue + br.ReadByte() *
65536);
  outputValue = (uint) (outputValue + br.ReadByte() * 256);
  outputValue = (uint) (outputValue + br.ReadByte());
  return outputValue;

Quote:
}

Is there a way to do a similar thing with a double
floating point value?  If I need to use unsafe code to
obtain the value by using pointers; can I declare
a "double" value outside the unsafe block, and obtain a
pointer to it for placing the unswapped bytes from within
the unsafe code, and then use the double value in the rest
of my "safe" code?

What would this code look like exactly(using the
BinaryReader Class to access the file as a stream of
binary bytes)?  



Tue, 12 Oct 2004 19:11:53 GMT  
 Swapping bytes in a double
Stewart,

    Look at the static GetBytes method on the BitConverter class.  You can
get a byte array representing your data type (integer, double, etc, etc) and
then swap them any way you want.  When done, you can use the other static
methods on the class to re-construct your data type.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Let's say I want to read in a double value from a file,
> but the bytes are swapped... is there a way to "safely"
> swap the bytes in the double value?

> For example, if I needed to read in and swap the bytes of
> 32 bit integer, I could do the following:

> internal static uint ReadUInt32Swap(BinaryReader br)
> {
>   uint outputValue;
>   outputValue = (uint) (br.ReadByte() * 16777216);
>   outputValue = (uint) (outputValue + br.ReadByte() *
> 65536);
>   outputValue = (uint) (outputValue + br.ReadByte() * 256);
>   outputValue = (uint) (outputValue + br.ReadByte());
>   return outputValue;
> }

> Is there a way to do a similar thing with a double
> floating point value?  If I need to use unsafe code to
> obtain the value by using pointers; can I declare
> a "double" value outside the unsafe block, and obtain a
> pointer to it for placing the unswapped bytes from within
> the unsafe code, and then use the double value in the rest
> of my "safe" code?

> What would this code look like exactly(using the
> BinaryReader Class to access the file as a stream of
> binary bytes)?



Tue, 12 Oct 2004 20:19:55 GMT  
 Swapping bytes in a double
Thanks, I'll try it on the airplane today, on my way back
to California from Alabama.

Quote:
>-----Original Message-----
>Stewart,

>    Look at the static GetBytes method on the

BitConverter class.  You can
Quote:
>get a byte array representing your data type (integer,

double, etc, etc) and
Quote:
>then swap them any way you want.  When done, you can use
the other static
>methods on the class to re-construct your data type.

>    Hope this helps.

>--
>               - Nicholas Paldino [.NET MVP]



>> Let's say I want to read in a double value from a file,
>> but the bytes are swapped... is there a way to "safely"
>> swap the bytes in the double value?

>> For example, if I needed to read in and swap the bytes
of
>> 32 bit integer, I could do the following:

>> internal static uint ReadUInt32Swap(BinaryReader br)
>> {
>>   uint outputValue;
>>   outputValue = (uint) (br.ReadByte() * 16777216);
>>   outputValue = (uint) (outputValue + br.ReadByte() *
>> 65536);
>>   outputValue = (uint) (outputValue + br.ReadByte() *
256);
>>   outputValue = (uint) (outputValue + br.ReadByte());
>>   return outputValue;
>> }

>> Is there a way to do a similar thing with a double
>> floating point value?  If I need to use unsafe code to
>> obtain the value by using pointers; can I declare
>> a "double" value outside the unsafe block, and obtain a
>> pointer to it for placing the unswapped bytes from
within
>> the unsafe code, and then use the double value in the
rest
>> of my "safe" code?

>> What would this code look like exactly(using the
>> BinaryReader Class to access the file as a stream of
>> binary bytes)?

>.



Wed, 13 Oct 2004 01:59:55 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. float byte swapping ?

2. help with byte swapping

3. swapping the nibbles in a byte !!

4. SIMPLE: Byte Swapping on Signed & Unsigned Shorts

5. Byte swapping

6. Question: Correct process to byte-swap floats?

7. Byte swapping on different architectures!

8. How can I swap bytes

9. byte swapping???

10. Efficient way to swap bytes in Turbo C 2.0

11. Byte swapping

12. Byte swapping

 

 
Powered by phpBB® Forum Software