finding a tag in a binary file

R

rob s.

Hi I'm a complete non programmer but willing to give it a try.

I have a DICOMDIR (medical imaging file) which has various tags in,
following a tag ie 0010, 0010 is the "patient name", "study date"
0008,0020 is another.

I have several DICOMDIR and I would like to extract just a few bits of
information from them to a .txt file, the files average size is about
343 KB as its full of other information as well which I don't need. Each
file has amongst other things a list of about 10 names, scan dates etc.

So far I've managed to read the file into an array using

contentsArray=[] #open new array empty

open('1DICOMDIR', 'rb') { |f| f.each_byte { |f| contentsArray.push f } }

This gets the info into an array

although its a hex file it displays the contents in decimal, but they
correspond to the hex codes I've seen in a hex editor...

My problem is I don't know how to detect 0010,0010 ? then extract the
name. Can anyone help, I've looked all over the web but not finding
anything to help me much. Thanks a lot
 
D

David Jacobs

[Note: parts of this message were removed to make it a legal post.]

Hi Rob,

I don't usually work with binary files, but I would imagine you could do
something like this (untested):

search_byte = 0b00100010
contents = File.binread('1DICOMDIR').bytes.to_a
index = contents.index(search_byte)

# I assume you want the next entry after
name = contents[index.next]

Then simply decode the name however is appropriate.

David
 
R

rob s.

thanks for the replies looks like I don't have
contents = File.binread('1DICOMDIR').bytes.to_a
undefined method `binread' for File:Class
I have ruby 1.8.7

I'll have a play with the code you suggested tomorrow.
 
R

rob s.

Hi David, sorry that does not work either, not on my system anyway.
looks like a nice way of doing it though if I can get the code to run
 
D

David Jacobs

[Note: parts of this message were removed to make it a legal post.]

I forgot to mention, it's 1.9 only.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top