如何将负数转换为 Java 中的底数
How to convert a negative number to base in Java
我有一个负整数 n
有谁知道我可以在 Java 中转换为基数 P 吗?
假设 n= -31246
如何将其转换为基数 4?
试试这个。
Integer.toString(-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:
我有一个负整数 n
有谁知道我可以在 Java 中转换为基数 P 吗?
假设 n= -31246
如何将其转换为基数 4?
试试这个。
Integer.toString(-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: