Base34 编码器/解码器指南

发布于 2024-08-17 15:51:03 字数 67 浏览 2 评论 0原文

有没有人有 Guid to Base34 编码器/解码器的好代码片段,我之前用谷歌搜索过它,但从未真正找到任何好的来源。

Does anyone have a nice code snippet for a Guid to Base34 encoder/decoder, I've googled around for it previously and never really found any good sources.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

脸赞 2024-08-24 15:51:03

这个 C# 中的数字基数转换类 可以相当容易地扩展为执行 base34 (或其他,如果你认为人们会混淆 S 和 5 或 b 和 6 或 i 和 j 或 B 和 8 或 9 和 g 或其他)

This Number base conversion class in C# could be fairly easily extended to do base34 (or others if you think people will confuse S and 5 or b and 6 or i and j or B and 8 or 9 and g or whatever)

喜爱纠缠 2024-08-24 15:51:03

这是一个简化版本...它基本上需要一个字符串,计算 MD5 哈希值,提取前四个字节作为无符号长整型(有效地将字符串映射到 4 字节数字),将其转换为 base36,然后交换出“ “X”和“Y”的“哦”和“零”字符。然后,它确保最终字符串只有六个字符,如果需要则用“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)

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". Then, it ensures the final string is only six chars, padding with "Z" chars if needed.

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)
戒ㄋ 2024-08-24 15:51:03

这里的关键方法是 ToByteArray这个特定的构造函数

编码:

string encodedGuid = Convert.ToBase64String(guid.ToByteArray());

解码:

Guid guid = new Guid(Convert.FromBase64String(encodedGuid));

The key methods here are ToByteArray and this particular constructor.

Encode:

string encodedGuid = Convert.ToBase64String(guid.ToByteArray());

Decode:

Guid guid = new Guid(Convert.FromBase64String(encodedGuid));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文