[英]Guid to Base34 encoder/decoder
有没有人对Guid to Base34编码器/解码器有一个不错的代码段,我以前在Google上搜索过它,却从未真正找到任何好的资源。
C#中的此Number基数转换类可以很容易地扩展为base34(或者如果您认为人们会混淆S和5或b和6或i和j或B以及8或9和g或其他东西,则可以进行其他转换)
Here's a simplified version... It basically takes a string, calculates the MD5 hash, extracts the first four bytes as an unsigned long (effective mapping the string to a 4-byte number), converts that to base36 and then swaps out the "oh" and "zero" chars for "X" and "Y". 这是一个简化的版本...它基本上需要一个字符串,计算MD5哈希值,将前四个字节提取为无符号长整数(有效地将字符串映射为4字节数字),将其转换为base36,然后换成“哦”和“零”字符代表“ X”和“ Y”。 Then, it ensures the final string is only six chars, padding with "Z" chars if needed. 然后,确保最终的字符串只有6个字符,如果需要,用“ Z”字符填充。
require 'digest/md5' # create an easy-to-read 6-digit unique idno idno = original # starting string idno = Digest::MD5.digest(idno).unpack("N").first # digest as unsigned long idno = idno.to_s(36).upcase.tr("0O","XY") # convert to base34 (no "oh" or "zero") idno = idno[0,6].ljust(6,"Z") # final 6-digit unique idno (pad with "Z" chars)
The key methods here are ToByteArray and this particular constructor . 这里的关键方法是ToByteArray和这个特定的构造函数 。
Encode: 编码:
string encodedGuid = Convert.ToBase64String(guid.ToByteArray());
Decode: 解码:
Guid guid = new Guid(Convert.FromBase64String(encodedGuid));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.