-6
\$\begingroup\$

Challenges

You need to do a program that outputs the input string to hexadecimal!

For each character in the input string, convert it's ascii value to hexadecimal and output it out.

Example

Something = 536f6d657468696e67

Of course, this is code golf, so the shorter code wins!

\$\endgroup\$
  • 2
    \$\begingroup\$ See Default input/output methods. A file is allowed by default and there isn't much (if any) difference after this (apart from separating by spaces in the other question) \$\endgroup\$ – Blue Feb 14 '16 at 17:40
  • 2
    \$\begingroup\$ I'm downvoting because this question is too simple. \$\endgroup\$ – isaacg Feb 14 '16 at 18:02
2
\$\begingroup\$

Pyth, 3 bytes

.Hz

Pyth docs:

.H <str>
  Converts A to a hexadecimal string, treating the string as a base 256 integer.

z is a variable autoinitialized to input().

Try it here.

\$\endgroup\$
  • 1
    \$\begingroup\$ 'That's what I get for writing a try-it link' \$\endgroup\$ – Blue Feb 14 '16 at 17:30
  • \$\begingroup\$ @muddyfish Hahaha. To be fair, you ninja'd my answer verbatim while I was writing an explanation on the last challenge :D \$\endgroup\$ – Doorknob Feb 14 '16 at 17:30
1
\$\begingroup\$

Bash, 6 bytes​​​​​​​​​​

xxd -p
\$\endgroup\$
1
\$\begingroup\$

JavaScript ES6, 49 bytes

s=>s.replace(/./g,l=>l.charCodeAt().toString(16))

Try it online

\$\endgroup\$
  • \$\begingroup\$ Wouldn't this replace newlines in the input with a instead of 0a? \$\endgroup\$ – Doorknob Feb 14 '16 at 17:34
  • \$\begingroup\$ @Doorknob the OP hasn't specified that and a is the hexadecimal value of 10 (newline) so I'm not sure \$\endgroup\$ – Downgoat Feb 14 '16 at 17:36
  • \$\begingroup\$ Should it become necessary, (l.charCodeAt()+256).toString(16).slice(1) edges out ('0'+l.charCodeAt().toString(16)).slice(-2) by one byte. \$\endgroup\$ – Neil Feb 14 '16 at 19:00
0
\$\begingroup\$

Python3 console, 21 bytes

input().encode("hex")

input() -- Gets the input in a string.
       .encode("hex") -- Encodes it to hexadecimal.

Doing this on the console saves the print argument.

\$\endgroup\$
0
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 7 chars / 9 bytes

ïⓢ⒨⒞ⓧ)⨝

Try it here (Firefox only).

Explanation later.

\$\endgroup\$
0
\$\begingroup\$

TeaScript, 5 bytes

ΣcT16

I really need to add base conversion built-ins

Try it online

Explanation

Σ    // Loop through input
 c   // Get char code
 T16 // To hexadecimal
\$\endgroup\$
0
\$\begingroup\$

JavaScript ES6, 50 bytes

s=>[for(c of s)c.charCodeAt().toString(16)].join``

Using an array generator comprehension, it loops through each character of the string and generates an array of the transform, then call .join() with an empty string as an argument to prevent inclusion of commas in default array toString() method.

Try it Online

This currently only works in Firefox v ≥ 30

\$\endgroup\$
0
\$\begingroup\$

Perl 6, 25 bytes

{lc [~] .ords».base(16)}

Usage:

my &code = {lc [~] .ords».base(16)}

say code 'Something';
# 536f6d657468696e67
\$\endgroup\$
0
\$\begingroup\$

Python, 24 bytes

lambda x:x.encode("hex")

Test:

a=lambda x:x.encode("hex")
a("something")    # 736f6d657468696e67
a("Hello World!") # 48656c6c6f20576f726c6421
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.