-12
\$\begingroup\$

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

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.
\$\endgroup\$
  • 3
    \$\begingroup\$ Related. \$\endgroup\$ – Martin Ender Apr 11 '18 at 20:22
  • 2
    \$\begingroup\$ Are quotes allowed in the input? e.g. "98" \$\endgroup\$ – recursive Apr 11 '18 at 20:23
  • 4
    \$\begingroup\$ Why do we have to use the standard streams, instead of, e.g., function arguments? \$\endgroup\$ – Dennis Apr 11 '18 at 20:29
  • 3
    \$\begingroup\$ Convert the ASCII to Char? But the whole point is that such a language has no concept of character data. \$\endgroup\$ – Adám Apr 11 '18 at 20:51
  • 10
    \$\begingroup\$ Why the requirement to output as a string? Trivial as this challenge may be, that requirement adds absolutely nothing to it. \$\endgroup\$ – Shaggy Apr 11 '18 at 22:03

27 Answers 27

3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Haskell, 24 23 22 bytes

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

Thanks to @user9549915 for saving a byte

Try it online!

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

Python 2, 20 bytes

lambda s:`int(s,16)`

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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)
\$\endgroup\$
2
\$\begingroup\$

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.
\$\endgroup\$
  • \$\begingroup\$ It seems to work fine on TIO without . \$\endgroup\$ – Pavel Apr 12 '18 at 14:32
  • \$\begingroup\$ Works perfectly, yes. The challenge explicitly asks to return a string though. :/ \$\endgroup\$ – Dennis Apr 12 '18 at 14:34
2
\$\begingroup\$

JavaScript (Node.js), 20 15 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!

\$\endgroup\$
  • \$\begingroup\$ -[] => -0 lol \$\endgroup\$ – l4m2 Apr 11 '18 at 21:29
2
\$\begingroup\$

Java 8, 24 bytes

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

Try it online here.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

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

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
\$\endgroup\$
  • \$\begingroup\$ Question updated. You might save some bytes now \$\endgroup\$ – 0x45 Apr 11 '18 at 20:40
  • \$\begingroup\$ @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. \$\endgroup\$ – caird coinheringaahing Apr 11 '18 at 20:41
1
\$\begingroup\$

Ruby, 14 bytes

p gets.to_i 16

Try it online!

\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog), 13 bytes

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

Try it online!

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

Julia, 23 bytes

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

Try it online!

dec appears to be shorter than repr or string.

\$\endgroup\$
1
\$\begingroup\$

Japt, 4 bytes

Outputting as a string doubles the byte count!

nG s

Try it

\$\endgroup\$
1
\$\begingroup\$

><>, 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 +!
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 6 bytes

I⍘↧S¹⁶

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:

   S    Input string
  ↧     Lower case
    ¹⁶  Literal 16
 ⍘      Base conversion
I       Cast to string
        Implicitly print
\$\endgroup\$
1
\$\begingroup\$

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%
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I don't know the details about this language, but you aren't taking from stdin are you? \$\endgroup\$ – 0x45 Apr 11 '18 at 20:29
  • \$\begingroup\$ @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. \$\endgroup\$ – Giuseppe Apr 11 '18 at 20:30
  • \$\begingroup\$ Question updated. You might save some bytes now \$\endgroup\$ – 0x45 Apr 11 '18 at 20:39
  • \$\begingroup\$ @0x45 nah it's the same either way. Thank you, though. \$\endgroup\$ – Giuseppe Apr 11 '18 at 20:47
  • \$\begingroup\$ I'm not very familiar with R, but it seems you are returning an integer instead of a string (as per the challenge rules). \$\endgroup\$ – O.O.Balance Apr 11 '18 at 22:55
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Pyth, 5 4 bytes

iz16

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

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Steadybox Apr 17 '18 at 16:03
0
\$\begingroup\$

Tcl, 20 bytes

proc D n {expr 0x$n}

Try it online!

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

Tcl, 19 bytes

puts [expr 0x$argv]

Try it online!

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

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))

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

Coconut, 15 bytes

str..int$(?,16)

Try it online!

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

Perl 5 -lp, 6 bytes

#!/usr/bin/perl -lp
$_=hex

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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