Java Integer.toUnsignedString()
方法
原文:https://www.studytonight.com/java-wrapper-class/java-integer-tounsignedstring-method
Java toUnsignedString()
方法是java.lang
包的Integer
类的一部分。此方法返回作为参数作为无符号十进制值传递的整数值的等效字符串对象。
简而言之,该方法用于将整数值转换为无符号字符串。
语法:
public static String toUnsignedString(int i)
参数:
传递的参数是整数值,其无符号十进制值的字符串表示形式将被返回。
返回:
返回以字符串形式作为参数传递的长值的无符号十进制等效值。
例 1:
在这里,整数值被转换成其等价的无符号十进制值,并表示为字符串。
import java.lang.Integer;
public class StudyTonight
{
public static void main(String[] args)
{
int a = 40;
int b = -56;
System.out.println("Equivalent String is : "+Integer.toUnsignedString(a)); //returns the unsigned String of the integer value
System.out.println("Equivalent String is : "+Integer.toUnsignedString(b)); //returns the unsigned String of the integer value
}
}
等效串为:40 等效串为:4294967240
例 2:
这里有一个用户定义的例子,任何使用这段代码的人都可以输入自己选择的值,并获得等效的输出。
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the value ");
Scanner sc = new Scanner(System.in);
int val = sc.nextInt();
String s = Integer.toUnsignedString(val); //converting to unsigned string
System.out.println("String value is : "+ s);
}
catch(Exception e)
{
System.out.println("Invalid input!!");
}
}
}
输入值 85 字符串值为:85 *输入值-69 字符串值为:4294967227 *输入值 0x71 输入无效!!
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。