RU beehive logo ITEC dept promo banner
ITEC 122
2008fall
ibarland

homeinfolecturesexamsarchive

lect10c
arithmetic

----------
arithmetic:

What does a numeral mean?
  stoi10
  stoi2
  
  itos10()
  itos2()

  public static int stoi( String numeral, short base ) {
    int numSoFar = 0;
    int columnWeight = 1;  // Start at 1's column, then 10s,100s,... (if base==10)
    for (int i=0; i < numeral.length();  ++i ) {
      numSoFar += dtoi( numeral.charAt( numeral.length()-1-i ) ) * columnWeight;
      columnWeight *= base;
      }
    return numSoFar;
    }


  public static String itos( int n, short base ) {
    String numeralSoFar = "";
    int num = n;
    while (num > 0) {
      numeralSoFar = itod((short)(num % base)) + numeralSoFar;
      num /= base;
      }
    return numeralSoFar.isEmpty() ? "0" : numeralSoFar;
    }
See full code at Numeral.java (and NumeralTest.java). Converting between bases directly: base-10 and base-100 [where each digit is an ascii char] binary, octal, hex

homeinfolecturesexamsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Nov.17 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme