The Artima Developer Community
Sponsored Link

Java Answers Forum
Conversion from decimal to dotted decimal base 256 number

1 reply on 1 page. Most recent reply: Apr 29, 2003 10:18 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Courtenay

Posts: 23
Nickname: courtz
Registered: Apr, 2003

Conversion from decimal to dotted decimal base 256 number Posted: Apr 28, 2003 11:55 PM
Reply to this message Reply
Advertisement
I need to write a program that takes as its input a decimal number and produces as its output a dotted-decimal base 256 number.

I have the following algorithm but Im battling to convert to java. can anyone help?

int readValue(int base){
boolean negative;
char ch;
int number = 0;
do{
read (ch)
}while (ch <= ' ');
if (ch == '-'){
negative = true; read (ch);
}else{
negative = false;
if (ch == '+') { read(ch);}
}
while (value(ch),base){
number = number * base + value(ch);
read(ch);
}
if (negative) {
number = -number;
}
return number;
}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Conversion from decimal to dotted decimal base 256 number Posted: Apr 29, 2003 10:18 AM
Reply to this message Reply
It looks like you need to parse a String one character at a time and process.

This is only a guess at the conversion of your algorithm to java:

    public int readValue(int base, String s){
        boolean negative;
        char ch;
        int number = 0;
        int index = 0;
        do{
            //read (ch) 
            ch = s.charAt(index++);
        }while (ch <= ' ');
            if (ch == '-'){
                negative = true; 
                //read (ch) 
                ch = s.charAt(index++);
            }else{
                negative = false;
            if (ch == '+') { 
                //read (ch) 
                ch = s.charAt(index++);
            }
        }
        //while (value(ch),base){
        while (index < s.length()){
            //number = number * base + value(ch);
            number = number * base + (int)ch;
            //read (ch) 
            ch = s.charAt(index++);
        }
        if (negative) {
            number = -number;
        }
        return number;
    }

Flat View: This topic has 1 reply on 1 page
Topic: HELP: read in a line and stores it in another file Previous Topic   Next Topic Topic: Conversion from decimal to dotted decimal base 256 number

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use