# Thread: String to int *not allowed to use Integer

1. Member
Join Date
Sep 2010
Posts
9
Rep Power
0

## 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. 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?

3. Member
Join Date
Sep 2010
Posts
9
Rep Power
0
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);
}
}
exponent--;
}
}
Last edited by Blueshocked; 09-17-2010 at 01:24 AM.

4. Member
Join Date
Sep 2010
Posts
9
Rep Power
0
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.

5. Member
Join Date
Sep 2010
Posts
9
Rep Power
0
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.

6. 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.

7. Member
Join Date
Sep 2010
Posts
9
Rep Power
0
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);
}
}
ex--;
}
}

8. Member
Join Date
Sep 2010
Posts
9
Rep Power
0
Originally Posted by Norm
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.

9. 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 ~.

10. Member
Join Date
Sep 2010
Posts
9
Rep Power
0
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?

11. 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

12. Member
Join Date
Sep 2010
Posts
9
Rep Power
0
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;
}
}
}
}

13. Member
Join Date
Sep 2010
Posts
9
Rep Power
0
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;
}
}
ex--;
}
}

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

15. 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

#### Posting Permissions

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