NodeJs Forum News

2014년 12월 6일 토요일

Can I convert a byte as a string into a real byte?

I am trying to convert a string with the byte number in it into a real byte.

var string = "23"; // Or it could be 4E, CA, 22, FF, or any other byte
var byte = somehowConvertStringToByte(string); // 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/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

I'm on mobile, if anything is incorrect then sorry. 



On Dec 3, 2014, at 9:36 PM, P THE AWESOME <p2kwolfgang@live.com> wrote:

I am trying to convert a string with the byte number in it into a real byte.

var string = "23"; // Or it could be 4E, CA, 22, FF, or any other byte
var byte = somehowConvertStringToByte(string); // 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.

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.

smime.p7s



Thank you so much. parseInt(string.slice(0,2), 16) worked perfectly!




작성자: Unknown 시간: 오후 6:25
이메일로 전송BlogThis!X에 공유Facebook에서 공유Pinterest에 공유

댓글 없음:

댓글 쓰기

최근 게시물 이전 게시물 홈
피드 구독하기: 댓글 (Atom)

프로필

Unknown
전체 프로필 보기

블로그 보관함

풍경 테마. Powered by Blogger.