简体   繁体   中英

Decode string of decimal ASCII codes to String

I have a problem, I have a string with decimal ascii codes '729799107101114328297110107' , I need to decode it (between 2 or 3 digits), for example, the letter 'a' is the number 97 ascii, so it would be the second letter of this string and 'k' is 107, the fourth letter in the string. But I don't know how to 'separate' this entire string to remove the corresponding ascii numbers.

There is a website that already does this, but, I need to implement it in javascript

We could use a reduce-like approach, iterating through the string and appending each valid char as we find it:

 function encodeDecimalString(input) { return Array.prototype.reduce.call(input, (acc, c) => acc + c.charCodeAt(0), "") } function decodeDecimalString(input) { const minCharValue = 32; const { decoded } = Array.prototype.reduce.call(input, (acc, e) => { acc.state += e; if (+acc.state >= minCharValue) { acc.decoded += String.fromCharCode(+acc.state); acc.state = ""; } return acc; }, { decoded: "", state: "" }); return decoded; } const str = '729799107101114328297110107'; console.log("Decoded value:", decodeDecimalString(str)); const testStr2 = "Another example"; console.log("Original string 2:", testStr2); const testStr2Encoded = encodeDecimalString(testStr2); console.log("Original string 2, encoded:", testStr2Encoded); console.log("Original string 2, decoded:", decodeDecimalString(testStr2Encoded));

First of all getting the character from an ascii code is easy, javascript has a method String.fromCharCode(charCode) that converts a character code to a string.

 let str = "729799107101114328297110107"; const tester = str.split(''); let currentChar = ''; let result = ""; const minVal = 25; const maxval = 255; for (let i = 0; i < tester.length; i++) { currentChar += tester[i]; if (parseInt(currentChar) > minVal) { console.log(currentChar, String.fromCharCode(currentChar)); result += String.fromCharCode(currentChar); currentChar = ""; } if (parseInt(currentChar) > maxval) { currentChar = ''; console.log('above ' + maxval + ' clearing', currentChar); } } console.log(result);

As I mentioned in my comments, parsing a decimal ascii value is not deterministic if you use the full range 0-255 (unless the decimal value is zero padded). If you want to parse 2-3 digit decimal ascii values, you can make the process deterministic if you limit the range of possible values. Fortunately, standard printable ASCII characters end around 127 decimal, so you could use the following function to parse a 2-3 digit ascii decimal string and return a printable value. Note: The range of supported values in this example is 20-199 (inclusive).

function parse_ascii_decimal(input) {
    var index = 0;
    var max_len = input.length;
    var ordinal = 0;
    var min_ascii_val = 20;
    var ascii_str = "";
    while (index < max_len) {
        var tmpstr = input.substr(index, 2);
        var ordinal = parseInt(tmpstr);
        if (ordinal < min_ascii_val) {
            tmpstr = input.substr(index, 3);
            ordinal = parseInt(tmpstr);
            index++;
        }
        ascii_str += String.fromCharCode(ordinal);
        index += 2;
    }
    console.log(ascii_str);
    return ascii_str;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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