Print the input in hexadecimal

-6

0

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!

AlexINF

Posted 2016-02-14T17:23:42.327

Reputation: 181

Question was closed 2016-02-14T20:36:19.987

2

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)

– Blue – 2016-02-14T17:40:23.137

2I'm downvoting because this question is too simple. – isaacg – 2016-02-14T18:02:10.113

Answers

2

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.

Doorknob

Posted 2016-02-14T17:23:42.327

Reputation: 68 138

1'That's what I get for writing a try-it link' – Blue – 2016-02-14T17:30:00.473

@muddyfish Hahaha. To be fair, you ninja'd my answer verbatim while I was writing an explanation on the last challenge :D – Doorknob – 2016-02-14T17:30:40.093

1

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

xxd -p

Doorknob

Posted 2016-02-14T17:23:42.327

Reputation: 68 138

1

JavaScript ES6, 49 bytes

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

Try it online

Downgoat

Posted 2016-02-14T17:23:42.327

Reputation: 27 116

Wouldn't this replace newlines in the input with a instead of 0a? – Doorknob – 2016-02-14T17:34:58.913

@Doorknob the OP hasn't specified that and a is the hexadecimal value of 10 (newline) so I'm not sure – Downgoat – 2016-02-14T17:36:56.930

Should it become necessary, (l.charCodeAt()+256).toString(16).slice(1) edges out ('0'+l.charCodeAt().toString(16)).slice(-2) by one byte. – Neil – 2016-02-14T19:00:31.053

0

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.

AlexINF

Posted 2016-02-14T17:23:42.327

Reputation: 181

If you do it on the console, it will print itself – AlexINF – 2016-02-14T17:43:08.917

You should mention, that this doesn't work in the recent Python versions 3.4 and 3.5. https://docs.python.org/3/whatsnew/3.4.html#codec-handling-improvements

– Jakube – 2016-02-14T17:59:55.590

0

, 7 chars / 9 bytes

ïⓢ⒨⒞ⓧ)⨝

Try it here (Firefox only).

Explanation later.

Mama Fun Roll

Posted 2016-02-14T17:23:42.327

Reputation: 7 234

0

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

Patrick Roberts

Posted 2016-02-14T17:23:42.327

Reputation: 2 475

0

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

Downgoat

Posted 2016-02-14T17:23:42.327

Reputation: 27 116

0

Perl 6, 25 bytes

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

Usage:

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

say code 'Something';
# 536f6d657468696e67

Brad Gilbert b2gills

Posted 2016-02-14T17:23:42.327

Reputation: 12 713

0

Python, 24 bytes

lambda x:x.encode("hex")

Test:

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

andlrc

Posted 2016-02-14T17:23:42.327

Reputation: 1 613