Folks, in the interest of a smaller example, and to show how one does a
complete round-trip of float array <--> byte encoded string <-->
base64binary, here is a irb (interactive ruby) session showing the steps and
fully commented:
# require the base64 encoding library
irb(main):002:0> require 'base64'
=> true
# create the array of floats
irb(main):003:0> a = [123.45, 124.5634, 1234.34121]
=> [123.45, 124.5634, 1234.34121]
# Encode the array as double precision (e.g. 64 bit) floats as a byte string
# in little-endian order. A lowercase "e" would encode it as single precision
# floats (32-bit).
# "G" and "g" would correspond to double and single precision in big-endian
# byte order. See the ruby API for the pack and unpack methods for more info
# on the types of byte encoding and what the "*" actually means ;)
irb(main):004:0> s = a.pack('E*')
=> "\315\314\314\314\314\334^@@\244\337\276\016$_@F|'f]I\223@"
# encode in base64
irb(main):006:0> sb64 = Base64.encode64(s)
=> "zczMzMzcXkBApN++DiRfQEZ8J2ZdSZNA\n"
# decode to byte string again
irb(main):007:0> s2 = Base64.decode64(sb64)
=> "\315\314\314\314\314\334^@@\244\337\276\016$_@F|'f]I\223@"
# unpack the byte string into float array
irb(main):008:0> a2 = s2.unpack('E*')
=> [123.45, 124.5634, 1234.34121]
And that is the long and short of it. Cheers!
-angel
On Wednesday 13 September 2006 11:25, Angel Pizarro wrote:
> Hello all,
>
> In the hopes of fostering mzData as a format, I am putting into the
> docstore an example of decoding the mzData base64binary float arrays for
> m/z and intensity using Ruby, my new language of choice.
>
> The docstore path is Documents/PSI_MS/mzData/decode_base64.rb
> Here is the URL:
>
> http://psidev.sourceforge.net/docstore/view.php?sess=0&parent=7&expand=1&or
>der=name&sortname=ASC&id=104&action=file_details
>
> The code and comments should be self-explanatory, but if not, give me an
> email and I will be happy to augment the file, Look for a Ruby API to
> read/write mzData sometime soon! ( airware at the moment )
>
> -angel
--
Angel Pizarro
Director, Bioinformatics Facility
Institute for Translational Medicine and Therapeutics
University of Pennsylvania
806 BRB II/III
421 Curie Blvd.
Philadelphia, PA 19104-6160
P: 215-573-3736
F: 215-573-9004
E: an...@ma...
|