Java Long.toUnsignedString(long,int)
方法
原文:https://www.studytonight.com/java-wrapper-class/java-long-tounsignedstringlong-i-int-radix-method
Java toUnsignedString(long,int)
方法是java.lang
包的Long
类的一部分。此方法用于返回在作为字符串传递的基(基数)中作为参数传递的长值的无符号整数值。
必须注意的是,如果基数小于Character.MIN_RADIX
或大于Character.MAX_RADIX
,则使用基数10
。此外,由于第一个参数被视为无符号值,因此不会打印前导符号字符。
如果量值为零,则用单个零字符'0'
( '\u0030'
)表示;否则,量值表示的第一个字符将不是零字符。
语法:
public static String toUnsignedString(long i, int radix)
参数:
传递的参数是long i
和int radix
,前者的无符号整数值作为字符串返回,后者定义了字符串转换的基础。
返回:
返回根据作为参数传递的基数值传递的长值的无符号字符串表示形式。
例 1:
这里,长值根据基数值被转换成其等价的无符号字符串表示形式。
import java.lang.Long;
public class StudyTonight
{
public static void main(String[] args)
{
long a = -78L;
int b = 78;
int d = 10;
int h = 16;
int o = 8;
String s1 = Long.toUnsignedString(a,d); //Returns the unsigned string representation of the long value with radix 10
System.out.println("Equivalent String Value = " + s1);
String s2 = Long.toUnsignedString(a,h); //Returns the unsigned string representation of the long value with radix 16
System.out.println("Equivalent String Value = " + s2);
String s3 = Long.toUnsignedString(a, o); //Returns the unsigned string representation of the long value with radix 8
System.out.println("Equivalent String Value = " + s3);
String s4 = Long.toUnsignedString(b, d); //Returns the unsigned string representation of the long value with radix 10
System.out.println("Equivalent String Value = " + s4);
String s5 = Long.toUnsignedString(b, h); //Returns the unsigned string representation of the long value with radix 16
System.out.println("Equivalent String Value = " + s5);
String s6 = Long.toUnsignedString(b, o); //Returns the unsigned string representation of the long value with radix 8
System.out.println("Equivalent String Value = " + s6);
}
}
等效字符串值= 18446744073709551538 等效字符串值= ffffffffffffffb2 等效字符串值= 1777777777777662 等效字符串值= 78 等效字符串值= 4e 等效字符串值= 116
例 2:
这里有一个用户定义的例子,任何使用这段代码的人都可以输入自己选择的值,并获得等效的输出。
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.println("Enter the value and base ");
Scanner sc = new Scanner(System.in);
long val = sc.nextLong();
int b = sc.nextInt();
System.out.println("Output: "+Long.toUnsignedString(val, b)); //returns unsigned string with equivalent base
}
catch(Exception e)
{
System.out.println("Invalid Input!!");
}
}
}
输入数值和基数 7445 8 输出:16425 *输入数值和基数 输入 -688 16 输出:fffffffffffd 50
- *输入数值和基数 0x67 8 无效!!
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。