-1
\$\begingroup\$

Assignment:

Create a function that can convert a string to its encoded hexadecimal value.

  • In your answer, also show how to run this function (to validate answers).
  • Your function must return OR print this text to STDOUT or any acceptable alternative.
  • Your return value doesn't need to have whitespace between hexadecimal pairs.
  • You may not use libraries/packages in general and any other functions that directly converts a string to hexadecimal. For example, using a function or package to convert the string to bytes is OK.
  • This is Code Golf, so shortest code wins.

Example Input:

Input Example:

Hello World!

Output Examples:

48 65 6c 6c 6f 20 57 6f 72 6c 64 21

OR

48656c6c6f20576f726c6421
\$\endgroup\$
1

1 Answer 1

0
\$\begingroup\$

Node.JS, 141 bytes

f=s=>{b=new TextEncoder().encode(s),h='';for(_ of b){H=(_>>4)&0x0F,l=_&0x0F,h+=String.fromCharCode(H+(H<10?48:87),l+(l<10?48:87))};return h;}

Usage

f("Hello World!")

Returns

48656c6c6f20576f726c6421
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.