Forum: Ruby So I got a binary file...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0904ced682c71189fc0763daf24ea07f?d=identicon&s=25 bostonmacosx (Guest)
on 2006-05-01 01:16
Sorry for being such a NEWB. I'm a pretty prolific PHP writer however
I'm new to RUBY and want to expand my horizons.

I have a big-endian binary file and want to convert it to ascii so I see
what's in it.
Is there an easy way to accomplish this?

Thanks
Robert
453336cfba0446c4eb97ee925324517d?d=identicon&s=25 William Stevens (wfspoet)
on 2006-05-01 16:50
bostonmacosx wrote:
> Sorry for being such a NEWB. I'm a pretty prolific PHP writer however
> I'm new to RUBY and want to expand my horizons.
>
> I have a big-endian binary file and want to convert it to ascii so I see
> what's in it.
> Is there an easy way to accomplish this?

Do you just want a hexdump of the contents were only the standard
printable characters are displayed?
If so you can do this, on Mac OS X, by typing 'hexdump -C <the file in
question>' in a terminal window.

>
> Thanks
> Robert

Good luck,
Bill
0904ced682c71189fc0763daf24ea07f?d=identicon&s=25 bostonmacosx (Guest)
on 2006-05-01 18:58
f 80 00 00 bf 80 00 00  bf 80 00 00 c0 00 00 00  |................|
00046170  bf 80 00 00 bf 80 00 00  3f 80 00 00 3f 80 00 00
|........?...?...|
00046180  bf 80 00 00 bf 80 00 00  c0 00 00 00 c0 40 00 00
|.............@..|
00046190  c0 00 00 00 bf 80 00 00  40 40 00 00 40 a0 00 00
|........@@..@...|
000461a0  40 e0 00 00 40 e0 00 00  40 c0 00 00 40 a0 00 00
|@...@...@...@...|
000461b0  40 40 00 00 40 00 00 00  40 00 00 00 40 40 00 00
|@@..@...@...@@..|
000461c0  40 80 00 00 40 a0 00 00  40 a0 00 00 40 a0 00 00
|@...@...@...@...|
000461d0  40 80 00 00 40 40 00 00  40 00 00 00 40 40 00 00
|@...@@..@...@@..|
000461e0  40 40 00 00 40 80 00 00  40 a0 00 00 40 c0 00 00
|@@..@...@...@...|
000461f0  40 c0 00 00 40 a0 00 00  40 40 00 00 40 00 00 00
|@...@...@@..@...|
00046200  3f 80 00 00 40 00 00 00  40 80 00 00 40 a0 00 00
|?...@...@...@...|
00046210  40 c0 00 00 40 a0 00 00  40 a0 00 00 40 80 00 00
|@...@...@...@...|
00046220  40 40 00 00 40 40 00 00  40 40 00 00 40 40 00 00
|@@..@@..@@..@@..|

Really means nothing to me. What I'm looking to find is turning the
whole thing into a human readable ASCII format.


William Stevens wrote:
> bostonmacosx wrote:
>> Sorry for being such a NEWB. I'm a pretty prolific PHP writer however
>> I'm new to RUBY and want to expand my horizons.
>>
>> I have a big-endian binary file and want to convert it to ascii so I see
>> what's in it.
>> Is there an easy way to accomplish this?
>
> Do you just want a hexdump of the contents were only the standard
> printable characters are displayed?
> If so you can do this, on Mac OS X, by typing 'hexdump -C <the file in
> question>' in a terminal window.
>
>>
>> Thanks
>> Robert
>
> Good luck,
> Bill
0904ced682c71189fc0763daf24ea07f?d=identicon&s=25 bostonmacosx (Guest)
on 2006-05-01 22:53
mydata = String.new
f = File.open("WesObs_Sumatra_032805.sac","rb")
#while line = f.gets
#mydata << line
#end
newstring =  f.read
#puts newstring

newdata = newstring.unpack('f*')
#puts newdata
fw = File.open("test.txt","wb")
fw.write(newdata)

Ok here is what I have.
It works however I need to do two things.

the Correct Data:
      0.1646618    -194.0000000     194.0000000  -12345.0000000
-12345.0000000
      0.0000000   11823.0429688       0.0000000  -12345.0000000
-12345.0000000
 -12345.0000000  -12345.0000000  -12345.0000000  -12345.0000000
-12345.0000000
 -12345.0000000  -12345.0000000  -12345.0000000  -12345.0000000
-12345.0000000
 -12345.0000000  -12345.0000000  -12345.0000000  -12345.0000000
-12345.0000000
 -12345.0000000  -12345.0000000  -12345.0000000  -12345.0000000
-12345.0000000
 -12345.0000000      42.3849983     -71.3219986      60.0000000
-12345.0000000
 -12345.0000000  -12345.0000000  -12345.0000000  -12345.0000000
-12345.0000000
 -12345.0000000  -12345.0000000  -12345.0000000  -12345.0000000
-12345.0000000
 -12345.0000000  -12345.0000000  -12345.0000000  -12345.0000000
-12345.0000000
 -12345.0000000  -12345.0000000  -12345.0000000  -12345.0000000
-12345.0000000
 -12345.0000000       1.5362868       0.0000000       0.0000000
-12345.0000000
 -12345.0000000  -12345.0000000  -12345.0000000  -12345.0000000
