# What are these letters?

So what might be the shortest language for this simple task?

You are given an input of type String - the string is a valid hexadecimal number ([0-9A-F]). Convert it to a decimal number and print it to the console.

## Tests:

HEX -> DEC
1 -> 1
98 -> 152
539 -> 1337
1337 -> 4919
A5F9 > 42489
F -> 15


## Win Condition:

This is code golf - shortest code wins.

## Rules:

• You must take the input(like function parameter) as String.
• You must return the result as String.
• Related. – Martin Ender Apr 11 '18 at 20:22
• Are quotes allowed in the input? e.g. "98" – recursive Apr 11 '18 at 20:23
• Why do we have to use the standard streams, instead of, e.g., function arguments? – Dennis Apr 11 '18 at 20:29
• Convert the ASCII to Char? But the whole point is that such a language has no concept of character data. – Adám Apr 11 '18 at 20:51
• Why the requirement to output as a string? Trivial as this challenge may be, that requirement adds absolutely nothing to it. – Shaggy Apr 11 '18 at 22:03

# 05AB1E, 1 byte

H


Try it online!

Am I missing something? I feel like... I feel like I am.

If "print as a string" implies "surround result with quotes":

# 05AB1E, 5 bytes

H'".ø


Try it online!

05AB1E is based in Python, so it doesn't really matter if it's a string, integer or whatever; H will convert an integer, a string, a Ferrarri or 27 gnomes on patrol into hex. Given, the latter two aren't valid, it will error upon trying to execute or return the original string with no function applied.

show.abs.read.("0x"++)


Thanks to @user9549915 for saving a byte

Try it online!

• Question updated. You might save some bytes now – 0x45 Apr 11 '18 at 20:39
• You can save a byte by using abs instead of (+0) – user9549915 Apr 11 '18 at 22:04

# JavaScript (Node.js), 18 bytes

My first JS golf!

x=>''+eval('0x'+x)


Try it online!

Just prepends 0x to the input and evaluates that, since it then represents a hexadecimal number in JS. Finally, we need to concatenate the empty string to convert to string.

• no eval required – l4m2 Apr 11 '18 at 21:21
• @l4m2 Yeah I realized that after seeing this improvement to mine, and thanks to op order I was able to drop () too – Pandacoder Apr 11 '18 at 21:27
• @l4m2 So now what? If I change this, then my solution will be identical to Pandacoder's, though we arrived to it independently. – Adám Apr 11 '18 at 22:34

# Python 2, 20 bytes

lambda s:int(s,16)


Try it online!

# J, 6 bytes

":@dfh


Try it online! (Contains a test suite, including type checking the parameters and the return value.)

This is pretty simple:

":@dfh
dfh    obtain *d*ecimal *f*rom *h*ex
@       then
":        format the result (cast to string)


# Jelly, 8 bytes

⁾0x;ŒVṾ


This is a monadic link that takes a string as argument and returns a string.

Try it online!

### How it works

⁾0x;ŒVṾ  Monadic link. Argument: s (string)

⁾0x;     Prepend "0x" to s.
ŒV   Eval as Python code.
This returns the result as an integer.
Ṿ  Uneval; turn the integer into its string representation.

• It seems to work fine on TIO without Ṿ. – Pavel Apr 12 '18 at 14:32
• Works perfectly, yes. The challenge explicitly asks to return a string though. :/ – Dennis Apr 12 '18 at 14:34

# JavaScript (Node.js), 2015 14 bytes

Thanks to l4m2 for catching my stupid mistake and saving me a byte. :)

x=>'0x'+x-0+''


Try it online!

# JavaScript (Node.js), 15 bytes

x=>'0x'+x-[]+''


Try it online!

# Original 20 bytes

l=>''+parseInt(l,16)


Try it online!

• -[] => -0 lol – l4m2 Apr 11 '18 at 21:29

# ><>, 23 bytes

0l1=?n88+*{'0'-:a)7*-+!


Try it online!

If the hex can be contain lowercase letters instead of uppercase, we can do it in 17 bytes:

