- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Javascript 将一系列八进制字节转换为文本,如下所示:
输入是\330\265 输出应该是 õ
以下工具成功地做到了这一点:
我正在尝试复制这个逻辑
最佳答案
这是一个非常简单的任务,您需要做的就是使用 Number.toString(radix)
方法转换从 String.charCodeAt( index)
对字符串进行编码。
结合使用 String.fromCharCode(charCode)
和 parseInt(numberString, radix)
,您可以使用值 8 作为基数来解码八进制值,将其传递给 fromCharCode
方法。
Input: Hello World
Encode: 110 145 154 154 157 040 127 157 162 154 144
Decode: Hello World
const
bytesToChars = (bytes) => bytes.map(byte => String.fromCharCode(parseInt(byte, 10))),
charsToBytes = (chars) => chars.map(char => char.charCodeAt(0)),
decToOctBytes = (decBytes) => decBytes.map(dec => dec.toString(8).padStart(3, '0')),
octToDecBytes = (octBytes) => octBytes.map(oct => parseInt(oct, 8)),
encode = (str) => decToOctBytes(charsToBytes(str.split(''))).join(' '),
decode = (octBytes) => bytesToChars(octToDecBytes(octBytes.split(/\s/))).join('');
let octBytes, str;
console.log('Input: ', str = 'Hello World');
console.log('Encode:', octBytes = encode(str));
console.log('Decode:', decode(octBytes));
.as-console-wrapper { top: 0; max-height: 100% !important; }
/* Redirect console output to HTML. */ document.body.innerHTML = '';
console.log=function(){document.body.innerHTML+=[].slice.apply(arguments).join(' ')+'\n';};
var octBytes, str;
console.log('Input: ', str = "Hello World");
console.log('Encode:', octBytes = encode(str));
console.log('Decode:', decode(octBytes));
function encode(str) {
return decToOctBytes(charsToBytes(str.split(''))).join(' ');
}
function decode(octBytes) {
return bytesToChars(octToDecBytes(octBytes.split(' '))).join('');
}
function charsToBytes(chars) {
return chars.map(function(char) {
return char.charCodeAt(0);
});
}
function bytesToChars(bytes) {
return bytes.map(function(byte) {
return String.fromCharCode(parseInt(byte, 10));
});
}
function decToOctBytes(decBytes) {
return decBytes.map(function(dec) {
return ('000' + dec.toString(8)).substr(-3);
});
}
function octToDecBytes(octBytes) {
return octBytes.map(function(oct) {
return parseInt(oct, 8);
});
}
body { font-family: monospace; white-space: pre; }
关于Javascript 将 Unicode 八进制字节转换为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30236912/
谁能推荐一个好的成员(member)结构? 例如,用户支付了 1 个月的成员(member)费,从 2012 年 2 月 1 日到 2012 年 3 月 1 日结束。 何时何地检查用户是否仍是成员(m
我想在我的 JTextPane 中实现查找机制(如文本编辑器或 word)。我希望它有下一个/上一个选项(向上/向下箭头)并突出显示它找到的所有单词。有没有简单的方法可以做到这一点? 最佳答案 我不是
Android 上是否有任何机制和/或编程实践来执行一次性安装/更新脚本?这似乎是一个非常基本的问题,但谷歌搜索没有帮助。 假设我的应用程序使用了一个数据库,并且需要一个常量值表(例如国家名称、城市名
是否可以将 SimpleMembership 与 ASP.NET WebForms 一起使用? John Galloway 的文章似乎表明 WebForms“在 ASP.NET 成员资格之上实现 OA
我是一名优秀的程序员,十分优秀!