Results 1 to 16 of 16
  1. #1
    Blueshocked is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default String to int *not allowed to use Integer

    I am trying to convert a binary number that enters the method as a string, I have to do the actual conversion within the same method, and it returns as an int. Right now I was thinking of running a loop and pulling out each character one by one as a character, but seeing as I have been away from java (and programming in general) for awhile I find myself stuck. The binary number is to be entered in 16bits so I will need to be able to ignore all the zeros before the first 1. I am not allowed to use any classes such as Integer.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    The binary number is to be entered in 16bits
    Do you mean the String will be 16 characters (consisting of the digits:0 or 1) long?

  4. #4
    Blueshocked is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    Norm: Do you mean the String will be 16 characters (consisting of the digits:0 or 1) long?
    yes 16 characters long for example: 0000001100110011

    I thought it through again.. and so far i have come up with this, I've never tried to do exponents like this so its just a guess atm.

    i should be able to tell if it is a 1 or 0 with charAt() i think, and use a counter that reduces for each cycle of a loop for the exponent

    i haven't had a chance to run this through the compiler yet, headed back to the computer lab now to try(forgot to mention i have to do it in linux, though i doubt that would change anything).

    private static final int WIDTH = Driver.WIDTH; // this should start as 16
    public static int binaryToDecimalUnsigned(String binary) {
    int addUp = 0;
    int temp = 2;
    int exponent = WIDTH - 1;
    for (int i = 0 ; i < WIDTH ; i++){
    if (exponent == 1){
    addUp = addUp + (2 * binary.charAt(i));
    }else{
    if (exponent == 0){
    addUp = addUp + (1 * binary.charAt(i));
    }else{
    for (int j = 0 ; j <= exponent ; j++){
    temp = temp + (2*2);
    }
    }
    addUp = addUP +temp ;
    exponent--;
    }
    return addUp ;
    }
    Last edited by Blueshocked; 09-17-2010 at 01:24 AM.

  5. #5
    Blueshocked is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    I think I noticed a problem with where and what value I initialized the variable 'temp'

    I changed to creating temp within the first loop and giving it a starting value of 0.
    Last edited by Blueshocked; 09-17-2010 at 01:44 AM.

  6. #6
    Blueshocked is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    well it compiles, and seems to work correctly with the exception that the decimal value i recieve is WAY off, so i need to work on the calculation part yet.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    temp = temp + (2*2);
    What is this statement supposed to do?

    You need to add some print outs for all the variables you are using to show how they change as the code executes.
    For example add this right after the for statement:
    System.out.println("i=" + i + ", temp=" + temp);
    Last edited by Norm; 09-17-2010 at 02:01 AM.

  8. #8
    Blueshocked is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    current code for binary to decimal conversion, my math seems to be off, i enter 0000000000111010 (58), but it returns 678

    public static int binaryToDecimalUnsigned(String binary) {
    int addUp=0; // will return the final decimal value
    int ex = WIDTH-1; // provides an exponent value for calculation
    for (int i = 0; i < WIDTH ; i++){
    int temp = 0; // the value to be added to addUP
    if (ex == 0 ){
    addUp = addUp + (1 * binary.charAt(i));
    }else{
    if (ex == 1 ){
    addUp = addUp + (2 * binary.charAt(i));
    }else{
    for (int j = 0 ; j <= ex ; j++){
    temp = temp + (2*2);
    }
    }
    addUp = addUp + temp;
    ex--;
    }
    }
    return addUp;

  9. #9
    Blueshocked is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What is this statement supposed to do?
    that is me being an idiot lol, that looks like the problem im still having

    I was attempting to do

    temp = (1 or 0) * 2^n
    to get the amount to add to the addUp variable
    Not sure what i was thinking when i first wrote temp = temp + (2*2)

    I did a System.out.println and it seems that every time the first loop runs it adds 52 to my total regardless of where in the binary number it is, or whether the digit is
    Last edited by Blueshocked; 09-17-2010 at 02:21 AM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    Ok on to the next lesson.
    Print out the decimal/int value of the char '1'. You will see that it is not 1.
    To understand the int values of characters look up the ASCII character table. There are about 90+ characters you can type in with a keyboard. They each have a different int value ranging from 32 for space to 126 for ~.

  11. #11
    Blueshocked is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    ah, i hadn't thought of that, would it be better to run a loop that would create an array and fill it with ones and zeros stored as int, then after that is done, run the calculation? and use binary.charAt() only to assign the array location a zero or one with an if?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    There are several ways to convert a '1' to 1. Look at the Character class for some.
    There is also a quick and dirty way that assumes that the characters '0' thru '9' are contiguous in sequence of values. Given that, if you subtract '0' from the character the results will be the int value of the character. For example '2' - '0' = 2

  13. #13
    Blueshocked is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    i started the method over and this is where i am at, the array is filling properly, but i haven't finished the math portion yet

    public static int binaryToDecimalUnsigned(String binary) {
    int addUp=0; // will return the final decimal value
    int ex = WIDTH; // provides an exponent value for calculation
    int[] binaryArray = new int[WIDTH];
    for (int i = 0; i < WIDTH ; i++){
    char c = 0;
    int h = binary.charAt(i) - c;
    //System.out.println(" h= "+h+" ");
    if (h == 48){
    binaryArray[i] = 0;
    }else{
    binaryArray[i] = 1;
    }
    }
    //for (int i = 0; i < WIDTH ; i++){
    // System.out.println(binaryArray[i]+" ");
    //}
    for (int i = 0; i < WIDTH ; i++){
    int temp = 2; // the value to be added to addUP
    if(binaryArray[i] == 1){
    for (int j = 0 ; j < ex ; j++){
    temp = temp*2;
    }
    System.out.println("temp= "+temp+" addUp= "+addUp);
    addUp=addUp+temp;
    }
    }
    return addUp;
    }

  14. #14
    Blueshocked is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    I have it working properly now, this is the final code I ended up with.

    public static int binaryToDecimalUnsigned(String binary) {
    int addUp=0; // will return the final decimal value
    int ex = WIDTH; // provides an exponent value for calculation
    int[] binaryArray = new int[WIDTH];
    for (int i = 0; i < WIDTH ; i++){
    char c = 0;
    int h = binary.charAt(i) - c;
    //System.out.println(" h= "+h+" ");
    if (h == 48){
    binaryArray[i] = 0;
    }else{
    binaryArray[i] = 1;
    }
    }
    //for (int i = 0; i < WIDTH ; i++){
    // System.out.println(binaryArray[i]+" ");
    //}
    for (int i = 0; i < WIDTH ; i++){
    int temp = 1; // the value to be added to addUP
    if(binaryArray[i] == 1){
    for (int j = 0 ; j < ex-1 ; j++){
    temp = temp*2;
    }
    addUp=addUp+temp;
    }
    ex--;
    }
    return addUp;
    }

  15. #15
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Thanks for sharing your solution for others with the same dilemma. :)

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    if (h == 48){
    A better more self documenting way to code this would be:
    if (h == '0'){

    Not everyone knows that the value of '0' is 48 (although they should)

    char c = 0;
    int h = binary.charAt(i) - c; // why subtract 0 from it? It does nothing useful

Similar Threads

  1. characters from a string into an integer
    By 2potatocakes in forum New To Java
    Replies: 7
    Last Post: 05-08-2012, 12:31 PM
  2. how to convert a string to an integer
    By LAW in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 03:29 AM
  3. check if String is an integer?
    By McChill in forum New To Java
    Replies: 5
    Last Post: 05-02-2009, 07:51 PM
  4. Integer to String
    By zervine in forum Forum Lobby
    Replies: 3
    Last Post: 09-12-2008, 12:07 PM
  5. String to Integer conversion
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 03:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •