Currently working on developing crypto tools, I encountered an issue while attempting to utilize the map function to reduce characters into a string. Strangely enough, one function works perfectly fine, while the other fails to 0 pad the string. What could possibly be causing this variation?
// returns '0102'
export const bufferToHex = (buffer: Buffer): string => {
const bytes = new Uint8Array(buffer)
const hex = []
bytes.forEach(byte => hex.push(byte.toString(16).padStart(2, '0')))
return hex.join('')
}
// returns '12'
export const bufferToHex = (buffer: Buffer): string => {
const bytes = new Uint8Array(buffer)
return bytes
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('')
}
When calling with:
bufferToHex(Buffer.from([1, 2])
Can anyone shed light on why this is happening?