简体   繁体   English

如何将数字的二进制表示形式的Java链接列表转换为数字的十进制表示形式的字符串?

[英]How to convert a java linked list of a binary representation of a number to a String of the decimal representation of a number?

I've built in java a new number type that holds a representation of a binary number as a link list of bits. 我在Java中建立了一个新的数字类型,该数字类型将二进制数的表示形式表示为位的链接列表。 The toString of my class prints the binary form of this number, but I want to create a function that prints the decimal representation of such number, and in order to work around the limitations of the number types, I want it to be in string form. 我班的toString打印此数字的二进制形式,但我想创建一个打印该数字的十进制表示形式的函数,并且为了解决数字类型的限制,我希望它采用字符串形式。 The problem is that i can't ever put the full number in an int or a long because my number type can hold a whole number of any size. 问题是我无法将完整数字放入int或long中,因为我的数字类型可以容纳任意大小的整数。 If someone needs to see the class or parts of it in order to help i can send it to them. 如果有人需要查看课程或课程的一部分以帮助我可以将其发送给他们。

Look into BigInteger . 查看BigInteger As your data structure allows for storing binary numbers of arbitrary size, BigInteger allows for storing integer numbers of arbitrary size. 由于您的数据结构允许存储任意大小的二进制数,因此BigInteger允许存储任意大小的整数。

As you are able to convert your list of bits to String , you might want to use this constructor public BigInteger​(String val, int radix) . 由于您可以将位列表转换为String ,因此可能需要使用此构造函数public BigInteger(String val,int radix) For example, let's say an object yourObject of YourClass holds a list of bits 1 0 1 1 0, which prints out as "10110" using the toString method you have implemented. 例如,假设一个对象yourObjectYourClass持有位1 0 1 1 0列表,打印出的“10110”使用toString你已经实现的方法。 Now: 现在:

YourClass yourObject; // 10110
BigInteger bi = new BigInteger(yourObject.toString(), 2);
System.out.println(bi); // Prints out 22

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

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