-1
\$\begingroup\$

This question already has an answer here:

Task

The task is to convert a string to a number. It must accept the string via stdin and output the number via stdout.

You convert a string to a number by replacing each letter of the string by its index (mind that you don't need to replace it with the index, that's just an example. You are allowed to replace it with anything you like, as long as the output is decodable) in the following list (you don't need to convert any other characters):

Characters: abcdefghijklmnopqrstuvwxyz1234567890 (the input will only either be lowercase letters, or/and a number).

Additionally, you must make the output decodable, so if you have output 0 for input a, and somebody inputs bl, then the output will be something like 111. This is not decodable since the program might interpret it as bbb or lb or bl. You might make it decodable by making sure the the number that each character outputs always has a length of 2. Any other way of making sure your output is decodable is allowed, but you must explain how one would decode the output in your answer.

Standard loopholes are not allowed. This is code golf, so shortest code wins!

EDIT: By decodable, just to clarify, I mean that you can convert the output back into the input. Your program doesn't need to feature decoding features, only encoding.

Examples

Input    Output
a        00
ab       0001
1        26
\$\endgroup\$

marked as duplicate by Peter Taylor code-golf Sep 1 '16 at 19:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    \$\begingroup\$ Welcome to PPCG! This is not a bad challenge, especially as your first, but I suggest using the Sandbox to get feedback on your future challenges before you post them. \$\endgroup\$ – El'endia Starman Sep 1 '16 at 16:42
  • 1
    \$\begingroup\$ You much convert a string to a number by replacing each letter of the string by its index (mind that you don't need to replace it with the index, that's just an example. You are allowed to replace it with anything you like, as long as the output is decodable) What exactly does that mean? Are we suppose to replace each letter of the string by its index or not? \$\endgroup\$ – DJMcMayhem Sep 1 '16 at 16:45
  • 1
    \$\begingroup\$ You don't need to, no. Anything that is decodable works. \$\endgroup\$ – Okx Sep 1 '16 at 16:46
  • 1
    \$\begingroup\$ "It must accept the string via stdin and output the number via stdout." Any reason for this restriction? Commonly any I/O methods appropriate to the given language is acceptable, which would include e.g. command-line args in many cases. \$\endgroup\$ – Digital Trauma Sep 1 '16 at 17:07
  • 1
    \$\begingroup\$ what's stopping us just doing base36 conversion? \$\endgroup\$ – Blue Sep 1 '16 at 17:10
4
\$\begingroup\$

Pyth, 1 byte

C

Try it here!

Converts a string to a number

Decode with the following Python code:

def decode(x):
    rest, char = divmod(x, 256)
    if rest < 256: return chr(rest)+chr(char)
    return decode(rest)+chr(char)
\$\endgroup\$
  • 1
    \$\begingroup\$ This basically base-256 converts the string. \$\endgroup\$ – Lynn Sep 1 '16 at 17:24
  • \$\begingroup\$ It doesn't appear to be against the rules \$\endgroup\$ – Blue Sep 1 '16 at 17:25
  • 1
    \$\begingroup\$ I'm not complaining :) \$\endgroup\$ – Lynn Sep 1 '16 at 17:32
  • 1
    \$\begingroup\$ The decoding (as presented here) doesn't work for smaller inputs -- for example, input "hi" yields 26729, but input 26729 yields \$\endgroup\$ – AdmBorkBork Sep 1 '16 at 18:17
  • \$\begingroup\$ It's still probably possible to decode - pyth just seems to have a bug? \$\endgroup\$ – Blue Sep 1 '16 at 18:51
2
\$\begingroup\$

C 69 61 52 62 Bytes

i;char v[99];main(){gets(v);while(v[i])printf("%.3d",v[i++]);}

Now taking input from stdin, each character code is 3 digits.

a.exe thisisatest

116104105115105115097116101115116

\$\endgroup\$
  • \$\begingroup\$ I think you have to take the input through stdin, not through input arguments. \$\endgroup\$ – Ian Chew Sep 1 '16 at 17:05
  • 1
    \$\begingroup\$ How does the output look for input of 1? I think it will be 49 i.e. only 2 chars long, which makes decoding harder \$\endgroup\$ – Digital Trauma Sep 1 '16 at 17:08
  • 1
    \$\begingroup\$ Good point I should output 3 digits for each character \$\endgroup\$ – cleblanc Sep 1 '16 at 17:14
  • \$\begingroup\$ I think you can do some as 2 chars and others as 3. If it starts with 1 (d-z) it'll be 3 chars, otherwise it's 2 chars (0-9,a-c). \$\endgroup\$ – milk Sep 1 '16 at 18:33
1
\$\begingroup\$

Bash + coreutils, 20

od -vbAn|tr -d ' \n'

The index of each character in the ASCII table is given in octal by od, which ensures each octal number for each input character is exactly 3 digits long (padded with leading zeros as necessary). tr removes the whitespace.

Decoding would be performed by grouping output into groups of 3 digits and then conversion from octal back to ASCII.

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

Ruby, 97 37 44 bytes

lambda{|s|s.chars.map{|c|c.upcase.ord}.join}

Take the ordinal of each character in s converted to uppercase (to ensure 2 chars per number for ordinals)

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

PowerShell v2+, 42 32 bytes

-join([char[]]$args[0]|%{$_-38})

Takes input $args[0], treats it as a char-array, sends it into a loop, and each loop cast the char as an int (implicit) minus 38. Those are all encapsulated in parens and -joined into one string. That string is left on the pipeline and printing is implicit.

The lowest ASCII point of abcdefghijklmnopqrstuvwxyz0123456789 is 0, with 48. The highest is z with 122. By subtracting 38 from each code point, we're guaranteed distinct two-digit number for each input character.

Examples

PS C:\Tools\Scripts\golfing> .\convert-string-to-number.ps1 'ab'
5960

PS C:\Tools\Scripts\golfing> .\convert-string-to-number.ps1 'ppcg'
74746165

PS C:\Tools\Scripts\golfing> .\convert-string-to-number.ps1 'error404'
6376767376141014

PS C:\Tools\Scripts\golfing> .\convert-string-to-number.ps1 'abcdefghijklmnopqrstuvwxyz0123456789'
596061626364656667686970717273747576777879808182838410111213141516171819
\$\endgroup\$
  • \$\begingroup\$ Interesting. I was expecting an answer similar to this. \$\endgroup\$ – Okx Sep 1 '16 at 16:50
1
\$\begingroup\$

S.I.L.O.S, 63 60 55 bytes

loadLine
b=256
lbla
c=get b
printIntNoLine c
b+1
if c a

Prints ASCII value.

Try it online!

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

GS2, 1 byte

Byte 14, which, in CP437, is .

Try it online!

Decoding is possible: get all matches of the regex (1..|..) and map them from decimal ASCII code points to ASCII characters.

\$\endgroup\$
  • 1
    \$\begingroup\$ Surely 1?.. would be a golfier regex? \$\endgroup\$ – Neil Sep 1 '16 at 18:31
  • \$\begingroup\$ Decoding isn't possible, because 'e0' gets 10148. Attempting to decode that can lead to a number of solutions (10 & 148 or 101 & 48). \$\endgroup\$ – Okx Sep 2 '16 at 9:38
  • 1
    \$\begingroup\$ Character 10 (a newline) is not in abcdefghijklmnopqrstuvwxyz1234567890. \$\endgroup\$ – Lynn Sep 2 '16 at 10:32
1
\$\begingroup\$

Python 3, 86 bytes

I appear to have misunderstood what the question allows... Either way, this exactly matches the test cases given.

print(''.join('%02d'%'abcdefghijklmnopqrstuvwxyz0123456789'.index(i)for i in input()))

Ideone it!

\$\endgroup\$

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