1

I have done a basic arithmetic calculation and got the answer aa

Here is the Smalltalk code in Squeak:

Transcript show: 'aa='.
Transcript show: 23124234 * 431213; cr.

The output is aa=9971470315842

9971470315842 is a decimal number.

What can I do to make a 10-based number displayed based on any integer I want ??

Say, 55-based, 33-based, 2999-base. Any positive integer.

Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51
  • What do you mean by _any_ hex? Typing `*hex*` in the search box in the top-right corner of Sqeuak should give you methods related to hexadecimal printing and reading, if that is what you want. – JayK Oct 23 '17 at 05:41
  • 2
    If you mean to print numbers with any _base_ you want (i.e. hexadecimal = base 16, binary = base 2, etc) , search for `*print*base*` or only `*base*`. – JayK Oct 23 '17 at 05:45
  • Commonly used are binary, decimal, octal and hexadecimal which are 2-based, 10-based, 8-based, 16-based. Now I would like any ten-based numbers to be displayed in any other integer I like. Say, 5-based, 55-base, 191-based. – Sleeping On a Giant's Shoulder Oct 23 '17 at 05:50
  • I do not know the message names by heart and have no Squeak at hand atm, but have you tried the search as proposed? I think what you are searching for is `printStringBase:`, but I am unsure about the exact name of the message. – JayK Oct 23 '17 at 07:12
  • I think it will be a challenge finding a good representation for a base 2999 number. :) The radix limit for the `radix:` selector mentioned in the answer below is 45. – lurker Nov 06 '17 at 17:25

1 Answers1

5

In Squeak, you can use

12345 printStringBase: 17.
 '28C3'

You can also prepend the radix (base) and re-interpret the number

17r28C3.
 12345

The behavior is well defined up to base 36 (10 digits and 26 latin letters). Then you might get some output with more character codes, but you will not be able to re-interpret the numbers.

This also work with floating point numbers:

Float pi printStringBase: 5.
 '3.0323221430334324112412'

5r3.0323221430334324112412.
  3.141592653589793

Unfortunately, we can't reinterpret them above base 14 because there is an ambiguity with exponent letter e (once upon a time, only uppercase letters were used as digits, and there were no ambiguity).

Note that you can find methods with the MethodFinder tool.
Open a method finder and enter the receiver, arguments and expected results, for example with:

34. 17. '20'

the method finder will display 2 matching methods, 34 printStringBase: 17 --> '20' and 34 radix: 17 --> '20'

aka.nice
  • 9,100
  • 1
  • 28
  • 40