如何在 Javascript 中对数据(不是字符串)进行 Base64 编码?

发布于 2024-11-18 19:03:00 字数 954 浏览 5 评论 0原文

我正在将功能从 Objective-C iPhone 应用程序移植到 Javascript iPhone 应用程序 (Appcelerator Titanium)。在 Objective-C 中,我有一个 NSData 对象来表示这个标记:

//NSData object printed to the console:
<0cd9f571 b0e66e6d ca410d12 f67a404a 7e64b9b5 d2483fd9 63a9267b 1c7609e2>

它不是一个字符串,它是一个 NSData 对象——字节缓冲区的面向对象的包装器。当我对对象进行 Base64 编码时,我得到以下结果:

//base64 encoded NSData object
DNn1cbDmbm3KQQ0S9npASn5kubXSSD/ZY6kmexx2CeI=

在我的 javascript 实现中,我有相同标记的字符串表示形式。它看起来像这样:

//string version of the token in my javascript implementation
0cd9f571b0e66e6dca410d12f67a404a7e64b9b5d2483fd963a9267b1c7609e2

当我在 javascript 中对字符串对象进行 Base64 编码时,我得到以下结果:

//base64 encoded token (string) in javascript
MGNkOWY1NzFiMGU2NmU2ZGNhNDEwZDEyZjY3YTQwNGE3ZTY0YjliNWQyNDgzZmQ5NjNhOTI2N2IxYzc2MDllMg==

问题是,我发布到的 Web 服务不需要 Base64 编码的字符串,它需要 Base64 编码的数据!我怎样才能在 JavaScript 中做到这一点?

I'm porting functionality from an Objective-C iPhone app to a Javascript iPhone app (Appcelerator Titanium). In Objective-C I have an NSData object that represents this token:

//NSData object printed to the console:
<0cd9f571 b0e66e6d ca410d12 f67a404a 7e64b9b5 d2483fd9 63a9267b 1c7609e2>

It's not a string, it's an NSData object -- an object-oriented wrapper for a byte buffer. When I base64 encode the object, I get this result:

//base64 encoded NSData object
DNn1cbDmbm3KQQ0S9npASn5kubXSSD/ZY6kmexx2CeI=

In my javascript implementation, I have a string representation of the same token. It looks like this:

//string version of the token in my javascript implementation
0cd9f571b0e66e6dca410d12f67a404a7e64b9b5d2483fd963a9267b1c7609e2

When I base64 encode the string object in javascript, I get this result:

//base64 encoded token (string) in javascript
MGNkOWY1NzFiMGU2NmU2ZGNhNDEwZDEyZjY3YTQwNGE3ZTY0YjliNWQyNDgzZmQ5NjNhOTI2N2IxYzc2MDllMg==

The problem is, the web service I'm posting to doesn't want the base64 encoded string, it wants the base64 encoded data! How can I do this in javascript?

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

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

发布评论

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

评论(3

谈场末日恋爱 2024-11-25 19:03:00

在进行 Base64 编码之前转换十六进制字符串应该可以解决该问题。要在 JS 中执行此操作:

if (! Array.prototype.map) {
    Array.prototype.map = function(f) {
        var result = [];
        for (var i=0; i < this.length; ++i) {
            result[i] = f(this[i], i);
        }
        return result;
    }
}
String.prototype.b16decode = function() {
    return this.match(/../g).map(
        function (x) {
            return String.fromCharCode(parseInt(x, 16));
        }
    ).join('');
}

例如,运行

btoa('0cd9f571b0e66e6dca410d12f67a404a7e64b9b5d2483fd963a9267b1c7609e2'.b16decode())

(where btoa< /a> 是一些浏览器提供的 base64 编码函数)导致

"DNn1cbDmbm3KQQ0S9npASn5kubXSSD/ZY6kmexx2CeI="

Convert the hexadecimal string before base64 encoding it should fix the issue. To do this in JS:

if (! Array.prototype.map) {
    Array.prototype.map = function(f) {
        var result = [];
        for (var i=0; i < this.length; ++i) {
            result[i] = f(this[i], i);
        }
        return result;
    }
}
String.prototype.b16decode = function() {
    return this.match(/../g).map(
        function (x) {
            return String.fromCharCode(parseInt(x, 16));
        }
    ).join('');
}

For example, running

btoa('0cd9f571b0e66e6dca410d12f67a404a7e64b9b5d2483fd963a9267b1c7609e2'.b16decode())

(where btoa is a base64 encoding function some browsers supply) results in

"DNn1cbDmbm3KQQ0S9npASn5kubXSSD/ZY6kmexx2CeI="
莫多说 2024-11-25 19:03:00

您需要将原始十六进制字符串作为整数列表读取。请参阅如何在 JavaScript 中将十进制转换为十六进制?

然后你需要修改众多 base64 js 算法之一以使用整数而不是 charCodeAt。请参阅http://www.webtoolkit.info/javascript-base64.html
另请参阅 http://www.codeproject.com/KB/cs/base64encoder.aspx< /a> 这是一个直接整数到 Base64 算法(大多数是字节或字符串到 Base64)。

即使最后一个示例是 C#,您也应该能够将其转换为 JS。

如果以后有时间我会看看是否可以编写代码。

You need to read the original hex string as list of integers. See How to convert decimal to hex in JavaScript?

Then you need to modify one of the many base64 js algorithms to use the integers instead of charCodeAt. See http://www.webtoolkit.info/javascript-base64.html
Also see http://www.codeproject.com/KB/cs/base64encoder.aspx which is a direct integer to base64 algo (most are byte or string to base64).

Even though that last example C# you should be able to convert it to JS.

If I have the time later on I will see if I can write the code.

莫多说 2024-11-25 19:03:00

搜索 toDataUrl() 函数

search for toDataUrl() function

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