我花了几个小时的研究,但找不到适合我需要的东西。
我需要通过TCP向设备发送转义十六进制值,如下所示:
var char = "\x14";现在,我需要得到字符串的大小,并将其转换为转义十六进制,就像上面的那样,动态地转换它。
当然,从十进制转换是很容易的:
var string = "qwertzuiopasdfghjkly"; //Length 20
var hex = (string.length).toString(16); //Returns 14我尝试过通过不可显示的ASCII字符:
char = String.fromCharCode(hex);但这并不能像
char = "\x14";已经在其他语言中找到了解决方案,但没有找到JavaScript .
发布于 2016-02-29 03:05:18
结果,我正在寻找String.fromCodePoint(),这是一个相对较新的函数,在ECMAScript 2015中指定。我还必须使用utf8编码来使其工作。
var string = "qwertzuiopasdfghjkly"; //Length 20
var char = String.fromCodePoint(string.length) // Equivalent to char = "\x14";https://stackoverflow.com/questions/35690044
复制相似问题