Hi Benjamin,
from your example I guess you know how to convert from decimal (base 10) to
binary (base 2):
9 printStringBase: 2
gives you the binary representation/bit string: '1001'.
If I understand your post correctly you want the other way around and you want
to
get the decimal value for a bit pattern:
This is simple since Smalltalk is able to work with numbers in any arbitrary
base -
such a number is written BASErNUMBER, i.e. the base an 'r' followed by the
number.
So you can write 2r1001 in a workspace to represent the decimal 9
And to solve your problem just use:
|bitCombination|
bitCombination :='1001'.
('2r', bitCombination) asNumber
Note that finder would have give you the answer very simply. Just enter
'2r1001'. 9
in the finder field and finder will tell you to use #asNumber
This works with any abitrary base, for instance hexadecimal:
|htmlColorValue|
htmlColorValue:='FF0000'.
('16r', htmlColorValue) asNumber
The FUN thing is that Java is completely behind the moon when working
with other bases for literal numbers. In old version Java already had support
for
hexadecimal literals: 0xFF
Java also has octal representation using a leading 0 (zero) digit.
In current Java version 7 they now after years added binary number system using
0b1001:
http://docs.oracle.com/javase/7/docs/technotes/guides/language/binary-literals.html
So "x" is hexadecimal and "b" is now binary and octal using a leading 0.
Inconsistent and confusing!
BUT what if I'm an alien with 7 fingers and want to work with base 7 or other
bases?
In Smalltalk it is easy to represent:
7r12
no need to wait for a vendor or a new version.
Thats why I like Smalltalk: it's simple, consistent and yet powerfull!
Lesson learned: there are no stupid questions - but sometimes stupid language
designers around.
Bye
Torsten