From: niklas | brueckenschlaeger Date: 2011-02-24T07:07:14+09:00 Subject: Re: finding a tag in a binary file --=-KYSImaEbF42GhG7RKYIE Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Example: byte =3D 0b00100010 # clear the trailing 4 bit, then shift: first_four_bits =3D (byte & 0b11110000) >> 4 # clear the leading 4 bits: last_four_bits =3D byte & 0b1111 btw., your code would be shorter as: open('1DICOMDIR', 'rb') { |f| contentsArray =3D f.each_byte.to_a } (also it's not so common to use camelCase in the ruby world, we usually prefer under_scored_identifiers, butThatsUpToYou...) By default ruby will print numbers as decimal, however you can look at them in other ways by using to_s and passing in a base: 123.to_s(16) #=3D> "7b" 123.to_s(2) #=3D> "1111011" hope this helps. -- niklas On Thu, 2011-02-24 at 06:28 +0900, rob s. wrote: > Hi I'm a complete non programmer but willing to give it a try. >=20 > 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. >=20 > 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. >=20 > So far I've managed to read the file into an array using >=20 > contentsArray=3D[] #open new array empty >=20 > open('1DICOMDIR', 'rb') { |f| f.each_byte { |f| contentsArray.push f } } >=20 > This gets the info into an array >=20 > 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... >=20 > 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 >=20 --=-KYSImaEbF42GhG7RKYIE Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iQIcBAABAgAGBQJNZYULAAoJEIQdQGZOjo82Co4P/2rraHiaJfxKleGIsBAeXRV3 KKb3pVnq8J4w5ewY1JvKnW1jYHfjhnSk7f+0DJh9AyNhX123hShmw32aUEZ+yOSW oXhFXIUh/ddUpHX5tJT5HPX8AGic609FKGEyxhA9Qv6xDQJVmeclG9i6ryu+FNCx CRqTH7AUqNdYgxQPR+LMjr9x/VWAz8nq8M8HcLVsCLEGHhImedyuNEh0hqwZmtUZ jlm8nr9Y2NXMyBsH5IMchSMERKWmgOcFSOMgmiL5VrYC1tEl7rmtxGAzZJKNnzSP PcSMk1GTn48F/J2Mpx2XbBunrcliOhiZLmbPwGUL0+S2xQUq5FKOYg6SB6C9pAED //hCwMgRcUivZRIIZz0EfbCNHPKlpweknkpTUzmevzjf8P3rTEUC2eKjOOToXS0x odyZiE6nloKv4pp8RvO7eJRTGz1vp4R5xg+c2bO9xQOFKmbtBk+hSieAZL2On17k ijRz/NPimxMg9P/DQea3RLNnRQn1uuvjLy5QZ/UkorA0UTdlX8Ci/BOohU3+DWh0 qQu68ndKMVrEjNM8pGuLTEPsKbUA/y7mduZpU+IsUCozJ977s6RQ0kqpX038qRcb J03Wily+vjCWIkuSKAapJY6SnaFIIrKw3oywUT8KWGi7DaaaB2O4LQsy57iVBpnw ekOfJ6BEuOyMZTGdus2P =Be+1 -----END PGP SIGNATURE----- --=-KYSImaEbF42GhG7RKYIE--