1

im using node.js and I have a string that I am trying to convert into hex.

This is the function I'm using:

function toHex(str) {
 var hex = '';
 var i = 0;
 while(str.length > i) {
 hex += ''+str.charCodeAt(i).toString(16);
 i++;
 }
 return hex;
} 

And this is how I am trying to call it:

console.log('Payload: ' + toHex(decryptedPayload));

However when it runs I get this error:

 hex += ''+str.charCodeAt(i).toString(16);
 ^ TypeError: undefined is not a function
at toHex (C:\Users\Office\Desktop\luigi-master\lib\middleware.js:131:17)
at Middleware._transform (C:\Users\Office\Desktop\luigi-master\lib\middleware.js:161:29)
at Middleware.Transform._read (_stream_transform.js:179:10)
at Middleware.Transform._write (_stream_transform.js:167:12)
at doWrite (_stream_writable.js:301:12)
at writeOrBuffer (_stream_writable.js:288:5)
at Middleware.Writable.write (_stream_writable.js:217:11)
at Packetize.ondata (_stream_readable.js:540:20)
at Packetize.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
asked Apr 4, 2015 at 14:21
1
  • 1
    What type is decryptedPayload? Is it a Buffer (Buffer.isBuffer(decryptedPayload) === true)? If it's a Buffer, you can just do decryptedPayload.toString('hex') Commented Apr 4, 2015 at 14:23

1 Answer 1

5

If you have a Buffer, you can call toString() directly and pass the kind of output you want, for example: decryptedPayload.toString('hex')

answered Apr 4, 2015 at 15:51
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.