Skip to content

Instantly share code, notes, and snippets.

@jharley
Created July 23, 2016 18:04
Show Gist options
  • Save jharley/2125f0d9e4c1ef7624a46b8741240d28 to your computer and use it in GitHub Desktop.
Save jharley/2125f0d9e4c1ef7624a46b8741240d28 to your computer and use it in GitHub Desktop.
Convert Oracle's RAW(16) type to a GUID w/Ruby 2
def is_guid?(guid)
standard_guid = /([A-F\d]{8})-([A-F\d]{4})-([A-F\d]{4})-([A-F\d]{4})-([A-F\d]{12})/i
(guid =~ standard_guid)
end
def to_byte_array(hex)
hex.unpack('H*').first.scan(/[A-F\d]{4}/i)
end
def format_guid(string)
guid_format = /([A-F\d]{8})([A-F\d]{4})([A-F\d]{4})([A-F\d]{4})([A-F\d]{12})/i
string.scan(guid_format).join('-').downcase
end
def raw_to_guid(raw)
# byte orders are flipped due to Endianness
oracle_raw_idx = [3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15]
hex_bytes = to_byte_array(raw)
hex_guid = oracle_raw_idx.map { |i| hex_bytes[i] }
format_guid(hex_guid.join.split('!').pack('H*'))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment