Thread View: borland.public.jbuilder.java.language
3 messages
3 total messages
Started by Kent
Thu, 13 Oct 2005 20:29
Big Indian format to Windows format real
Author: Kent
Date: Thu, 13 Oct 2005 20:29
Date: Thu, 13 Oct 2005 20:29
52 lines
2277 bytes
2277 bytes
I have an application written in Delphi that I am porting to
Java. In the delphi program I read in some real values in
BigEndian format. I then convert these to Windows real format
by using a variant record, which Java doesn't have, to swap
bytes so that the proper format can be obtained.
How do I accomplish the same task in Java? Below is the code
that swaps the proper bytes to get the value in the proper
format.
type
realvar = record // Note use of variant record which Java does not have.
case RealNum:byte of
1:(bytename: byte4);
2:(realname: single);
end;
WordVar = record
case WordNum:byte of
1:(ByteName: Byte2);
2:(WordName: Word);
end;
// procedures
function TSDIAppForm.Convert(ByteArray:single):single;
var
temp : realvar;
begin
temp.realname := ByteArray;
swap(temp.bytename[1], temp.bytename[4]);
swap(temp.bytename[2], temp.bytename[3]);
try
begin
if temp.realname < 0 then
Convert := 0
else
Convert := temp.realname;
end
except
on EInvalidOp do
Convert := 0;
end;
end; { of Convert }
function TSDIAppForm.ConvertWord(Value : Word):Word;
var
Temp : Wordvar;
begin
Temp.wordName := Value;
Swap(temp.ByteName[1], Temp.ByteName[2]);
ConvertWord := Temp.WordName;
end; { of ConvertWord }
Re: Big Endian format Found conversion
Author: Kent
Date: Thu, 13 Oct 2005 20:42
Date: Thu, 13 Oct 2005 20:42
55 lines
2433 bytes
2433 bytes
"Kent" <Kent.White@att.net> wrote:
>
>I have an application written in Delphi that I am porting to
>Java. In the delphi program I read in some real values in
>BigEndian format. I then convert these to Windows real format
>by using a variant record, which Java doesn't have, to swap
>bytes so that the proper format can be obtained.
>
>How do I accomplish the same task in Java? Below is the code
>that swaps the proper bytes to get the value in the proper
>format.
>
>type
> realvar = record // Note use of variant record which Java does not have.
> case RealNum:byte of
> 1:(bytename: byte4);
> 2:(realname: single);
> end;
> WordVar = record
> case WordNum:byte of
> 1:(ByteName: Byte2);
> 2:(WordName: Word);
> end;
>
>// procedures
>function TSDIAppForm.Convert(ByteArray:single):single;
>var
> temp : realvar;
>begin
> temp.realname := ByteArray;
> swap(temp.bytename[1], temp.bytename[4]);
> swap(temp.bytename[2], temp.bytename[3]);
> try
> begin
> if temp.realname < 0 then
> Convert := 0
> else
> Convert := temp.realname;
> end
> except
> on EInvalidOp do
> Convert := 0;
> end;
>end; { of Convert }
>
>function TSDIAppForm.ConvertWord(Value : Word):Word;
>var
> Temp : Wordvar;
>begin
> Temp.wordName := Value;
> Swap(temp.ByteName[1], Temp.ByteName[2]);
> ConvertWord := Temp.WordName;
>end; { of ConvertWord }
>
Re: Big Indian format to Windows format real
Author: Gillmer J. Derge [TeamB]
Date: Fri, 14 Oct 2005 04:07
Date: Fri, 14 Oct 2005 04:07
20 lines
1683 bytes
1683 bytes
Kent wrote:
> How do I accomplish the same task in Java? Below is the code
> that swaps the proper bytes to get the value in the proper
> format.
Read the values as 4 bytes, turn them into an int, then use
Float.intBitsToFloat. Something like this (uncompiled and untested, but
if you treat it as pseudocode, it should more or less work).
float readBigEndianFloat(InputStream in) throws IOException {
DataInputStream dataIn = new DataInputStream(in);
byte[] bytes = new byte[4];
dataIn.readFully(bytes);
int i = (byte[3] << 24) | (byte[2] << 16) | (byte[1] << 8) |
byte[0];
return Float.intBitsToFloat(i);
}
--
Gillmer J. Derge [TeamB]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads