如何在打字稿中将八进制转换为文本?

时间:2019-01-31 13:17:33

标签: javascript text ascii octal

我将此字符串设置为八进制:

60 150 145 154 154 157

要如何将其转换为纯文本格式?

对于十六进制字符串,我做到了,它起作用了:

hexToPlain() {
this.plaintext = "";
this.hextext = this.hextext.trim();
var hex = this.hextext.replace(/ /g,"").toString();

for (var i = 0; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2)
    this.plaintext += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}

对于OCTAL,我已经尝试过:

octToPlain() {
this.plaintext = "";
this.octtext = this.octtext.trim();
var oct = this.octtext.replace(/ /g,"").toString();

for (var i = 0; (i < oct.length && oct.substr(i, 2) !== '00'); i += 2)
    this.plaintext += String.fromCharCode(parseInt(oct.substr(i, 2), 8));
}

但是它仅在我有由60这样的两位数字组成的八进制字符时才起作用,而对于145这样的三位数的八进制字符却不起作用

1 个答案:

答案 0 :(得分:0)

  

OP的答案

octToPlain() {
   this.plaintext = "";
   this.octtext = this.octtext.trim();
   this.plaintext = this.octtext.split(' ').map(s => parseInt(s,8)).map(n => String.fromCharCode(n)).join('');
}