字的数组为String [英] Word Array to String

查看:220
本文介绍了字的数组为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何做到这一点的Javascript或Jquery的?

请在2个步骤提示:

1:字数组为单字节数组。

2:字节数组字符串。

这也许可以帮助:

 函数hex2a(十六进制){
    无功海峡='';
    对于(VAR I = 0; I< hex.length;我+ = 2)
        STR + = String.fromChar code(parseInt函数(hex.substr(1,2),16));
    返回海峡;
}


解决方案

你所要实现的目标已经在CryptoJS实施。从文档


  

您可以将WordArray对象通过显式调用toString方法,并通过一个连接codeR转换为其他格式。


  VAR哈希= CryptoJS.SHA256(信息);
警报(hash.toString(CryptoJS.enc.Base64));
警报(hash.toString(CryptoJS.enc.Hex));

结果
老实说,我不知道你为什么要实施自己...但是,如果你确实需要做手动,在你所提到的2个步骤,你可以尝试这样的事:

 函数wordToByteArray(wordArray){
    VAR的字节数组= [],一句话,I,J;
    对于(i = 0; I< wordArray.length ++我){
        字= wordArray [I]
        为(J = 3; J> = 0; --j){
            byteArray.push((词GT;→8 * j)条和放大器;为0xFF);
        }
    }
    返回的字节数组;
}功能byteArrayToString(字节阵列){
    无功海峡=,我;
    对于(i = 0; I< byteArray.length ++我){
        STR + =逃生(String.fromChar code(的字节数组[我]));
    }
    返回海峡;
}VAR哈希= CryptoJS.SHA256(信息);
VAR的字节数组= wordToByteArray(hash.words);
警报(byteArrayToString(字节阵列));

wordToByteArray 函数应该很好地工作,但要注意, byteArrayToString 将产生几乎在任何情况下,奇怪的结果。我不知道很多关于编码,但ASCII只使用了7位,这样试图连接code中的整个字节,当你不会得到ASCII字符。所以我加了逃跑函数至少能够显示所有这些奇怪的字符你可能会得到。 ;)

我建议你使用CryptoJS已经实现的功能,或只使用字节数组(无需将其转换为字符串),为您分析。

how to do this in Javascript or Jquery?

Please suggest in 2 steps:

1.- Word Array to Single Byte Array.

2.- Byte Array to String.

Maybe this can help:

function hex2a(hex) {
    var str = '';
    for (var i = 0; i < hex.length; i += 2)
        str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    return str;
}

解决方案

What you are trying to achieve is already implemented in CryptoJS. From the documentation:

You can convert a WordArray object to other formats by explicitly calling the toString method and passing an encoder.

var hash = CryptoJS.SHA256("Message");
alert(hash.toString(CryptoJS.enc.Base64));
alert(hash.toString(CryptoJS.enc.Hex));


Honestly I have no idea why you want to implement that yourself... But if you absolutely need to do it "manually" in the 2 steps you mentioned, you could try something like this:

function wordToByteArray(wordArray) {
    var byteArray = [], word, i, j;
    for (i = 0; i < wordArray.length; ++i) {
        word = wordArray[i];
        for (j = 3; j >= 0; --j) {
            byteArray.push((word >> 8 * j) & 0xFF);
        }
    }
    return byteArray;
}

function byteArrayToString(byteArray) {
    var str = "", i;
    for (i = 0; i < byteArray.length; ++i) {
        str += escape(String.fromCharCode(byteArray[i]));
    }
    return str;
}

var hash = CryptoJS.SHA256("Message");
var byteArray = wordToByteArray(hash.words);
alert(byteArrayToString(byteArray));

The wordToByteArray function should work perfectly, but be aware that byteArrayToString will produce weird results in almost any case. I don't know much about encodings, but ASCII only uses 7 bits so you won't get ASCII chars when trying to encode an entire byte. So I added the escape function to at least be able to display all those strange chars you might get. ;)

I'd recommend you use the functions CryptoJS has already implemented or just use the byte array (without converting it to string) for your analysis.

这篇关于字的数组为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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