Created
July 23, 2016 18:04
-
-
Save jharley/2125f0d9e4c1ef7624a46b8741240d28 to your computer and use it in GitHub Desktop.
Convert Oracle's RAW(16) type to a GUID w/Ruby 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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