I was provided a function like this
function toHexString(bytes) {
const a = bytes.map(function (byte) {
console.log("--------------------")
const parsedValue = ("00" + (byte & 0xFF).toString(16)).slice(-2)
console.log(parsedValue)
console.log(typeof parsedValue)
console.log("--------------------")
return ("00" + (byte & 0xFF).toString(16)).slice(-2);
});
console.log(a)
}
toHexString(Buffer.from("2241f2", 'hex'))
Here is the log response of it
--------------------
22
string
--------------------
--------------------
41
string
--------------------
--------------------
f2
string
--------------------
<Buffer 16 29 00>
I actually thought it gonna provide me 2241f2
in the response but it is not. Can you guys explain it to me why is that ?
If possible, Can you re-create it with a for loop to help me understand it better ?
My try using loop
const a = Buffer.from("2241f2", 'hex')
const b= []
for (let byte of a) {
b.push(("00" + (byte & 0xFF).toString(16)).slice(-2))
}
console.log(b)
Result
[ '22', '41', 'f2' ]