简体   繁体   English

在JavaScript中将字符串解码为数组值

[英]Decoding string to array values in javascript

I have 2 strings with some chars. 我有2个字符串和一些字符。 One of them is with "mashed" characters, and the other one is with ordered characters which have some sense. 其中一个带有“混搭”字符,另一个带有有一定意义的有序字符。 For example: 例如:

wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!
Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT

So as you may see - the first one have equivalent of symbols which are the same for the equal symbol in the second one. 如您所见-第一个具有与第二个中的相等符号相同的符号。 And the task is - to create somehow kind of alphabet from them, because I have third one string wich have to be "decoded". 而任务是-从其中创建某种字母,因为我有第三个必须“解码”的字符串。 (wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!). (wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f / -G / N-.f7jN:MC:ifDCGN7tn!)。 And the tricky part here is - that if I'm pretty sure for the first two strings that they're absolutely equal like a number of chars, so about the new one - is completely different number of symbols, but they consisting in the "alphabet" 而这里最棘手的部分是-如果我很确定前两个字符串的绝对值与多个字符完全相同,那么对于新的字符来说-的符号数量则完全不同,但它们包含在“字母”

And here is my current code for creation of the "alphabet": 这是我当前用于创建“字母”的代码:

var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!";

var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT";

var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!";

var myenc = {};

var mynewenc = {};

for (i = 0; i < enc.length; i+=1) {
var encoded = new Array(enc[i]);
var decoded = new Array(dec[i]);


myenc[enc[i]] = dec[i];
};

console.log(myenc);

And now - how I have to decode, the new one string, using this "alphabet"? 现在-我必须如何使用这个“字母”解码新的一个字符串?

Here's one way that you can approach it (as I understand the question): 这是解决问题的一种方法(据我了解的问题):

// Create your dictionary
var dict = {};

var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!".split('');
var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT".split('');

// Populate your dictionary
for (var i = 0; i < enc.length; i++) {
  dict[enc[i]] = dec[i];
}

// You can use your dictionary like this
var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!".split('');

// Returns your translated string
newenc.map(function(e) {
  return dict[e];
}).join('');

However for this method you'll have to deal with characters that are defined in newenc that are not defined in your enc (such as T ). 但是,对于这种方法,您必须处理newenc中定义的字符,这些字符在enc未定义(例如T )。 I tried to do the best I could given the situation and rules that you've described, hope this helps! 考虑到您所描述的情况和规则,我已尽力做到最好,希望这会有所帮助!

If I understand well, you can try using this code. 如果我很了解,您可以尝试使用此代码。 It is finding the appropriate encoded letter in your enc variable and if the letter is found, it is replacing it with the corresponding letter from your dec variable. 它会在enc变量中找到适当的编码字母,如果找到该字母,则将其替换为dec变量中的相应字母。

var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!";

var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT";

var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!";

for (i = 0; i < newenc.length; i++) {
    for (j = 0; j < enc.length; j++) {
        if (enc[j] == newenc[i])
            newenc[i] = dec[j];
    }
};

console.log(newenc);

At the end your variable newenc may contain your decoded string. 最后,变量newenc可能包含解码后的字符串。

var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!";
var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT";

var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!";

function make_dictionary(enc, dec){
    o = new Object();
    if(enc.length == dec.length){
        for(i=0; i<enc.length; i++){
            o[enc[i]] = dec[i];
        }
    }
    else{
        console.log('error');
    }
    return o;
}

function translate(newenc, dictionary, fill){
    var newstring = '';
    for(i=0; i<newenc.length; i++){
        if(typeof dictionary[newenc[i]] !== 'undefined'){
            newstring += dictionary[newenc[i]];
        }
        else{
            newstring += fill;
        }

    }
    return newstring;
}

var d = make_dictionary(enc, dec);
console.log(d);
var string = translate(enc, d, '_');
console.log(string);
var string = translate(newenc, d, '_');
console.log(string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM