Reputation: 83
Now I have to convert the hexadecimal encoded in a String to a byte hexadecimal.
var str = "5e"
var b = // Should be 0x5e then.
if str = "6b", then b = 0x6b and so on.
Is there any function in javascript, like in java
Byte.parseByte(str, 16)
Thanks in advance
Upvotes: 3
Views: 424
Reputation: 83
I solved it by using just
new Buffer("32476832", 'hex')
this solved my problem and gave me the desired buffer
<Buffer 32 47 68 32>
Upvotes: 1
Reputation: 1703
From your comment, if you expect "an output of 0x6b" from the string "6b" then just prepend "0x" to your string, and further manipulate as you need. There is no Javascript type that will output a hexadecimal in a readable format that you'll see prefixed with '0x' other than a string.
Upvotes: 1
Reputation: 69964
The function you want is parseInt
parseInt("6b", 16) // returns 107
The first argument to parseInt
is a string representation of the number and the second argument is the base. Use 10 for decimal and 16 for hexadecimal.
Upvotes: 1