dxding
asked on
converts a string binary number to a decimal
import java.io.*;
import java.math.*;
class yk {
static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.i n));
static PrintWriter screen = new PrintWriter(System.out, true);
public static void main(String[] args) throws IOException {
String str;
char[] charArray;
String[] subs;
int[] q;
int count = 0;
char separator = ' ';
int index = 0;
index = 0;
int endIndex = 0;
int sum = 0;
int weight = 1;
screen.println("Try System");
screen.println("Enter a sentences");
screen.println();
str = keyboard.readLine();
//str = str.toLowerCase();
char[] array = new char [str.length()];
StringBuffer buf = new StringBuffer();
int dis = 1 << 7;
for ( int i = 0; i < str.length(); i++ ) {
if (str.length() % 2 == 1) {
for (int sh1=1; sh1<=8; sh1++)
buf.append('0');
}
array[i] = str.charAt(i);
for (int sh = 1; sh <= 8; sh++) {
buf.append(( array[i] & dis ) == 0 ? '0' : '1');
array[i] <<= 1;
}
i++;
if (i < str.length()) {
array[i] = str.charAt(i);
for (int sh2 = 1; sh2 <= 8; sh2++) {
buf.append(( array[i] & dis ) == 0 ? '0' : '1');
array[i] <<= 1;
}
}
buf.append(' '); // added for debug
}
String s = buf.toString();
System.out.println(s);
do
{
++count;
++index;
index = s.indexOf( separator, index );
}
while ( index != -1);
String[] subStr = new String[count];
index = 0;
for ( int i = 0; i < count; i++ )
{
endIndex = s.indexOf( separator, index );
if ( endIndex == -1 )
subStr[i] = s.substring ( index );
else
subStr[i] = s.substring( index, endIndex );
index = endIndex + 1;
}
//DISPLAY THE SUBSTRINGS
for ( int i = 0; i < subStr.length; i++ )
System.out.println(subStr[ i]);
//System.out.print(" " + Integer.parseInt(subStr[i] , 2));
}
}
At the final results, i want to convert each binary string to become a decimal but i getting some error message with this code: System.out.print(" " + Integer.parseInt(subStr[i] , 2)); Then each decimal with do some math calculation. For example math.pow(decimal, x) and mod by a value. Can some one help me to solve this probelm!!
import java.math.*;
class yk {
static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.i
static PrintWriter screen = new PrintWriter(System.out, true);
public static void main(String[] args) throws IOException {
String str;
char[] charArray;
String[] subs;
int[] q;
int count = 0;
char separator = ' ';
int index = 0;
index = 0;
int endIndex = 0;
int sum = 0;
int weight = 1;
screen.println("Try System");
screen.println("Enter a sentences");
screen.println();
str = keyboard.readLine();
//str = str.toLowerCase();
char[] array = new char [str.length()];
StringBuffer buf = new StringBuffer();
int dis = 1 << 7;
for ( int i = 0; i < str.length(); i++ ) {
if (str.length() % 2 == 1) {
for (int sh1=1; sh1<=8; sh1++)
buf.append('0');
}
array[i] = str.charAt(i);
for (int sh = 1; sh <= 8; sh++) {
buf.append(( array[i] & dis ) == 0 ? '0' : '1');
array[i] <<= 1;
}
i++;
if (i < str.length()) {
array[i] = str.charAt(i);
for (int sh2 = 1; sh2 <= 8; sh2++) {
buf.append(( array[i] & dis ) == 0 ? '0' : '1');
array[i] <<= 1;
}
}
buf.append(' '); // added for debug
}
String s = buf.toString();
System.out.println(s);
do
{
++count;
++index;
index = s.indexOf( separator, index );
}
while ( index != -1);
String[] subStr = new String[count];
index = 0;
for ( int i = 0; i < count; i++ )
{
endIndex = s.indexOf( separator, index );
if ( endIndex == -1 )
subStr[i] = s.substring ( index );
else
subStr[i] = s.substring( index, endIndex );
index = endIndex + 1;
}
//DISPLAY THE SUBSTRINGS
for ( int i = 0; i < subStr.length; i++ )
System.out.println(subStr[
//System.out.print(" " + Integer.parseInt(subStr[i]
}
}
At the final results, i want to convert each binary string to become a decimal but i getting some error message with this code: System.out.print(" " + Integer.parseInt(subStr[i]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanx for help :)
//DISPLAY THE SUBSTRINGS
for ( int i = 0; i < subStr.length; i++ )
System.out.println(subStr[
//System.out.print(" " + Integer.parseInt(subStr[i]
to
//DISPLAY THE SUBSTRINGS
for ( int i = 0; i < subStr.length; i++ ) {
// add the following two lines
if (subStr[i].equals(""))
break;
System.out.println(subStr[
//System.out.print(" " + Integer.parseInt(subStr[i]
}
And that should work.