Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

output big endian IEEE reals

Status
Not open for further replies.

rsballard

Technical User
Oct 1, 2002
16
US
I'm converting binary data in two programs. Had everything working OK until I found that all the binary data needs to be in Big endian byte order. So...

Situation 1: the input data is Intel standard IEEE reals. I need to output same as BIG endian IEEE reals

Situation 2: the input data is 16bit signed integer output directly from a high precision low distortion A/D. The data is 16channel multiplexed data. I read in, convert to single, apply gain, amplify and demux to an IEEE real array. Then I need to output as BIG endian IEEE reals.

In both cases I also have two binary buffers of 16bit and 32 bit integers that will need to be output as BIG endian. I think I can just use swap() on these buffers and be OK.

Can I use the following routine (found on the web) for the reals to convert to big endian?

function EndianLong(L : longint) : longint;
begin
result := swap(L shr 6) or
(longint(swap(L and $ffff)) shl 6);
end;

"EndianLong" was designed for 4 byte integers but will my IEEE exponent survive if I use this one or is there a big endian routine specifically for reals?

Thanks
Bob
 
I am not sure exactly what you are trying to do, I have written a set of IEEE conversion routines, they run to a few hundred lines of code!.

e.g function declarations

// Access Functions
//Convert IEEE Hexadecimal value to Normal Decimal
function IeeeToDecimal(Ieee: string): extended;

// Convert Decimal value to IEEE hexadecimal
function DecimalToIeee(Decimal: extended): String;

// split an IEEE hex string into byte values string
function GetIEEEByte(IEEE: string; Which: Integer): Byte;

// make an IEEE string from 4 bytes
function GetIEEEvalue(B1,B2,B3,B4: byte): string;


// Calculation functions
function Decimal(Bin: string): extended;
function Binary(Y: integer; size: integer): string;
function sign(Y: extended): string;
function BinToFrac(Bin: string): extended;
function BinaryFraction(Y: extended): string;
function Normalised(X: extended; var N: byte): extended;
function bintohex(Bin: string): string;

function Power(Base, Exponent: Extended): Extended;
function IntPower(Base: Extended; Exponent: Integer): Extended;
function Ldexp(X: Extended; P: Integer): Extended;

const maxint = 2147483648.0; // $80000000

This was for an interface with a emmbeded device that returns vales in IEEE format the delphi code had to display the actual values.

Steve.


 
Steve, thanks but what I need is to do a 4 byte conversion 'in place' that 16 bit word swaps then byte swaps within those two words so I go from 0123 byte order to 3210 byte order.

Found

function EndianLong(L : longint) : longint;
begin
result := swap(L shr 16) or
(longint(swap(L and $ffff)) shl 16);
end;

and

function ZReverse(a: Integer): Integer;
begin
Result := ($ff000000 and (a shl 24))
or ($00ff0000 and (a shl 8))
or ($0000ff00 and (a shr 8))
or ($000000ff and (a shr 24));
end;

on the web but I want to have a pointer version so I don't need another variable or buffer to store the results. I want to re-use the same space for the results. What's the best thing to do? Change them to procedures and use a pointer? Really just learning to crawl on this type of stuff.

Bob
 
Any Pointer can be casted to LongInt or Cardinal or any other 32 bit integer type. Here's another example func for swaping bytes in double word:
Code:
function SwapBytes(L : Cardinal): Cardinal;
begin
  asm
    PUSH  EBX
    XCHG  AH, AL
    MOV   BX, AX
    SHR   EAX, 16
    XCHG  AH, AL
    PUSH  AX
    MOV   AX, BX
    SHL   EAX, 16
    POP   AX
    MOV   Result, EAX
    POP   EBX
  end;
end;
Hope that helps

--- markus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top