简体   繁体   English

十进制到二进制格式的转换

[英]Decimal to Binary Format Conversion

I have to write a Java program as part of an assignment to convert decimal inputs to the following formats: unsigned binary, unsigned hex, signed-magnitude, 1's complement and 2's complement . 我必须编写Java程序作为赋值的一部分,以将十进制输入转换为以下格式: 无符号二进制,无符号十六进制,有符号幅度,1的补码和2的补码 The issue is that I am not allowed to use any of the built-in java components that would otherwise not make this so difficult. 问题是不允许我使用任何内置的Java组件,否则这些组件不会使它变得如此困难。 I've been working on this for a number of hours, and the last thing I wanted to do was come on here and ask for help, but I really am stumped here. 我已经为此工作了好几个小时,而我想做的最后一件事就是来这里寻求帮助,但是我真的很沮丧。 I do not expect, nor am I looking for anyone to complete my homework for me. 我没想到,我也没有寻找任何人为我完成家庭作业。 All I ask is for a nudge in the right direction. 我所要求的只是朝着正确方向前进。

The output/input of the program must be as follows: 程序的输出/输入必须如下:

Enter num bytes: 2

Enter number (or Q to quit): 4095

Input number=4095
    Unsigned binary = 0000 1111 1111 1111   (0x0fff)
    Signed-magnitude = 0000 1111 1111 1111
    One's complement = 0000 1111 1111 1111
    Two's complement = 0000 1111 1111 1111
    Excess 32768 = 1000 1111 1111 1111

Enter number (or Q to quit): -4095

Input number=-4095
    Unsigned binary = undefined
    Signed-magnitude = 1000 1111 1111 1111
    One's complement = 1111 0000 0000 0000
    Two's complement = 1111 0000 0000 0001
    Excess 32768 = 0111 0000 0000 0001

I have a good understanding of how to calculate these values, and I have worked out many of the algorithms accordingly. 我对如何计算这些值有很好的了解,因此我已经制定了许多算法。 The issue I'm having is that I don't know how to properly organize my classes to make this program efficient. 我遇到的问题是我不知道如何正确组织我的班级以使该程序高效。 Mostly, I'm getting confused by the instructions I've been given. 通常,我对收到的指示感到困惑。

They read as follows: 他们的内容如下:

Don't use the byte data type in Java. 不要在Java中使用字节数据类型。 We want to show the algorithms we use to do binary encodings. 我们想展示用于进行二进制编码的算法。 I created a simplistic BitString class. 我创建了一个简单的BitString类。 It's one field is an array of char's. 一个字段是char的数组。 It has methods like: BitString( numBytes), clear(), invert(), encodeUnsigned( num), setBit( pos, char). 它具有以下方法:BitString(numBytes),clear(),invert(),encodeUnsigned(num),setBit(pos,char)。

I don't understand why we would want to use a char array to store these bit values. 我不明白为什么我们要使用char数组来存储这些位值。 I've set this up so that the constructor in BitString accepts an argument for the number of bytes, multiplies that by 8 and creates a new char array using this number. 我进行了设置,以使BitString中的构造函数接受字节数的参数,将其乘以8,然后使用该数字创建一个新的char数组。 Then to obtain the unsigned binary value of the decimal, I've implemented the following: 然后,为了获得小数的无符号二进制值,我实现了以下内容:

String unsigned = "";
while(decimal > 0)
{
  unsigned = decimal%2 + unsigned;
  decimal = decimal >> 1;
}

I don't know what I'm supposed to do from here to be able to store these values in that array so that I can use it to calculate the other formats. 我不知道该怎么做才能将这些值存储在该数组中,以便可以使用它来计算其他格式。 I can't seem to be able to store integers as chars and I'm confused on how I am supposed to use that data structure to perform the other operations. 我似乎无法将整数存储为char,并且我对如何使用该数据结构执行其他操作感到困惑。 To complicate matters further, you'll notice that I need to pad the data so that it contains the correct number of bits. 为了进一步使事情复杂化,您会注意到我需要填充数据,以使其包含正确的位数。

If this were your problem, how would you solve it and how would you lay it out? 如果这是您的问题,您将如何解决?如何布置? I'm looking for the most fundamental solution that would be easy for a novice like me to understand. 我正在寻找最基本的解决方案,对于像我这样的新手来说很容易理解。 Thank you very much. 非常感谢你。

The instructions probably mean that, instead of 这些说明可能意味着

String unsigned = "";

The data would be more easily manipulated into other formats with something like (this is pseudo-code, I don't really know java): 数据将更容易通过其他类似的方式处理为其他格式(这是伪代码,我不太了解java):

char unsigned[numBytes * 8];

The array stores one bit per location. 阵列每个位置存储一位。

To do bitwise operations, you just iterate through the array and operate on each "bit." 要进行按位运算,您只需遍历数组并对每个“位”进行运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM