Results 1 to 16 of 16
 09162010, 11:06 PM #1Member
 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.

Hello and welcome to the forum. Let's see what you've tried.
Luck
 09172010, 12:01 AM #3The binary number is to be entered in 16bits
 09172010, 01:22 AM #4Member
 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);
}
}
addUp = addUP +temp ;
exponent;
}
return addUp ;
}Last edited by Blueshocked; 09172010 at 01:24 AM.
 09172010, 01:41 AM #5Member
 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; 09172010 at 01:44 AM.
 09172010, 01:48 AM #6Member
 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.
 09172010, 01:59 AM #7temp = temp + (2*2);
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; 09172010 at 02:01 AM.
 09172010, 01:59 AM #8Member
 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 = WIDTH1; // 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;
 09172010, 02:04 AM #9Member
 Join Date
 Sep 2010
 Posts
 9
 Rep Power
 0
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 isLast edited by Blueshocked; 09172010 at 02:21 AM.
 09172010, 02:27 AM #10
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 ~.
 09172010, 02:52 AM #11Member
 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?
 09172010, 02:58 AM #12
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
 09172010, 03:37 AM #13Member
 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;
}
System.out.println("temp= "+temp+" addUp= "+addUp);
addUp=addUp+temp;
}
}
return addUp;
}
 09172010, 03:49 AM #14Member
 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 < ex1 ; j++){
temp = temp*2;
}
addUp=addUp+temp;
}
ex;
}
return addUp;
}
 09172010, 04:35 AM #15
Thanks for sharing your solution for others with the same dilemma. :)
 09172010, 12:49 PM #16
Similar Threads

characters from a string into an integer
By 2potatocakes in forum New To JavaReplies: 7Last Post: 05082012, 12:31 PM 
how to convert a string to an integer
By LAW in forum New To JavaReplies: 6Last Post: 11092009, 03:29 AM 
check if String is an integer?
By McChill in forum New To JavaReplies: 5Last Post: 05022009, 07:51 PM 
Integer to String
By zervine in forum Forum LobbyReplies: 3Last Post: 09122008, 12:07 PM 
String to Integer conversion
By eva in forum New To JavaReplies: 2Last Post: 12172007, 03:59 PM
Bookmarks