如何将二进制转换为十进制和八进制?

时间:2015-06-28 18:09:15

标签: binary decimal data-conversion octal

我有一个BINARY号码,我想把它转换成DECIMAL和OCTAL。

(0100 1111 1011 0010) 2

我知道如何将其转换为小数。但这个问题让我感到困惑。因为每4个数字中间有一个空格“0101 1111” 你能帮我理解这个问题。

由于

5 个答案:

答案 0 :(得分:2)

空间不是数字的一部分,只是为了让人们更容易阅读。从二进制到八进制的转换很简单。将二进制数字分组为3组(从右到左,向最左边的组添加额外的0,然后单独转换每个组。例如:

0100 1111 1011 0010 - > 100 111 110 110 010 - > 47662

答案 1 :(得分:2)

首先,请确保您要转换为十进制和八进制的数字实际上是二进制数字'而不是二进制编码十进制(BCD)'通常当数字被分组为4个二进制数字时,它代表BCD而不是二进制数字。

因此,一旦你确定它实际上是二进制而不是BCD,转换为十进制和八进制都是简单的步骤。

对于二进制到八进制,您将二进制数分组为3位数组,从最低有效位(LSB或最右侧)到最高有效位(MSB或最左侧)开始。如果在MSB上无法形成一组3位数,则添加前导零。

现在将每组数字从二进制转换为八进制:

(000) - > 0 (001) - > 1 。 。 (111) - > 7

最后将数字放在一起,然后将二进制转换为八进制。

如: - 二进制 - 00101101 分为2组: - > 000 101 101 - > 0 5 5 - > 55

二进制编码十进制之间的区别'和二元':

对于十进制数1248 二进制文件只是10011100000 但是,BCD将是 - > 0001 0010 0100 1000

答案 2 :(得分:1)

空间只是为了便于阅读。特别好,如果你试图将其转换为十六进制,因为4个二进制数字组成一个十六进制数字。

答案 3 :(得分:1)

Firstly, those spaces are for human readability; just delete them. Secondly, If this is not for a computer program, simply open up the windows calculator, go to view, and select programmer. Then chose the bin radio button and type in your number. the qword radio button should be selected. If it's for a program, I will need to know what language to help you.

答案 4 :(得分:0)

要快速将八进制转换为十进制,有两种方法。您实际上可以使用位移位进行实际计算。在编程时,应进行移位。

Example octal number = 147

Method one: From left to right.
Step 1: First digit is one. Take that times 8 plus 4. Got 12.
Step 2: Take 12 times 8 + 7. Got 103, and 103 is the answer.

您最终可以使用方法一将任何基数转换为基数10。

方法一是从字符串的左到右读取。使结果保持器进行计算。读取最左边的第一个数字时,会将其添加到结果值中。每次读取新的数字时,都将结果值乘以该数字的基数(对于八进制,则为8),然后将新数字的值添加到结果中。

Method 2, bitshift:
Octal Number: 147.

Step 1: 1 = 1(bin) = Shift << 3 = 1000(result value)
Step 2: 4 = 100(bin) + 1000(result value) = 1100(result value)
Step 3: 1100(result value) Shift << 3 = 1100000
Step 4: 7 = 111(bin) + 1100000(result value) = 1100111
Step 5: 1100111 binary is 103 decimal.

在编程循环中,您可以执行以下操作,并且操作速度很快。该代码非常简单,可以将其转换为任何编程语言。请注意,没有任何错误检查。

 for ( int i = 0; i < length; i++ ){
     c = (str.charAt(i) ^ 48);
     if ( c > 7 ) return 0; // <- if char ^ 48 > 7 then that is not a valid octal number.
     out = (out << 3) + c;
 }