Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Reading binary float data as string
Hi Folks,

I'm wondering whether I could achieve something like this from a
shell:
Suppose I do this from a C program:

MyFloatData = 3.513423;
fwrite(MyFloatData, sizeof(float), 1, pFile);

is there any  way to retrieve the string "3.513243" at the shell? I
would like to pass the string later on to bc or expr for processing.

Thanks
Jeenu

Report this thread to moderator Post Follow-up to this message
Old Post
Jeenu
04-03-08 01:41 PM


Re: Reading binary float data as string
On 3 Apr., 13:36, Jeenu <jee...@gmail.com> wrote:
> Hi Folks,
>
> I'm wondering whether I could achieve something like this from a
> shell:
> Suppose I do this from a C program:
>
> MyFloatData =3D 3.513423;
> fwrite(MyFloatData, sizeof(float), 1, pFile);
>
> is there any =A0way to retrieve the string "3.513243" at the shell? I
> would like to pass the string later on to bc or expr for processing.

$ MyFloatData=3D3.513423
$ printf "%g\n" "$MyFloatData"
3.51342
$ printf "%s\n" "$MyFloatData * $MyFloatData" | bc
12.344141

Is that what you want?

>
> Thanks
> Jeenu


Report this thread to moderator Post Follow-up to this message
Old Post
Janis
04-03-08 01:41 PM


Re: Reading binary float data as string
On Apr 3, 4:51 pm, Janis <janis_papanag...@hotmail.com> wrote:
> On 3 Apr., 13:36, Jeenu <jee...@gmail.com> wrote:
> 
> 
> 
> 
>
> $ MyFloatData=3.513423
> $ printf "%g\n" "$MyFloatData"
> 3.51342
> $ printf "%s\n" "$MyFloatData * $MyFloatData" | bc
> 12.344141
>
> Is that what you want?
>
>
> 

I'm afraid not. I wanted to read the binary data from file. Following
what you wrote:

MyFloatData=$(some_command_or_commands myfile) # MyFloatData should
now contain 3.513423

where "myfile" is to where the fwrite output went in OP.

Report this thread to moderator Post Follow-up to this message
Old Post
Jeenu
04-03-08 01:41 PM


Re: Reading binary float data as string

On 4/3/2008 6:56 AM, Jeenu wrote:
> On Apr 3, 4:51 pm, Janis <janis_papanag...@hotmail.com> wrote:
> 
>
> I'm afraid not. I wanted to read the binary data from file. Following
> what you wrote:
>
> MyFloatData=$(some_command_or_commands myfile) # MyFloatData should
> now contain 3.513423
>
> where "myfile" is to where the fwrite output went in OP.

$ cat myfile
3.513423
$ awk '{print $1 "^2 = " $1 * $1}' myfile
3.513423^2 = 12.3441

Like that?

Ed.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-03-08 01:41 PM


Re: Reading binary float data as string
On Apr 3, 5:05 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 4/3/2008 6:56 AM, Jeenu wrote:
>
>
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>
> $ cat myfile
> 3.513423
> $ awk '{print $1 "^2 = " $1 * $1}' myfile
> 3.513423^2 = 12.3441
>
> Like that?
>
>         Ed.

Still no :)

When I did fwrite in my C program, the file does no more contain the
string "3.513423", instead it's 4-byte (being of type float) *binary
data* - unlike fprintf. So `cat myfile` would give you only some non-
printable characters. My intention is to do something in shell, what
this would do in a C program:

fread(&MyFloatData, sizeof(float), 1, pFile);
sprintf(MyString, "%f", MyFloatData); /* MyString is of type char* */
printf("%s\n", MyString);

Here, program will print "3.513423"

Report this thread to moderator Post Follow-up to this message
Old Post
Jeenu
04-03-08 01:41 PM


Re: Reading binary float data as string
Jeenu wrote:

> Still no :)
>
> When I did fwrite in my C program, the file does no more contain the
> string "3.513423", instead it's 4-byte (being of type float) *binary
> data* - unlike fprintf.

In other words, you want to read the IEEE (or similar) representation of a
float and convert it to a string, IIUC.
If everything else fails, you can always write a small program that fread()s
the data from the file and does a printf on stdout.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Report this thread to moderator Post Follow-up to this message
Old Post
pk
04-03-08 01:41 PM


Re: Reading binary float data as string
On Apr 3, 5:42 pm, pk <p...@pk.invalid> wrote:
> Jeenu wrote: 
> 
>
> In other words, you want to read the IEEE (or similar) representation of a
> float and convert it to a string, IIUC.
> If everything else fails, you can always write a small program that fread(
)s
> the data from the file and does a printf on stdout.
>
> --
> All the commands are tested with bash and GNU tools, so they may use
> nonstandard features. I try to mention when something is nonstandard (if
> I'm aware of that), but I may miss something. Corrections are welcome.

Yup, exactly. I could always use od or xxd to dump other binary data;
since float has a special bit encoding it's not possible ordinarily.
And yes, if nothing helps, I'll have to stick to my small program.

Report this thread to moderator Post Follow-up to this message
Old Post
Jeenu
04-03-08 01:41 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Shell Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:00 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.