Convert string to number

-1

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

Okx

Posted 2016-09-01T16:36:56.147

Reputation: 15 025

Question was closed 2016-09-01T19:04:21.323

4

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 – 2016-09-01T16:42:42.343

1You 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? – James – 2016-09-01T16:45:08.217

1You don't need to, no. Anything that is decodable works. – Okx – 2016-09-01T16:46:20.033

1"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 – 2016-09-01T17:07:12.460

1what's stopping us just doing base36 conversion? – Blue – 2016-09-01T17:10:17.157

Can the input be empty? – Ian Chew – 2016-09-01T17:16:53.867

@muddyfish I think the fact that a string prepended with zeros returns the same output as the string - e.g. int("abc",36) == int("0abc",36) – Jonathan Allan – 2016-09-01T19:00:35.233

Answers

4

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)

Blue

Posted 2016-09-01T16:36:56.147

Reputation: 26 661

1This basically base-256 converts the string. – Lynn – 2016-09-01T17:24:01.727

It doesn't appear to be against the rules – Blue – 2016-09-01T17:25:52.387

1I'm not complaining :) – Lynn – 2016-09-01T17:32:25.790

1The decoding (as presented here) doesn't work for smaller inputs -- for example, input "hi" yields 26729, but input 26729 yields – AdmBorkBork – 2016-09-01T18:17:53.427

It's still probably possible to decode - pyth just seems to have a bug? – Blue – 2016-09-01T18:51:27.047

@muddyfish I think the problem is that if the integer is in the UTF-8 range, C converts to the character with that code point. Since the maximum UTF-8 code point is 1114111, any encoding that results in an integer less than or equal that number may fail upon decoding. I came up with jkm-d38CM for encoding and sCMm+d38sMcz2 for decoding, using the same idea as the PowerShell answer. – TheBikingViking – 2016-09-01T19:04:00.307

@muddyfish TheBikingViking has it right. Encoding with C should be fine, you just need a more-explicit base-256-to-string decoding algorithm than simply C. – AdmBorkBork – 2016-09-01T20:06:03.060

I'm not going to count this as correct because "ab" gets 24930, but 24930 gets 慢. – Okx – 2016-09-02T09:34:17.993

@Okx it is possible to decode those values, it just won't work with that function - if you want, I could write something that decodes it – Blue – 2016-09-02T09:36:53.820

@muddyfish alright, but alternatively you can edit your answer to explain how you would decode it. – Okx – 2016-09-02T09:39:44.480

@Okx that good? – Blue – 2016-09-02T09:46:05.937

2

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

cleblanc

Posted 2016-09-01T16:36:56.147

Reputation: 3 360

I think you have to take the input through stdin, not through input arguments. – Ian Chew – 2016-09-01T17:05:03.107

1How 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 – 2016-09-01T17:08:57.120

1Good point I should output 3 digits for each character – cleblanc – 2016-09-01T17:14:36.370

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 – 2016-09-01T18:33:04.700

1

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)

TuxCrafting

Posted 2016-09-01T16:36:56.147

Reputation: 4 547

1

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

AdmBorkBork

Posted 2016-09-01T16:36:56.147

Reputation: 41 581

Interesting. I was expecting an answer similar to this. – Okx – 2016-09-01T16:50:51.180

1

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.

Digital Trauma

Posted 2016-09-01T16:36:56.147

Reputation: 64 644

1

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!

betseg

Posted 2016-09-01T16:36:56.147

Reputation: 8 493

1

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!

Beta Decay

Posted 2016-09-01T16:36:56.147

Reputation: 21 478

1

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.

Lynn

Posted 2016-09-01T16:36:56.147

Reputation: 55 648

1Surely 1?.. would be a golfier regex? – Neil – 2016-09-01T18:31:49.587

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 – 2016-09-02T09:38:24.623

1Character 10 (a newline) is not in abcdefghijklmnopqrstuvwxyz1234567890. – Lynn – 2016-09-02T10:32:44.430