BASIC 中的任何 Base64 解码算法

发布于 2024-09-24 11:53:32 字数 93 浏览 1 评论 0原文

我正在用 BASIC 编程一个小型微控制器。基本上它收到了一些 BASE64 编码的数据,我需要在最后使用 BASIC 对其进行解码。我想知道是否有什么办法可以做到这一点?

I am programming a small micro controller in BASIC. Basically it received some BASE64 encoded data and I need to decode it at my end using BASIC. I was wondering if there is any way to do that?

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

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

发布评论

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

评论(1

萌无敌 2024-10-01 11:53:32

我不知道基本的,但你可以从这个链接开始: http://en.wikipedia.org/wiki /Base64 清楚地解释了编码的工作原理。

您需要解码 Base64 编码的消息,因此您应该
设置一个类似

dim('A','B','C','D','E',....,'8','9','+','/')

(或无论基本语法是什么)的数组,索引为 64 并
从输入中读取 4 个字符。
到查找表中找到对应的值并使用
它来解码消息。

我将使用术语char来表示
您在编码消息中找到的字符以及术语索引
地址它们代表解码算法的值。

如果您有基本的支持位掩码,您可以很快完成。
如果没有,你必须使用一些巧妙的乘法/除法。

  • 乘以第一个索引并乘以 4(模 255),
    将第二个索引除以 16,
    将两个结果相加以获得第一个字节

  • 将第二个索引乘以 16(模 255),
    将第三个索引除以 4,
    将两个结果相加得到第二个字节,

  • 将第三个索引乘以 16(模 255),
    添加第四个索引以获得第三个字节

重复,直到消息结束。

您需要注意填充字符:如果您遇到 = 符号,您就知道您到达了编码字节的末尾,并且您必须停止解码并知道:

  • 如果第三个编码字符是 = 您必须仅解码第一个字节;
  • 如果第四个字符是a =,则需要解码两个字节;

没有其他情况了。

另一个提示:有时编码消息被包装,您需要忽略 \n 字符(或您可能遇到的 \r\n 的任何组合)。

I don't know basic but you can start with this link: http://en.wikipedia.org/wiki/Base64 with a clear explanation of how the encoding works.

You need to decode a base64 encoded message so you should
setup an array like

dim('A','B','C','D','E',....,'8','9','+','/')

(or wathever the basic syntax is) with the 64 index and
read 4 chars from input.
Find the corresponding value into the lookup table and use
it to decode the message.

I will use the term char to indicate
the chars you find into the encoded message and the term index to
address the value they represent for the decoding algorithm.

If your basic support bitmask you can do it very quickly.
if not, you have to use some clever multiplication/division.

  • multiply the first index and multiply it for 4 (modulus 255),
    divide the second index for 16,
    add the two result to obtain the first byte

  • multiply the second index for 16 (modulus 255),
    divide the third index for 4,
    add the two result to obtain the second byte,

  • multiply the third index for 16 (modulus 255),
    add the fourth index to obtain the third byte

repeat until the end of the message.

You need to pay attention the the padding characters: If you encounter an = sign you know that you hit the end of the encoded bytes and you have to stop the decoding knowing that:

  • if the third encoded char is a = you have to decode only the first byte;
  • if the fourth char is a = you need to decode two byte;

There aren't other cases.

Just another hint: sometimes the encoded messages is wrapped, you need to ignore the \n chars (or wathever combination of \r\n you can encounter).

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