[英]Converting from binary to decimal from 2D int array in Java and storing variablse
I am trying to iterate through a 2D array of ints and change all of their values from binary form to decimal form and store those values. 我试图遍历int的2D数组,并将其所有值从二进制形式更改为十进制形式并存储这些值。 To be more clear: I have a bunch of different int values which are all technically in binary form but their values are stored as ints.
更清楚地说:我有一堆不同的int值,从技术上来说,它们都是二进制形式,但它们的值存储为int。 For instance, I might have a variable int example1 = 10001110 -- where the chars form a binary number but it's still a primitive int.
例如,我可能有一个变量int example1 = 10001110-字符组成一个二进制数,但它仍然是原始int。 So again, I need to convert those values from binary to decimal form and store them as ints.
同样,我需要将这些值从二进制转换为十进制形式,并将其存储为int。 To make your lives a bit easier/more straightforward: the array that currently contains the ints has 8 rows and 2 columns: all of whose entries are in binary int form.
为了使您的生活更轻松/更简单:当前包含int的数组具有8行和2列:所有条目均采用二进制int形式。
Thanks guys. 多谢你们。
遍历二进制字符串数组并将其转换,然后将其存储在新数组中。
Integer.parseInt(binaryString, 2);
To change binary form to decimal form you can use Integer.parseInt(someStringInteger, 2)
. 要将二进制形式更改为十进制形式,可以使用
Integer.parseInt(someStringInteger, 2)
。 To make someStringInteger
use Integer.parseInt(someInt)
. 要使
someStringInteger
使用Integer.parseInt(someInt)
。 For example 例如
//1110 (bin) -> 14 (dec)
System.out.println(Integer.parseInt(Integer.toString(1110), 2));//out -> 14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.