简体   繁体   English

从十六进制字符串获取原始十六进制

[英]Get raw HEX from HEX string

I've spend the last few hours researching, but couldn't find anything suiting my needs. 我花了最后几个小时进行研究,但找不到适合我需要的东西。

I need to send escaped hex values to a device via TCP, like this: 我需要通过TCP将转义的十六进制值发送到设备,如下所示:

var char = "\x14";

Now i need to get the size of a string and convert it in escaped hex, like the above, dynamically. 现在,我需要获取字符串的大小,并像上面一样,将其动态转换为转义的十六进制。

Converting from decimal is easy of course: 从十进制转换当然很容易:

var string = "qwertzuiopasdfghjkly"; //Length 20
var hex = (string.length).toString(16); //Returns 14

I've tried it via non-displayable ASCII characters: 我已经通过不可显示的ASCII字符尝试过:

char = String.fromCharCode(hex);

But that doesn't return the same as 但这不会返回与

char = "\x14";

Already found the solution in other languages, but not JavaScript ... 已经找到其他语言的解决方案,但没有JavaScript ...

As it turned out, i was looking for String.fromCodePoint() , a relatively new function, specified in ECMAScript 2015. I also had to use utf8 Encoding to make it work. 事实证明,我正在寻找String.fromCodePoint() ,这是ECMAScript 2015中指定的一个相对较新的函数。我还必须使用utf8编码使其起作用。

var string = "qwertzuiopasdfghjkly"; //Length 20
var char = String.fromCodePoint(string.length) // Equivalent to char = "\x14";

More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint 更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint

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

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