18

I need to convert data into String to Hex and then again from Hex to String using nodejs 8

I have issue while decoding from Hex to String

Code to convert string into hex

function stringToHex(str)
{
    const buf = Buffer.from(str, 'utf8');
    return buf.toString('hex');
}

Code to convert hex into string

function hexToString(str)
{
    const buf = new Buffer(str, 'hex');
    return buf.toString('utf8');
}

I have string dailyfile.host

output of encoding: 3162316637526b62784a5a37697a45796c656d465643747a4a505a6f59774641534c75714733544b4446553d

output of decoding: 1b1f7RkbxJZ7izEylemFVCtzJPZoYwFASLuqG3TKDFU=

Required output of decoding: dailyfile.host

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
  • FYI, this is not an encryption, this is an _encoding_. – Patrick Roberts Sep 10 '18 at 16:00
  • Thanks @PatrickRoberts for correcting me :) – Muhammad Hassaan Sep 10 '18 at 16:01
  • 1
    Are your sure you use this code ? (And call it right) I define function same you. I call this code `stringToHex('dailyfile.host'); // '6461696c7966696c652e686f7374'` ` hexToString(stringToHex('dailyfile.host')); // 'dailyfile.host'` – hong4rc Sep 10 '18 at 16:35
  • Voting to close, as [mcve] is not satisfied. Please produce a complete and verifiable example. There is no code here that can reproduce a conversion from `dailyfile.host` to `3162316637526b62784a5a37697a45796c656d465643747a4a505a6f59774641534c75714733544b4446553d`. – Patrick Roberts Sep 10 '18 at 16:42

1 Answers1

30

You need to use Buffer.from() for decoding as well. Consider writing a higher-order function to reduce the amount of repeated code:

const convert = (from, to) => str => Buffer.from(str, from).toString(to)
const utf8ToHex = convert('utf8', 'hex')
const hexToUtf8 = convert('hex', 'utf8')

hexToUtf8(utf8ToHex('dailyfile.host')) === 'dailyfile.host'
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • 1
    This still does not work for me, I am using `nodejs v8.11`. – Muhammad Hassaan Sep 10 '18 at 16:14
  • 3
    @Hassaan "This does not work" is not a problem statement. Be more specific about the issue you're having. – Patrick Roberts Sep 10 '18 at 16:21
  • 1
    Issue is still the same while decoding. Encoded string `4d6a772b646e316c4e314867684c435431724a53674e593430306333675274474753506742672f622b4f6b3d` but decoded string is `Mjw+dn1lN1HghLCT1rJSgNY400c3gRtGGSPgBg/b+Ok=` – Muhammad Hassaan Sep 10 '18 at 16:23
  • That's because your hexadecimal string _is_ the hex encoding of that output string. What's the output of `utf8ToHex('dailyfile.host')`? It should be `6461696c7966696c652e686f7374`. – Patrick Roberts Sep 10 '18 at 16:29
  • I see, how to resolve it? Can you please update answer for this? – Muhammad Hassaan Sep 10 '18 at 16:31
  • Where is that "encoded string" coming from? Can you update your question to clarify? – Patrick Roberts Sep 10 '18 at 16:33
  • I am giving string to as parameter of GET request to URL. I am updating my question – Muhammad Hassaan Sep 10 '18 at 16:46
  • 1
    I have tested your provided answer as sandbox. Its working fine but there was issue with my code as you mentioned in above comment. I was using `@skavinvarnan/cryptlib` module for encryption and while while decoding I was not using that module. Thanks man for pointing me in right way :) – Muhammad Hassaan Sep 10 '18 at 16:52
  • Just check that hex string doesnot start with '0x', and above code should work while converting from hex to utf8 – krupesh Anadkat Jan 02 '21 at 17:40
  • The way that you answered the question communicated how an entire concept works using 4 lines of code. Its brilliant. 1up Fantastic answer, this is great man! **Thank sir @PatrickRoberts !** – JΛYDΞV Oct 04 '22 at 03:33
  • I do want to point out that `from` is the name of a parameter, not a keyword. It seems that Stack Overflow is unable to correctly parse & highlight JavaScript. – JΛYDΞV Oct 04 '22 at 03:34