I'm using jquery / javascript to print raw data to a network label printer. In order to do that the strings will need to be converted to hexadecimal. with a \\x
preceding each character.
SO if I wanted to print a label with " I love stackoverflow " the string would need to be converted to \\x49\\x20\\x6C\\x6F\\x76\\x65\\x20\\x73\\x74\\x61\\x63\\x6B\\x6F\\x76\\x65\\x72\\x66\\x6C\\x6F\\x77
.
I have searched around and I can't find anything on this. Any ideas on how this can be achieved?
I could create a library of variables for each character var A = '/x41;
and then run the string through a 'checker' that goes through the entire sting, but I feel like I am missing a simpler solution.
You can simply do it this way:
function stringtohex(str) { return [...str].map(e => '\\\\x' + Number(e.charCodeAt(0)).toString(16)).join('') } var convertedstr = stringtohex( "I love stackoverflow"); console.log(convertedstr);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.