java - Java中如何将负数转换为基数

标签 java radix

我有一个负整数n有人知道我可以在Java中转换为基数P吗?

假设n= -31246如何将其转换为基数4?

最佳答案

您可以使用 Integer.toString,

Integer.toString(i, 4);

根据文档,

public static String toString(int i, int radix)

Returns a string representation of the first argument in the radix specified by the second argument. If the radix is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX, then the radix 10 is used instead.

If the first argument is negative, the first element of the result is the ASCII minus character '-' ('\u002D'). If the first argument is not negative, no sign character appears in the result.

The remaining characters of the result represent the magnitude of the first argument. If the magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the magnitude will not be the zero character. The following ASCII characters are used as digits:

关于java - Java中如何将负数转换为基数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40164630/

相关文章:

c++ - vector 迭代的派生和基础之间的类型转换 : C2664 error

采用基数的 JavaScript 函数或方法?

java - Log4J:将 Stderr 消息重定向到主日志文件

java - Lucene - 默认搜索词形还原/词干提取

java - 静态方法文件路径

JAVA:如何使用paint()方法更新之前绘制的对象?

java - 单例的抽象基枚举/类

java - 尝试存储链表数组时出现 ArrayStoreException

c# - 有没有办法为重载运算符调用基方法? C#

python - 如何使用队列进行基数排序?