# Convert string to number [duplicate]

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

• 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. – El'endia Starman Sep 1 '16 at 16:42
• 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? – DJMcMayhem Sep 1 '16 at 16:45
• You don't need to, no. Anything that is decodable works. – Okx Sep 1 '16 at 16:46
• "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. – Digital Trauma Sep 1 '16 at 17:07
• what's stopping us just doing base36 conversion? – Blue Sep 1 '16 at 17:10

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

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

## C 696152 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

• I think you have to take the input through stdin, not through input arguments. – Ian Chew Sep 1 '16 at 17:05
• 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 – Digital Trauma Sep 1 '16 at 17:08
• Good point I should output 3 digits for each character – cleblanc Sep 1 '16 at 17:14
• 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). – milk Sep 1 '16 at 18:33

# 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.

## Ruby, 9737 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)

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

• Interesting. I was expecting an answer similar to this. – Okx Sep 1 '16 at 16:50

# S.I.L.O.S, 6360 55 bytes

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


Prints ASCII value.

Try it online!

# 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.

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

# 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!