Java 如何将二进制字符串值转换为十进制
如何转换二进制字符串,例如Java 如何将二进制字符串值转换为十进制,java,Java,如何转换二进制字符串,例如 String c = "110010"; // as binary Java中的十进制值?(示例中的预期结果为50)使用Integer.parseInt(请参阅),使用基数2将字符串转换为int: int decimalValue = Integer.parseInt(c, 2); 我想你在找我。第二个参数采用基数,在本例中为2 Integer.parseInt(c, 2) public静态整型整数二进制(String str){ 双j=0; 对于(int i=
String c = "110010"; // as binary
Java中的十进制值?(示例中的预期结果为50)使用
Integer.parseInt
(请参阅),使用基数2将字符串
转换为int
:
int decimalValue = Integer.parseInt(c, 2);
我想你在找我。第二个参数采用基数,在本例中为2
Integer.parseInt(c, 2)
public静态整型整数二进制(String str){
双j=0;
对于(int i=0;i
或
测试一下
import java.util.Scanner;
public class BinaryToDecimal{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int binaryNumber = 0;
int counter = 0;
int number = 0;
System.out.print("Input binary number: ");
binaryNumber = input.nextInt();
//it's going to stop when the binaryNumber/10 is less than 0
//example:
//binaryNumber = 11/10. The result value is 1 when you do the next
//operation 1/10 . The result value is 0
while(binaryNumber != 0)
{
//Obtaining the remainder of the division and multiplying it
//with the number raised to two
//adding it up with the previous result
number += ((binaryNumber % 10)) * Math.pow(2,counter);
binaryNumber /= 10; //removing one digit from the binary number
//Increasing counter 2^0, 2^1, 2^2, 2^3.....
counter++;
}
System.out.println("Decimal number : " + number);
}
}公共静态void main(字符串[]args){
java.util.Scanner scan=新的java.util.Scanner(System.in);
长小数=0;
System.out.println(“请输入一个正二进制数。(只有1s和0s)”;
//这会将输入读取为字符串,并将每个符号拆分为
//数组列表
String元素=scan.nextLine();
String[]数组=element.split(“”);
//这将根据实际的阵列数为整数阵列指定长度
//输入的符号
int[]numberSplit=newint[array.length];
int position=array.length-1;//将起始位置设置为数组的结尾
//这将字符串数组转换为整数数组
for(int i=0;i
必须考虑十进制精度,因此必须限制位字符串长度。无论如何,使用BigDecimal是一个不错的选择
public BigDecimal bitStringToBigDecimal(String bitStr){
BigDecimal sum = new BigDecimal("0");
BigDecimal base = new BigDecimal(2);
BigDecimal temp;
for(int i=0;i<bitStr.length();i++){
if(bitStr.charAt(i)== '1'){
int exponent= bitStr.length()-1-i;
temp=base.pow(exponent);
sum=sum.add(temp);
}
}
return sum;
}
public BigDecimal bitStringToBigDecimal(字符串bitStr){
BigDecimal总和=新的BigDecimal(“0”);
BigDecimal基数=新的BigDecimal(2);
大十进制温度;
对于(int i=0;i测试此项,您将发现代码中有一行包含Scan.S()。此行仅用于存储数据(字符串)。
那么就试试这个:
PS:别忘了将文件另存为bindec
import java.io.*;
class Scan
{
public static String S()
{
String x;
char c;
boolean erreur;
do
{
x = "";
erreur = false;
try
{
while((c = (char)System.in.read()) != '\n')
{
if (c != '\r')
{
x += c;
}
}
}
catch(IOException e)
{
System.out.print(" > enter String : ");
erreur = true;
}
} while(erreur);
return x;
}
public class bindec{
public static void main (String[] args) {
int b=0;
String a;
System.out.println("bin: ");
a = Lire.S();
int j=a.length()-1;
for(int i=0;i<a.length() ;i++ ){
if(a.charAt(i)=='1'){
b += Math.pow(2,j);
}
if(a.charAt(i)=='0'){
b+=0;
}
j=j-1;
}
System.out.println("dec: "+b);
}
}
import java.io.*;
类扫描
{
公共静态字符串S()
{
字符串x;
字符c;
布尔误差;
做
{
x=“”;
错误=错误;
尝试
{
而((c=(char)System.in.read())!='\n')
{
如果(c!='\r')
{
x+=c;
}
}
}
捕获(IOE异常)
{
System.out.print(“>输入字符串:”);
错误=正确;
}
}while(erreur);
返回x;
}
公共类bindec{
公共静态void main(字符串[]args){
int b=0;
字符串a;
System.out.println(“bin:”);
a=里拉·S();
int j=a.长度()-1;
用于(int i=0;iint base2(字符串位){
int ans=0;
对于(int i=bits.length()-1,f=1;i>=0;i--){
ans+=f*(位字符(i)-“0”);
f根据这一点:
我们可以编写以下帮助函数:
public static int convertBinaryToDecimal(String str) {
int result = 0;
for (int i = 0; i < str.length(); i++) {
int value = Character.getNumericValue(str.charAt(i));
result = result * 2 + value;
}
return result;
}
public static int convertBinaryToDecimal(字符串str){
int结果=0;
对于(int i=0;i
请不要发布您尚未尝试解决的问题。只想分享我的解决方案:)您可以通过以下方式进行测试:str=“1000001010010010000010111100110000000000000001010111101”;
public static Long binToDec(String bin) {
long dec = 0L;
long pow = 1L;
for (int i = (bin.length() - 1); i >= 0; i--) {
char c = bin.charAt(i);
dec = dec + (Long.parseLong(c + "") * pow);
pow = pow * 2;
}
return dec;
}
long num = Long.parseLong("101110111",2);
import java.util.Scanner;
public class BinaryToDecimal{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int binaryNumber = 0;
int counter = 0;
int number = 0;
System.out.print("Input binary number: ");
binaryNumber = input.nextInt();
//it's going to stop when the binaryNumber/10 is less than 0
//example:
//binaryNumber = 11/10. The result value is 1 when you do the next
//operation 1/10 . The result value is 0
while(binaryNumber != 0)
{
//Obtaining the remainder of the division and multiplying it
//with the number raised to two
//adding it up with the previous result
number += ((binaryNumber % 10)) * Math.pow(2,counter);
binaryNumber /= 10; //removing one digit from the binary number
//Increasing counter 2^0, 2^1, 2^2, 2^3.....
counter++;
}
System.out.println("Decimal number : " + number);
}
private static int convertBinaryToDecimal(String strOfBinary){
int flag = 1, binary=0;
char binaryOne = '1';
char[] charArray = strOfBinary.toCharArray();
for(int i=charArray.length-1;i>=0;i--){
if(charArray[i] == binaryOne){
binary+=flag;
}
flag*=2;
}
return binary;
}
public static void main(String[] args) {
java.util.Scanner scan = new java.util.Scanner(System.in);
long decimalValue = 0;
System.out.println("Please enter a positive binary number.(Only 1s and 0s)");
//This reads the input as a String and splits each symbol into
//array list
String element = scan.nextLine();
String[] array = element.split("");
//This assigns the length to integer arrys based on actual number of
//symbols entered
int[] numberSplit = new int[array.length];
int position = array.length - 1; //set beginning position to the end of array
//This turns String array into Integer array
for (int i = 0; i < array.length; i++) {
numberSplit[i] = Integer.parseInt(array[i]);
}
//This loop goes from last to first position of an array making
//calculation where power of 2 is the current loop instance number
for (int i = 0; i < array.length; i++) {
if (numberSplit[position] == 1) {
decimalValue = decimalValue + (long) Math.pow(2, i);
}
position--;
}
System.out.println(decimalValue);
main(null);
}
public BigDecimal bitStringToBigDecimal(String bitStr){
BigDecimal sum = new BigDecimal("0");
BigDecimal base = new BigDecimal(2);
BigDecimal temp;
for(int i=0;i<bitStr.length();i++){
if(bitStr.charAt(i)== '1'){
int exponent= bitStr.length()-1-i;
temp=base.pow(exponent);
sum=sum.add(temp);
}
}
return sum;
}
import java.util.*;
public class BinaryToDecimal
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the binary number");
double s=sc.nextDouble();
int c=0;
long s1=0;
while(s>0)
{
s1=s1+(long)(Math.pow(2,c)*(long)(s%10));
s=(long)s/10;
c++;
}
System.out.println("The respective decimal number is : "+s1);
}
}
import java.io.*;
class Scan
{
public static String S()
{
String x;
char c;
boolean erreur;
do
{
x = "";
erreur = false;
try
{
while((c = (char)System.in.read()) != '\n')
{
if (c != '\r')
{
x += c;
}
}
}
catch(IOException e)
{
System.out.print(" > enter String : ");
erreur = true;
}
} while(erreur);
return x;
}
public class bindec{
public static void main (String[] args) {
int b=0;
String a;
System.out.println("bin: ");
a = Lire.S();
int j=a.length()-1;
for(int i=0;i<a.length() ;i++ ){
if(a.charAt(i)=='1'){
b += Math.pow(2,j);
}
if(a.charAt(i)=='0'){
b+=0;
}
j=j-1;
}
System.out.println("dec: "+b);
}
}
int base2(String bits) {
int ans = 0;
for (int i = bits.length() - 1, f = 1; i >= 0; i--) {
ans += f * (bits.charAt(i) - '0');
f <<= 1;
}
return ans;
}
public static int convertBinaryToDecimal(String str) {
int result = 0;
for (int i = 0; i < str.length(); i++) {
int value = Character.getNumericValue(str.charAt(i));
result = result * 2 + value;
}
return result;
}