var string = "23"; // Or it could be 4E, CA, 22, FF, or any other byte
var byte = somehowConvertStringToByte(str ing); // The variable "byte" Should look like 0x23 to NodeJS
var otherByte = 0x23; // This is 100% equal to the variable "byte." NodeJS will see this variable and "byte" the exact same way.
Hopefully this makes sense. I'm have to generate some stupid checksums to get my UPB serial PIM to accept commands. I have the reset of the code working. I'm making a CLI script that generates UPB commands and then sends them with node-serialport.
Thank you for any help, as this has completely stumped me.
This parseInt() something you want?
Try parseInt("23", 16)
https://developer.mozilla.org/
I'm on mobile, if anything is incorrect then sorry.
Try this:
parseInt(string.slice(0,2), 16)
string.slice(0, 2) grabs the first two characters: two nybbles, one byte.
parseInt(str, 16) parses that into a native Number, assuming base 16 (hexadecimal, like your example)
0x23 is a literal for a Number, so that, too, makes a value that is equal to what that returns.
Thank you so much. parseInt(string.slice(0,2), 16) worked perfectly!
|
댓글 없음:
댓글 쓰기