将ASCII小数转换为字符串

发布于 2025-02-06 06:01:46 字数 439 浏览 1 评论 0 原文

我在DB中的一列中存储了ASCII小数点的字符串:

[104 105]

这将转换为 hi

我需要能够将我的列转换为字符串表示。

我知道我可以使用 string.fromcharcode(num,... num+1),但这对我不太有用。

我需要解析并将我的DB列值 [104 105] 分为两个单独的vars:

var num1 = 104;
var num2 - 105;

当我具有复杂的ASCII小数表示时,这无效。

是否有更有效的方法可以做到这一点?我的输入将是 [104 105 243 0 0 0 255 ...] ,它是ASCII小数点的,我需要获得字符串表示。

I have a string in representation of ASCII decimal stored in a column in my db:

[104 105]

this converts to hi.

I need to be able to convert my column into string representation.

I know I can use String.fromCharCode(num,...num+1) but it doesn't quite work for me.

I would need to parse and split my db column value [104 105] into two separate vars:

var num1 = 104;
var num2 - 105;

this doesn't work when I have a complex ASCII decimal representation.

Is there a more efficient way to do this? My input would be something like [104 105 243 0 0 255...] which is in ASCII decimal and I need to get the string representation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱果果 2025-02-13 06:01:46

您需要首先解析该字符串,通过删除 [] 字符,在空格上拆分并将数组元素转换为数字。

let num_string = '[104 105]';
let nums = num_string.replace(/[\[\]]/g, '').split(' ').map(Number);
let string = String.fromCharCode(...nums);
console.log(string);

You need to parse that string first, by removing the [] characters, splitting at spaces, and converting the array elements to numbers.

let num_string = '[104 105]';
let nums = num_string.replace(/[\[\]]/g, '').split(' ').map(Number);
let string = String.fromCharCode(...nums);
console.log(string);

岁月无声 2025-02-13 06:01:46

您可以使用 带有正则,然后 地图 并返回转换的代码的数组。只是在末尾将数组成弦。

function convert(str, re) {
  return str.match(/(\d+)/g).map(code => {
    return String.fromCharCode(code);
  }).join('');
}

console.log(convert('[104 105]'));
console.log(convert('[104 105 243 0 0 255]'));

You can find the codes using match with a regex, and then map and return an array of the converted codes. Just join the array into a string at the end.

function convert(str, re) {
  return str.match(/(\d+)/g).map(code => {
    return String.fromCharCode(code);
  }).join('');
}

console.log(convert('[104 105]'));
console.log(convert('[104 105 243 0 0 255]'));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文