-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.
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.2574Why 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.570Updated 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