Method: String#unpack

Defined in:
lib/dicom/ruby_extensions.rb

#unpack(format) ⇒ Array<String, Integer, Float>

Redefines the core library unpack method, adding the ability to decode signed integers in big endian.

Parameters:

  • format (String)

    a format string which decides the decoding scheme to use

Returns:

  • (Array<String, Integer, Float>)

    the decoded values



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dicom/ruby_extensions.rb', line 138

def unpack(format)
  # Check for some custom unpack strings that we've invented:
  case format
    when "k*" # SS
      # Unpack BE US, repack LE US, then finally unpack LE SS:
      wrongly_unpacked = self.__original_unpack__('n*')
      repacked = wrongly_unpacked.__original_pack__('S*')
      correct = repacked.__original_unpack__('s*')
    when 'r*' # SL
      # Unpack BE UL, repack LE UL, then finally unpack LE SL:
      wrongly_unpacked = self.__original_unpack__('N*')
      repacked = wrongly_unpacked.__original_pack__('I*')
      correct = repacked.__original_unpack__('l*')
    else
      # Call the original method for all other (normal) cases:
      self.__original_unpack__(format)
  end
end