Reading binary float data as string

Reading binary float data as string

am 03.04.2008 13:36:17 von Jeenu

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

Re: Reading binary float data as string

am 03.04.2008 13:51:36 von Janis Papanagnou

On 3 Apr., 13:36, Jeenu 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

Re: Reading binary float data as string

am 03.04.2008 13:56:37 von Jeenu

On Apr 3, 4:51 pm, Janis wrote:
> On 3 Apr., 13:36, Jeenu 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 = 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.
>
> $ MyFloatData=3.513423
> $ printf "%g\n" "$MyFloatData"
> 3.51342
> $ printf "%s\n" "$MyFloatData * $MyFloatData" | bc
> 12.344141
>
> Is that what you want?
>
>
>
> > Thanks
> > Jeenu

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.

Re: Reading binary float data as string

am 03.04.2008 14:05:45 von Ed Morton

On 4/3/2008 6:56 AM, Jeenu wrote:
> On Apr 3, 4:51 pm, Janis wrote:
>
>>On 3 Apr., 13:36, Jeenu 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 = 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.
>>
>>$ MyFloatData=3.513423
>>$ printf "%g\n" "$MyFloatData"
>>3.51342
>>$ printf "%s\n" "$MyFloatData * $MyFloatData" | bc
>>12.344141
>>
>>Is that what you want?
>>
>>
>>
>>
>>>Thanks
>>>Jeenu
>>
>
> 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.

Re: Reading binary float data as string

am 03.04.2008 14:21:00 von Jeenu

On Apr 3, 5:05 pm, Ed Morton wrote:
> On 4/3/2008 6:56 AM, Jeenu wrote:
>
>
>
> > On Apr 3, 4:51 pm, Janis wrote:
>
> >>On 3 Apr., 13:36, Jeenu 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 = 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.
>
> >>$ MyFloatData=3.513423
> >>$ printf "%g\n" "$MyFloatData"
> >>3.51342
> >>$ printf "%s\n" "$MyFloatData * $MyFloatData" | bc
> >>12.344141
>
> >>Is that what you want?
>
> >>>Thanks
> >>>Jeenu
>
> > 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.

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"

Re: Reading binary float data as string

am 03.04.2008 14:32:14 von Jeenu

On Apr 3, 5:42 pm, pk wrote:
> 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.

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.

Re: Reading binary float data as string

am 03.04.2008 14:42:17 von PK

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.

Re: Reading binary float data as string

am 03.04.2008 16:40:39 von Icarus Sparry

On Thu, 03 Apr 2008 05:32:14 -0700, Jeenu wrote:

> On Apr 3, 5:42 pm, pk wrote:
>> 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.
>
> 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.

You mean like "od -f" (GNU) or "od -tf" (AST) ?

Re: Reading binary float data as string

am 03.04.2008 17:26:32 von Jeenu

On Apr 3, 7:40 pm, Icarus Sparry wrote:
> On Thu, 03 Apr 2008 05:32:14 -0700, Jeenu wrote:
> > On Apr 3, 5:42 pm, pk wrote:
> >> 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.
>
> > 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.
>
> You mean like "od -f" (GNU) or "od -tf" (AST) ?

Hey! I didn't notice od could do it. Many thanks Icarus, for pointing
this out :D