# String to int *not allowed to use Integer

• 09-17-2010, 12:06 AM
Blueshocked
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.
• 09-17-2010, 12:36 AM
Fubarable
Hello and welcome to the forum. Let's see what you've tried.

Luck
• 09-17-2010, 01:01 AM
Norm
Quote:

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?
• 09-17-2010, 02:22 AM
Blueshocked
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 temp = 2;
int exponent = WIDTH - 1;
for (int i = 0 ; i < WIDTH ; i++){
if (exponent == 1){
}else{
if (exponent == 0){
}else{
for (int j = 0 ; j <= exponent ; j++){
temp = temp + (2*2);
}
}
exponent--;
}
}
• 09-17-2010, 02:41 AM
Blueshocked
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.
• 09-17-2010, 02:48 AM
Blueshocked
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.
• 09-17-2010, 02:59 AM
Norm
Quote:

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);
• 09-17-2010, 02:59 AM
Blueshocked
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 ){
}else{
if (ex == 1 ){
}else{
for (int j = 0 ; j <= ex ; j++){
temp = temp + (2*2);
}
}
ex--;
}
}
• 09-17-2010, 03:04 AM
Blueshocked
Quote:

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
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
• 09-17-2010, 03:27 AM
Norm
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 ~.
• 09-17-2010, 03:52 AM
Blueshocked
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?
• 09-17-2010, 03:58 AM
Norm
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
• 09-17-2010, 04:37 AM
Blueshocked
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;
}
}
}
}
• 09-17-2010, 04:49 AM
Blueshocked
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--;
}
}
• 09-17-2010, 05:35 AM
Zack
Thanks for sharing your solution for others with the same dilemma. :)
• 09-17-2010, 01:49 PM
Norm
Quote:

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