Interpreting bit patterns as arbitrary types

R

Ray Mitchell

Hello,

I need to be able to interpret the bit pattern in a 32-bit unsigned int as
if it represents 32-bit type float. I obviously can't simply assign the
unsigned int object to a float object because the type conversion would
change the bit pattern. In C/C++ this is an extremely trivial operation,
which I would typically do using one of the following:


int i = some pattern;
float f;

// The simplest solution
f = *(float *)&i;


// Another solution
union { int iu; float fu; } u;

u.iu = i;
f = i.fu;


However, in C# there are no unions, and pointers result in unmanaged code.
Is there a trivial way to accomplish what I want in C#?

Thanks,
Ray
 
A

Arne Vajhøj

Ray said:
I need to be able to interpret the bit pattern in a 32-bit unsigned int as
if it represents 32-bit type float. I obviously can't simply assign the
unsigned int object to a float object because the type conversion would
change the bit pattern.

Try:

f = BitConverter.ToSingle(BitConverter.GetBytes(i));

Arne
 
M

Mike Schilling

Arne said:
Try:

f = BitConverter.ToSingle(BitConverter.GetBytes(i));

Having just used this a few days ago, I can nitpickily correct this to

f = BitConverter.ToSingle(BitConverter.GetBytes(i), 0);

The second argument says "start the conversion at byte number 0".

In general, any of these "reinterpret one primitive type as another"
conversion are done by first calling

byte[] bytes = BitConverter.GetBytes(input);

to turn the input into a byte array, followed by

XXX output = BitConverter.ToXXX(bytes, 0)

to turn those bytes into the desired output. Converting, say, two
integers into a double is left as an exercise for the reader.
 
R

Roberto Collina

Hello,
I'd like to mention explicitly layed-out structures, as in:

[StructLayout(Layout.Explicit)] // found in
System.Runtime.InterOpServices
struct UnionEx
{
[Offset(0)]
public uint A;
[Offset(0)]
public float B;
}

Just a thought.

Regards,
r.

Arne said:
f = BitConverter.ToSingle(BitConverter.GetBytes(i));

Having just used this a few days ago, I can nitpickily correct this to

f = BitConverter.ToSingle(BitConverter.GetBytes(i), 0);

The second argument says "start the conversion at byte number 0".

In general, any of these "reinterpret one primitive type as another"
conversion are done by first calling

    byte[] bytes = BitConverter.GetBytes(input);

to turn the input into a byte array, followed by

    XXX output = BitConverter.ToXXX(bytes, 0)

to turn those bytes into the desired output.  Converting, say, two
integers into a double is left as an exercise for the reader.
 
A

Arne Vajhøj

Mike said:
Having just used this a few days ago, I can nitpickily correct this to

f = BitConverter.ToSingle(BitConverter.GetBytes(i), 0);

I always forget that argument.

:-(

MS should have made overloads with and without.

Arne
 
A

Arne Vajhøj

Jeff said:
Roberto Collina said:
Hello,
I'd like to mention explicitly layed-out structures, as in:
[StructLayout(Layout.Explicit)] // found in
System.Runtime.InterOpServices
struct UnionEx
{
[Offset(0)]
public uint A;
[Offset(0)]
public float B;
}
Just a thought.

Okay, you mentioned them. WHY did you mentioned them?

It is very handy in an obfuscated code contest ...

:)

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top