What are these letters?

-12

0

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.

0x45

Posted 2018-04-11T20:16:25.073

Reputation: 810

3Related. – Martin Ender – 2018-04-11T20:22:38.890

2Are quotes allowed in the input? e.g. "98" – recursive – 2018-04-11T20:23:08.303

1@recursive "The string is a valid hexadecimal number ([0-9A-F])" The regex wouldnt match ", so no. – 0x45 – 2018-04-11T20:24:06.257

4Why do we have to use the standard streams, instead of, e.g., function arguments? – Dennis – 2018-04-11T20:29:15.300

@Dennis to make it more tricky - stdin is string for every language I know, so the crux is to convert it couple of times... – 0x45 – 2018-04-11T20:34:23.277

@0x45 But the input needs to be a string anyway (not a decimal number), so what's the added difficulty? Also, is stdin as number in APL. – Adám – 2018-04-11T20:36:01.570

Updated the task @Adám – 0x45 – 2018-04-11T20:36:57.237

1Printing a number to stdout/stderr constitutes returning a string, right? – Adám – 2018-04-11T20:38:35.533

1What about languages which can only handle numbers (and therefore I/O strings as list of ASCII codes)? – Adám – 2018-04-11T20:39:45.550

Convert the ASCII to Char and then concatenate to a String @Adám – 0x45 – 2018-04-11T20:41:11.957

@Adám printing to stdout is String, yes. – 0x45 – 2018-04-11T20:42:15.523

3Convert the ASCII to Char? But the whole point is that such a language has no concept of character data. – Adám – 2018-04-11T20:51:49.710

1

Repost of https://codegolf.stackexchange.com/questions/3741/hexadecimal-to-decimal ?

– l4m2 – 2018-04-11T21:27:06.220

@l4m2 I thought of flagging this question for the same reason but the other question was closed. Would the best path to be to edit that 2015 question to be more clear and re-open it? – Engineer Toast – 2018-04-11T21:40:59.653

10Why the requirement to output as a string? Trivial as this challenge may be, that requirement adds absolutely nothing to it. – Shaggy – 2018-04-11T22:03:53.070

1Can the letters in the hex be lowercase? – Jo King – 2018-04-11T22:09:25.100

@Engineer Toast I think you meant the 2011 one? The 2015 one that Martin Ender linked is the opposite conversion. With regards to the 2011 one, it does have two clear restrictions (NaN -> 0, length limit of 8) that this question doesn't have (and then everything else is a little murky), so would we be replacing it entirely or merging the restrictions of both questions? – Pandacoder – 2018-04-11T22:09:45.950

Is it OK to add a leading 0 to the output? I.e. 042489 What about a trailing .? I.e. 42489. – Wernisch – 2018-04-12T10:04:50.413

@Wernisch leading 0 okay, . not. – 0x45 – 2018-04-12T11:13:03.463

@Pandacoder I did mean the 2011, thanks. I had gotten the two confused. That makes a difference, too. After 6.5 years, it's probably better to improve this question rather than re-open the old one. – Engineer Toast – 2018-04-12T15:34:44.287

@EngineerToast usually you don't come back from -12. – Magic Octopus Urn – 2018-04-13T15:55:52.360

Answers

3

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.

Magic Octopus Urn

Posted 2018-04-11T20:16:25.073

Reputation: 19 422

1Aww man – caird coinheringaahing – 2018-04-12T16:39:58.003

3

Haskell, 24 23 22 bytes

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

Thanks to @user9549915 for saving a byte

Try it online!

Angs

Posted 2018-04-11T20:16:25.073

Reputation: 4 825

Question updated. You might save some bytes now – 0x45 – 2018-04-11T20:39:48.770

You can save a byte by using abs instead of (+0) – user9549915 – 2018-04-11T22:04:31.623

2

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!

Pandacoder

Posted 2018-04-11T20:16:25.073

Reputation: 190

-[] => -0 lol – l4m2 – 2018-04-11T21:29:28.230

2

Python 2, 20 bytes

lambda s:`int(s,16)`

Try it online!

totallyhuman

Posted 2018-04-11T20:16:25.073

Reputation: 15 378

2

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)

Conor O'Brien

Posted 2018-04-11T20:16:25.073

Reputation: 36 228

2

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.

Dennis

Posted 2018-04-11T20:16:25.073

