Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: binary conversion..

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default binary conversion..

    anyone can make me a simple program that converts decimal to binary and to decimal again..

    i just want to know how to make its structure in program...
    its not that easy. althoug i know the process of it and i cant do it it PAPER.

    and i will study its algorithm and then apply it in converting in hex and octal..

    please...

    im getting some hard time with this


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: binary conversion..

    Some time ago I wrote this in C++. You shouldn't have a problem reading the Syntax, if you do let me know.

    std::string conv(std::string number, int startBase, int endBase){
        if(startBase > 16 || endBase > 16) return "BASE ERROR";
        char NUMS[] = "0123456789ABCDEF";
        std::string result = "";
        int temp = 0, x;
        bool found = false;
        for(int i = 0; i < number.length(); i++){
            for(x = 0; x < startBase; x++){
                if(NUMS[x] == number[number.length()-(i+1)]){
                    found = true;
                    break;
                }
            }
            if(!found) return "NUMBER ERROR";
            temp += (x*power(startBase, i));
        }
        do{
            result.push_back(NUMS[temp%endBase]);
            temp /= endBase;
        }while(temp != 0);
        return std::string(result.rbegin(), result.rend());
    }
     
    long long power(int num, int pow){
        if(pow == 0) return 1;
        return num*power(num, pow-1);
    }

    Enjoy the studying of it

    Regards,
    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    chronoz13 (September 16th, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: binary conversion..

    im not really familiar with C .. can you please convert it into java... ?? ill study both of it ,in C and in Java

    sori for that....

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: binary conversion..

    public String conv(String number, int startBase, int endBase){
        if(startBase > 16 || endBase > 16) return "BASE ERROR";
        char NUMS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        String result = "";
        int temp = 0;
        int x;
        boolean found = false;
        for(int i = 0; i < number.length(); i++){
            for(x = 0; x < startBase; x++){
                if(NUMS[x] == number.charAt(number.length()-(i+1))){
                    found = true;
                    break;
                }
            }
            if(!found) return "NUMBER ERROR";
            temp += (x*power(startBase, i));
        }
        do{
            result += NUMS[temp%endBase];
            temp /= endBase;
        }while(temp != 0);
        return new StringBuilder(result).reverse();
    }
     
    public long power(int num, int pow){
        if(pow == 0) return 1;
        return num*power(num, pow-1);
    }

    I think i've made all the conversions...there are not many lol! I'm amazed you needed me to do it

    Regards,
    Chris
    Last edited by Freaky Chris; September 16th, 2009 at 09:36 AM.

  6. The Following User Says Thank You to Freaky Chris For This Useful Post:

    chronoz13 (September 16th, 2009)

  7. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: binary conversion..

    sori to bother you.. hehehe anyway tnx for that,!!..

    ah one more thing chris..


    what does java use? ASCII or UNICODE?

    i notice something using .read() method of InputStreamReader class

    once i input some numbers or characters it returns something like ASCII or UNICODE.
    im curious of what kind of character code does java use.

  8. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: binary conversion..

    It uses ASCII, read() returns an integer which represents the ASCII value for the character it has read. If you enter 'A' it will return 65 .

    At least as far as I'm aware, I don't have a compiler infront of me to test hehe!

    Regards,
    Chris

  9. The Following User Says Thank You to Freaky Chris For This Useful Post:

    chronoz13 (September 16th, 2009)

  10. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: binary conversion..

    hehehe.. tnx for the info chris!! hahah

  11. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: binary conversion..

    ah last chris.. the program that you gave was a hexadecimal conversion right? hehe

  12. #9
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: binary conversion..

    No, it will go from any base to any base, asuming it is a base below 16 and given in a String format.

    Chris

Similar Threads

  1. How to save statistics of a game in a binary file instead of txt file?
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 19th, 2009, 05:05 AM
  2. How to convert float and double data types to binary?
    By rosh72851 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2008, 10:45 PM