-12345.0000000
 -12345.0000000  -12345.0000000  -12345.0000000  -12345.0000000
-12345.0000000
      2005         87         16         17         36
       963          6     -12345     -12345      71803
    -12345     -12345     -12345     -12345     -12345
         1          7         10     -12345     -12345
    -12345     -12345          5         45     -12345
    -12345     -12345     -12345     -12345     -12345
    -12345     -12345     -12345     -12345     -12345
         1          0          1 4294954951 4294954951
WesObs
4feed660d3728526797edeb4f0467384?d=identicon&s=25 Bill Kelly (Guest)
on 2006-05-03 18:56
(Received via mailing list)
From: "bostonmacosx" <tcdevlin@bc.edu>
>
> Sorry for being such a NEWB. I'm a pretty prolific PHP writer however
> I'm new to RUBY and want to expand my horizons.
>
> I have a big-endian binary file and want to convert it to ascii so I see
> what's in it.
> Is there an easy way to accomplish this?

Not sure what you mean.  Do you want an ASCII hex dump of the
file?  (like `od -x`)  Or just strip the binary characters, and print
any
ASCII sequences found (like `strings`) ?


Regards,

Bill
4feed660d3728526797edeb4f0467384?d=identicon&s=25 Bill Kelly (Guest)
on 2006-05-03 18:58
(Received via mailing list)
From: "bostonmacosx" <tcdevlin@bc.edu>
> 000461b0  40 40 00 00 40 00 00 00  40 00 00 00 40 40 00 00
> |?...@...@...@...|
> 00046210  40 c0 00 00 40 a0 00 00  40 a0 00 00 40 80 00 00
> |@...@...@...@...|
> 00046220  40 40 00 00 40 40 00 00  40 40 00 00 40 40 00 00
> |@@..@@..@@..@@..|
>
> Really means nothing to me. What I'm looking to find is turning the
> whole thing into a human readable ASCII format.

But... there's no ASCII in that data.  Except for some @ signs and
a few ?-marks.

If you want to _scan_ for ascii strings in a file, try the `strings`
command
from the command line.  But, if your whole file contains data like the
above, then it's in your best interest to learn to read a hex dump.
It's already showing you all the ASCII you can get.



Regards,

Bill
66247570dbe0c350a165cdabc2769ba1?d=identicon&s=25 Paul Robinson (Guest)
on 2006-05-03 18:58
(Received via mailing list)
On 1 May 2006, at 17:58, bostonmacosx wrote:

> Really means nothing to me.

That output is perfectly acceptable hex/ASCII representation of the
executable code. What exactly were you expecting?

> What I'm looking to find is turning the
> whole thing into a human readable ASCII format.

That makes no sense. Do you mean:

a) You want to reduce the executable code to 7-bit ASCII so you can
look at it? You've just done that.
b) You want to extract the text that is used within the program to
print out messages to stdout and other places? strings has already
been suggested.
c) You want to reverse-engineer the code back into Ruby so that you
can pick through it and change it?

a) and b) are possible and people have told you how to to do it. c)
is not. Well, not easily anyway.

ASCII is a way to encode characters in binary not a 'file format' as
you seem to be suggesting, so you need to be more explicit about what
you want, why you want it, what you're going to do once you've got
it, and then perhaps somebody can help. Until then, all you've done
is confuse everybody. :-)
66247570dbe0c350a165cdabc2769ba1?d=identicon&s=25 Paul Robinson (Guest)
on 2006-05-03 18:58
(Received via mailing list)
On 1 May 2006, at 21:53, bostonmacosx wrote:

> mydata = String.new
> f = File.open("WesObs_Sumatra_032805.sac","rb")

What? You tell us you have a 'binary file' that you need to convert
into 'plain ASCII' and then don't tell us the basic information we
need and complain that the data you're getting means nothing to you...

If you'd said in the first place 'I have an Adobe Shared Asset
Catalog (.sac) format file that I'd like to process by getting [data
x] out of it' then maybe somebody might have been able to help you.

We're really cool guys, but we're not able to read minds (yet).

> Ok here is what I have.
> It works however I need to do two things.

Which two things? What two things do you need to do?
0904ced682c71189fc0763daf24ea07f?d=identicon&s=25 bostonmacosx (Guest)
on 2006-05-03 21:14
HI all and thanks for the help.
After beating around a little.

mydata = String.new
f = File.open("WesObs_Sumatra_032805.sac","rb")
newstring =  f.read

newdata = newstring.unpack('f*\n')
fw = File.open("test.txt","wb")
fw.write(newdata[0])

Is all I needed to do to extract the floats from the binary data in the
file.
Was a lot simpler then I though.
I didn't know that unpack stuck things in arrays so that was the largest
problem I was having.

Onto problem #2.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-04 03:26
(Received via mailing list)
On May 3, 2006, at 3:15 PM, bostonmacosx wrote:

>
>
>
>
> --
> Posted via http://www.ruby-forum.com/.
>

Please don't forget to close your files. Better than that, use the
block form which takes care of closing the file for you.

newstring = nil

File.open("WesObs_Sumatra_032805.sac","rb") do |f|
   newstring = f.read
end # file is automatically closed at the end of this block, even if
an exception is raised


also I believe the "\n" in the unpack string is both unnecessary and
meaningless.
This topic is locked and can not be replied to.