Reputation: 196 637

It seems to work fine on TIO without . – Pavel – 2018-04-12T14:32:11.430

Works perfectly, yes. The challenge explicitly asks to return a string though. :/ – Dennis – 2018-04-12T14:34:54.163

2

Java 8, 24 bytes

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

Try it online here.

O.O.Balance

Posted 2018-04-11T20:16:25.073

Reputation: 1 499

1

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.

recursive

Posted 2018-04-11T20:16:25.073

Reputation: 8 616

1

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.

Giuseppe

Posted 2018-04-11T20:16:25.073

Reputation: 21 077

I don't know the details about this language, but you aren't taking from stdin are you? – 0x45 – 2018-04-11T20:29:05.487

@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 – 2018-04-11T20:30:45.357

Question updated. You might save some bytes now – 0x45 – 2018-04-11T20:39:59.647

@0x45 nah it's the same either way. Thank you, though. – Giuseppe – 2018-04-11T20:47:12.013

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 – 2018-04-11T22:55:04.347

@O.O.Balance ah, I see. Must have missed that a while back. I'll fix it. – Giuseppe – 2018-04-11T23:13:38.617

1

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

caird coinheringaahing

Posted 2018-04-11T20:16:25.073

Reputation: 13 702

Question updated. You might save some bytes now – 0x45 – 2018-04-11T20:40:18.083

@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 – 2018-04-11T20:41:30.713

1

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.

brhfl

Posted 2018-04-11T20:16:25.073

Reputation: 1 291

1

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.

Adám

Posted 2018-04-11T20:16:25.073

Reputation: 37 779

no eval required – l4m2 – 2018-04-11T21:21:43.500

@l4m2 Yeah I realized that after seeing this improvement to mine, and thanks to op order I was able to drop () too – Pandacoder – 2018-04-11T21:27:23.040

@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 – 2018-04-11T22:34:54.153

1

Julia, 23 bytes

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

Try it online!

dec appears to be shorter than repr or string.

Uriel

Posted 2018-04-11T20:16:25.073

Reputation: 11 708

1

Ruby, 14 bytes

p gets.to_i 16

Try it online!

Asone Tuhid

Posted 2018-04-11T20:16:25.073

Reputation: 1 944

1

APL (Dyalog), 13 bytes

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

Try it online!

Uriel

Posted 2018-04-11T20:16:25.073

Reputation: 11 708

16⊥⍞⍳⍨⎕D,⎕A as a full program prints to stdout and so doesn't require . – Adám – 2018-04-11T22:32:19.810

1

Japt, 4 bytes

Outputting as a string doubles the byte count!

nG s

Try it

Shaggy

Posted 2018-04-11T20:16:25.073

Reputation: 24 623

I had nG before the OP said we couldn't have quotes in the input. And the output has to be a string? -__-

– Oliver – 2018-04-12T00:42:56.563

1

><>, 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 +!

Jo King

Posted 2018-04-11T20:16:25.073

Reputation: 38 234

1

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

Neil

Posted 2018-04-11T20:16:25.073

Reputation: 95 035

1

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%

Neil

Posted 2018-04-11T20:16:25.073

Reputation: 95 035

1

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

milk

Posted 2018-04-11T20:16:25.073

Reputation: 3 043

1

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.

Kevin Cruijssen

Posted 2018-04-11T20:16:25.073

Reputation: 67 575

1

Pyth, 5 4 bytes

iz16

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

fortraan

Posted 2018-04-11T20:16:25.073

Reputation: 61

1Welcome to PPCG! – Steadybox – 2018-04-17T16:03:20.973

0

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

Wernisch

Posted 2018-04-11T20:16:25.073

Reputation: 2 534

0

Tcl, 20 bytes

proc D n {expr 0x$n}

Try it online!

sergiol

Posted 2018-04-11T20:16:25.073

Reputation: 3 055

0

Tcl, 19 bytes

puts [expr 0x$argv]

Try it online!

sergiol

Posted 2018-04-11T20:16:25.073

Reputation: 3 055

0

Coconut, 15 bytes

str..int$(?,16)

Try it online!

ovs

Posted 2018-04-11T20:16:25.073

Reputation: 21 408

0

Perl 5 -lp, 6 bytes

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

Try it online!

Ton Hospel

Posted 2018-04-11T20:16:25.073

Reputation: 14 114