我有一个负整数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/