From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,2aaba1527862ef22 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 30 Jan 2007 14:02:18 -0600 Date: Tue, 30 Jan 2007 20:02:15 +0000 From: Martin Dowie User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Reading Float Data from a binary file into ada References: <1170172307.292500.256090@m58g2000cwm.googlegroups.com> In-Reply-To: <1170172307.292500.256090@m58g2000cwm.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <2qKdna5hWvrXOSLYnZ2dnUVZ8sTinZ2d@bt.com> NNTP-Posting-Host: 81.158.163.147 X-AuthenticatedUsername: NoAuthUser X-Trace: sv3-Q1CpoclxA2ljQlvz/JPmJLlhQy/thuac+uIqqo7YlgMElEG3T0XwXACH1L0Ufq2gzscVxsVDEqU1jl2!1N1HuMUBp4AMOQZAYmmD8LMoXvB9jVudCAxEBh12uKYOoOivawqivCe95FamLCY98FynM+vNJfdz X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news2.google.com comp.lang.ada:8733 Date: 2007-01-30T20:02:15+00:00 List-Id: frikk wrote: > Hello everyone! I am having a problem that I would love some help > with. > > Essentially I was given a Visual Basic program that dumps a binary > configuration file with all of the variables in a set. The variables > are each 32 bit floats, with the first 16 bits being the integer part > and the second 16 bits being a representation of the fraction (I'm not > sure if this is stanard - but its just how VB dumps the data). The > binary dump is basically a copy of the way VB stores the data in > memory. I need to be able to use this data in ada. There is a C > counterpart to this that makes use of a 'union' to grab the data 1 > byte (8 bits) at a time, put them into a char array of size 4, then > use a 32 bit float to reference the data. Is there somehow I can do > this in ada as well? > > Basically I need to be able to read in the binary data byte by byte > but store it into a 32 bit Float. The C union example above uses the > same memory address for the Float as it does for the size 4 char > array. I don't even know if the VB dump will correspond with the way > ada handles floats or not, but I'll worry about that later. > > I am also using Matlab/Simulink if that provides any additional tools > to use for debugging. Here's a small example that I think shows what you need (doesn't deal with any endian issues!) and goes the other way to what you need (fixed-point -> unsigned 32-bit) but you'll get the idea. The one line I'm not sure about is if both 'Small and 'Size are needed - I always use both but I think 'Size may not strictly be needed. Hope this helps! Cheers -- Martin -- File: fp_example.adb with Ada.Text_IO; use Ada.Text_IO; with Ada.Unchecked_Conversion; with Interfaces; use Interfaces; procedure FP_Example is -- Defines the LSB FP_Delta : constant := 2.0**(-16); type FP is delta FP_Delta range -2.0**15 .. 2.0**15-FP_Delta; for FP'Small use FP_Delta; for FP'Size use 32; F1 : FP := FP'First; F2 : FP := FP'Last; package FP_IO is new Ada.Text_IO.Fixed_IO (FP); use FP_IO; package Unsigned_32_IO is new Ada.Text_IO.Modular_IO (Unsigned_32); use Unsigned_32_IO; function To_Unsigned_32 is new Ada.Unchecked_Conversion (Source => FP, Target => Unsigned_32); begin Put (F1, Aft => 16); New_Line; Put (F2, Aft => 16); New_Line; Put (To_Unsigned_32 (F1), Base => 16); New_Line; Put (To_Unsigned_32 (F2), Base => 16); New_Line; end FP_Example; c:\ada\fp_example\fp_example.exe -32768.0000000000000000 32767.9999847412109375 16#80000000# 16#7FFFFFFF# process terminated successfully