0l1=?n88+*{e0p +!


# Charcoal, 6 bytes

Ｉ⍘↧Ｓ¹⁶


Try it online! Link is to verbose version of code. Save 1 byte if the input can be in lower case. Charcoal doesn't automatically convert numbers to decimal on output, so the cast operator is necessary anyway. Explanation:

   Ｓ    Input string
↧     Lower case
¹⁶  Literal 16
⍘      Base conversion
Ｉ       Cast to string
Implicitly print


# Java 8, 24 bytes

s->Long.valueOf(s,16)+""


Try it online here.

# Excel, 14 bytes

=0&HEX2DEC(A1)


Concatenates a leading 0 to convert to String. I.e. 042489

# Excel, 20 bytes

=TEXT(HEX2DEC(A1),0)


Naive solution.

Format both outputs verified as String (=T(B2))

# Pyth, 5 4 bytes

iz16

Parses input as a hex number and returns the base 10 equivalent.

• Welcome to PPCG! – Steadybox Apr 17 '18 at 16:03

# Stax, 3 bytes

i|H


Run and debug it

i ensures that the input doesn't get automatically evaluated as an integer, and |H converts from/to hexadecimal. The link above is for the online interpreter, but the language really does use stdin and stdout. The C# implementation of the language does this, which is available from the repository above.

# dc, 5 bytes

16i?p


Try it online!

Rather trivial. If input really needs to be a string vs. the language's natural hex input, then 16i?xp takes care of that in 6 bytes.

# Jelly, 10 bytes

ØHiÐ€ɠ’ḅ⁴Ṿ


Try it online!

## How it works

ØHiÐ€ɠ’ḅ⁴Ṿ - Main link, no arguments

ØH         - Yield the string “0123456789ABCDEF”. Call that H
ɠ     - Read a line from STDIN, e.g. “A5F9”. Call that A
Ð€      - For each character in A
i   ’    - Get the 0-based index of that character in H
⁴  - Yield 16
ḅ   - Convert from base
Ṿ - Convert to string

• Question updated. You might save some bytes now – 0x45 Apr 11 '18 at 20:40
• @0x45 Oddly enough, I doubt it. Reading from STDIN takes one byte here ɠ, but if the input cannot be surrounded by ", then it would automatically be evaluated, which ɠ doesn't do, and unevaluating could take more bytes. – caird coinheringaahing Apr 11 '18 at 20:41

# Ruby, 14 bytes

p gets.to_i 16


Try it online!

# APL (Dyalog), 13 bytes

⍕16⊥(⎕D,⎕A)⍳⊢


Try it online!

• 16⊥⍞⍳⍨⎕D,⎕A as a full program prints to stdout and so doesn't require ⍕. – Adám Apr 11 '18 at 22:32

# Julia, 23 bytes

!s=dec(parse(Int,s,16))


Try it online!

dec appears to be shorter than repr or string.

# Japt, 4 bytes

Outputting as a string doubles the byte count!

nG s


Try it

## Batch, 15 bytes

@cmd/cset/a0x%1


If input via command-line argument is unacceptable, then for 26 bytes:

@set/ph=
@cmd/cset/a0x%h%


# R, 27 bytes

paste(strtoi(scan(,""),16))


Try it online!

strtoi will always return a number of type integer which is a signed 32-bit integer type, and paste implicitly converts to character, as required.

• I don't know the details about this language, but you aren't taking from stdin are you? – 0x45 Apr 11 '18 at 20:29
• @0x45 well...sorta. scan() takes input from stdin except that stdin will in certain circumstances (like on TIO) redirect to the file so while technically on TIO it's not stdin, it's equivalent. – Giuseppe Apr 11 '18 at 20:30
• Question updated. You might save some bytes now – 0x45 Apr 11 '18 at 20:39
• @0x45 nah it's the same either way. Thank you, though. – Giuseppe Apr 11 '18 at 20:47
• I'm not very familiar with R, but it seems you are returning an integer instead of a string (as per the challenge rules). – O.O.Balance Apr 11 '18 at 22:55

# WinDbg, 15 bytes

.printf"%d",$u0  Input is done by first setting the pseudo-alias $u0 with the input string and then running the code (eg- to set pseudo-alias: r$.u0=a5f9). Prints the input, conversion from hex to decimal is implicit. How it works: .printf"%d", $$Prints a decimal u0$$ The decimal to print  If the extra sugar that WinDbg prints when evaluating an expression is allowed: ## WinDbg, 4 bytes ?$u0


How this one works:

?                  $$Evaluates an expression u0$$ The expression to evaluate


This one would have output like this (for input= A5F9):

Evaluate expression: 42489 = 0000a5f9


# C# .NET, 34 bytes

s=>System.Convert.ToInt32(s,16)+""


I have the feeling this is overly long, but the alternative s=>int.Parse(s,System.Globalization.NumberStyles.HexNumber)+"" is even worse..

Try it online.

# Perl 5-lp, 6 bytes

#!/usr/bin/perl -lp
$_=hex  Try it online! # Io, 24 bytes method(x,x fromBase(16))  Try it online! # Tcl, 20 bytes proc D n {expr 0x$n}


Try it online!

puts [expr 0x$argv]  Try it online! # Coconut, 15 bytes str..int$(?,16)


Try it online!