views:

34

answers:

5

Hello, I know that this isn't exactly a programming question, but it's related to the subject for me. How do you denote different numeral systems in just text? (By text I mean able to type at a proper speed and not copy-pasting it from another program.) For example if i have a number in base 2 how do I type it so others can understand that it's a base 2 number. On paper you can do something like (1001)2 where 2 is a small index. Is there some specific symbol that you have to type before the 2 so that others understand it as subscript? (Exponentiation uses the symbol ^ for this.) Or is it all just random and no standard exists in it?

A: 

I think a lot of this is going to depend on how/why/what you want to do with the data in the end.

For me, the ^ is logical to use, as it is quick to type, BUT it could be interpreted incorrectly by someone unfamiliar with the notation.

Mitchel Sellers
+2  A: 

A convention in many programming languages is

0b1001

where the "b" indicates binary. Other conventions include starting with 0x for hexadecimal and starting with just 0 (followed by other digits) for octal.

JacobM
A: 

For hex, you'd prefix it with 0x.

0xFF

For Binary, a 0b

0b101

For Octal, a 0o

0o44

Alternatively, be more explicit?

dec(123)
hex(0AF)
bin(101)
oct(111)
Chad
A: 

The convention I've seen is that, just as a caret indicates superscript (as for exponentiation), an underscore indicates subscript. So the most "literal" way to translate your example to ASCII would be 1001_2. I agree with JacobM, though; the 0b prefix is unambiguous and unlikely to be misunderstood.

Thom Smith
Thank you. Thanks to all of you. The question was more about mathematical notation rather than programming itself. I just needed a way the user to enter different numeral systems in a way that is convenient and is generally used.
Johnny
A: 

The notations for the base vary depending on the programming language (as I was saying in a comment to this question).

  • Decimal notation tends to be the default (no specific notation).
  • Hexadecimal tends to be prefixed with # or 0x or use the h suffix: 0xA0, #A0 or A0h.
  • Some languages use a 0 prefix to represent octal notation (e.g. 0777).
  • Binary notation is less common (often hexadecimal is used instead, as it makes the notation a bit more compact and ends up using the right padding for octets). You could however use 0b1010 or 1010b. Again, this depends on the language and whether the compiler supports it.

All of the above depend on the programming language. If it's just for writing in text, either of these conventions should do. Of course, if you're writing a book don't change the convention you're using in the middle...

Bruno