# Reversing base numbers [closed]

Where input is binary, convert it to a decimal number (base 10), reverse the decimal number and output it. Vice versa for decimal (base 10) input: reverse it, convert it to binary and output the binary number.

Sample Input:

00000001
00000010
255
00011000


Sample Output

1
2
1000101000
42


other Sample Input

78
35
01011001
231


other Sample Output

01010111
00110101
98
10000100

• Welcome to CodeGolf.SE! You question seem to be underspecified (by which I mean I am not parsing "decimal reversed equivalent" at all), and you have not stated what contest is being proposed. If you mean this to be a code golf it should be tagged as such. Please consider having your questions reviewed at the meta SandBox or in the puzzle lab chat where we would be more than happy to help you hammer out these kinds of details before your task goes live. – dmckee --- ex-moderator kitten Jun 6 '11 at 17:40
• What happens if the input is a number which has an ambiguous base, e.g. 10000000 which could be interpreted as binary (32768 decimal) or decimal (1 million) ? – Paul R Jun 6 '11 at 17:53
• yes, may be we have to add some thing to recognize when is binary and when decimal :) some thing like d or b after the number idk :) – skylamer Jun 7 '11 at 4:51
• What about those leading zeroes actually? – pimvdb Jun 7 '11 at 20:15
• The problem remains inadequately specified. – dmckee --- ex-moderator kitten Jun 8 '11 at 0:04

puts$<.map{|i|i[/[^01\s]/]?"%08b"%i.reverse: i.to_i(2).to_s.reverse}  Uses stdin/stdout, interprets lines that consist only of 1s, 0s and whitespace as binary numbers, everything else as decimal. • this is working , but its just conversing the number from bin to dec... not reversing it :) – skylamer Jun 7 '11 at 5:55 • @skylamer: Fixed, I wasn't sure what exactly you meant with "reversing" before. A more detailed problem description with more details for the input/ouput specification would help preventing such misunderstandings. – Ventero Jun 7 '11 at 16:37 • So for now, pimvdb@js and ventero@ruby is the closer to the answer z – skylamer Jun 8 '11 at 16:24 # perl - 90 89 map{print reverse(/^[01]+$/?unpack"N",pack"B*",sprintf"%032d",$_:sprintf"%b",$_).$/}@ARGV  Any number containing only 1's and 0's is considered binary. Funny how so much more code is required for bin->dec vs. dec->bin... ## JavaScript, 140 199 bytes (function(x){s=x.split("\n"),r=b='';for(i in s)r+='\n'+(s[i].replace(/[01]*/g,b)? (s[i].split(b).reverse().join(b)-0).toString(2):(parseInt(s[i],2)+"") .split(b).reverse().join(b));return r.trim()})('1\n10\n255\n11000')  Excluding input. It requires, however, that there are no numbers present in the input which are base 10 and only have 1s / 0s in them. • may be , cuz when the input is dec... goes reversed in same and then out like a binary :) so 255 552 and 1000101000 – skylamer Jun 7 '11 at 4:55 • @primvdb: Apparently this is what the OP means by the confusing phrase "decimal reversed equivalent". It looks like all decimal input is to be text reversed before converting, and all binary after converting should be text reversed as well. – mellamokb Jun 7 '11 at 18:17 • @mellamokb: Oh I see, thanks. It now passes the testcases. – pimvdb Jun 7 '11 at 20:25 • So for now, pimvdb@js and ventero@ruby is the closer to the answer z – skylamer Jun 8 '11 at 16:24 • @skylamer: I don't have the leading zeroes yet, as I still don't see the way you'd like them to appear - should a binary number's length always be a multiple of 4? – pimvdb Jun 8 '11 at 16:59 # Python - 117 characters while 1: x=raw_input() if(not(False in map(lambda x:x in ("1","0"),s))):print int(x,2) else:print bin(int(x))[2:]  This golf relies heavily on the fact that python counts a single space as a full indentation. Note that this golf IS NOT ERROR-PROOF. While functional, it will throw an EOFError when there is no input. Protecting against this requires an extra 30 characters. # Changelog v0 - 120 chars v1 - 117 chars - changes "While True" to "While 1" Quite frankly, i still don't get the testcase for 255, i'll mend my solution when the OP corrects/expands the description. My Solution is based of rmckenzie ### Python - 84 bytes while 1: x=raw_input() print all(i<"2"for i in x)and int(x,2)or bin(int(x))[2::]  • all(i<"2"for...) – gnibbler Jun 8 '11 at 12:00 • @gnibbler, doh, so obvious... – st0le Jun 8 '11 at 12:32 ### bash 67 b(){ [[$1 =~ ^[01]*$]]&& s="i"||s="o";echo$s"base=2;\$1"|bc|rev;}

((1&":@#:@(10&#.@|.)(|.@":@#.))@.(0=[:+/1<[))".;' ',.1!:1[1
`