如何使用Ruby中的自定义字符集将UUID转换为字符串? [英] How can I convert a UUID to a string using a custom character set in Ruby?

查看:313
本文介绍了如何使用Ruby中的自定义字符集将UUID转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据此处的规范创建一个有效的IFC GUID(IfcGloballyUniqueId): http://www.buildingsmart-tech. org/ifc/IFC2x3/TC1/html/ifcutilityresource/lexical/ifcgloballyuniqueid.htm

I want to create a valid IFC GUID (IfcGloballyUniqueId) according to the specification here: http://www.buildingsmart-tech.org/ifc/IFC2x3/TC1/html/ifcutilityresource/lexical/ifcgloballyuniqueid.htm

它基本上是一个UUID或GUID(128位),映射到一组22个字符,以限制文本文件中的存储空间.

It's basically a UUID or GUID (128 bit) mapped to a set of 22 characters to limit storage space in a text file.

我目前有此解决方法,但这只是一个近似值:

I currently have this workaround, but it's merely an approximation:

guid = '';22.times{|i|guid<<'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$'[rand(64)]}

似乎最好使用ruby SecureRandom生成128位UUID,如本示例中所示( https://ruby-doc.org/stdlib-2.3.0/libdoc/securerandom/rdoc/SecureRandom.html ):

It seems best to use ruby SecureRandom to generate a 128 bit UUID, like in this example (https://ruby-doc.org/stdlib-2.3.0/libdoc/securerandom/rdoc/SecureRandom.html):

SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"

此UUID需要根据以下格式映射到长度为22个字符的字符串:

This UUID needs to be mapped to a string with a length of 22 characters according to this format:

           1         2         3         4         5         6 
 0123456789012345678901234567890123456789012345678901234567890123
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$";

我不太了解这一点. 是否应将32个字符的长十六进制数转换为128个字符的长二进制数,然后划分为22组6位(除了获得剩余2位的一位以外?),因此可以将每组转换为十进制数从0到64?然后可以用转换表中的相应字符替换哪个字符?

I don't understand this exactly. Should the 32-character long hex-number be converted to a 128-character long binary number, then devided in 22 sets of 6 bits(except for one that gets the remaining 2 bits?) for which each can be converted to a decimal number from 0 to 64? Which then in turn can be replaced by the corresponding character from the conversion table?

我希望有人可以验证我是否在正确的轨道上.

I hope someone can verify if I'm on the right track here.

如果我愿意,那么在Ruby中是否有比使用所有这些单独的转换更快的计算方式来将128位数字转换为22组0-64?

And if I am, is there a computational faster way in Ruby to convert the 128 bit number to the 22 sets of 0-64 than using all these separate conversions?

编辑:对于有相同问题的任何人,这是我目前的解决方案:

Edit: For anyone having the same problem, this is my solution for now:

require 'securerandom'

# possible characters in GUID
guid64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$'
guid = ""

# SecureRandom.uuid: creates a 128 bit UUID hex string
# tr('-', ''): removes the dashes from the hex string
# pack('H*'): converts the hex string to a binary number (high nibble first) (?) is this correct?
#   This reverses the number so we end up with the leftover bit on the end, which helps with chopping the sting into pieces.
#   It needs to be reversed again to end up with a string in the original order.
# unpack('b*'): converts the binary number to a bit string (128 0's and 1's) and places it into an array
# [0]: gets the first (and only) value from the array
# to_s.scan(/.{1,6}/m): chops the string into pieces 6 characters(bits) with the leftover on the end.

[SecureRandom.uuid.tr('-', '')].pack('H*').unpack('b*')[0].to_s.scan(/.{1,6}/m).each do |num|

  # take the number (0 - 63) and find the matching character in guid64, add the found character to the guid string
  guid << guid64[num.to_i(2)]
end
guid.reverse

推荐答案

Base64编码与您在此处想要的非常接近,但是映射是不同的.没什么大不了的,您可以解决此问题:

Base64 encoding is pretty close to what you want here, but the mappings are different. No big deal, you can fix that:

require 'securerandom'
require 'base64'

# Define the two mappings here, side-by-side
BASE64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
IFCB64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$'

def ifcb64(hex)
  # Convert from hex to binary, then from binary to Base64
  # Trim off the == padding, then convert mappings with `tr`
  Base64.encode64([ hex.tr('-', '') ].pack('H*')).gsub(/\=*\n/, '').tr(BASE64, IFCB64)
end

ifcb64(SecureRandom.uuid)
# => "fa9P7E3qJEc1tPxgUuPZHm"

这篇关于如何使用Ruby中的自定义字符集将UUID转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