如何将带有文本和数字组合的字符串转换为java中的int类型?

时间:2021-09-15 16:31:56

How do i convert for example A0 into an int? And how do i then combine 0x with A0 as an int in java? My task requires me to use int as the data type. Thanks

我如何将例如A0转换为int?然后我如何将0x与A0结合作为java中的int?我的任务要求我使用int作为数据类型。谢谢

1 个解决方案

#1


1  

You are wanting to convert a Hex value into an int value for this you can use the Integer method decode().

您想要将Hex值转换为int值,您可以使用Integer方法decode()。

The conversion can look like this:

转换可能如下所示:

int i = Integer.decode("0xA0");

or

int i = Integer.decode("#A0");

in which case the result will be: i = 160

在这种情况下,结果将是:i = 160

If you wise to convert an int value back to Hex form use the Integer method toHexString() like this:

如果明智地将int值转换回Hex形式,请使用Integer方法toHexString(),如下所示:

String s = Integer.toHexString(i);

This will give you s = "a0".

这将给你s =“a0”。

For more information on the conversions take a look at the java docs:

有关转换的更多信息,请查看java文档:

https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#decode(java.lang.String)

#1


1  

You are wanting to convert a Hex value into an int value for this you can use the Integer method decode().

您想要将Hex值转换为int值,您可以使用Integer方法decode()。

The conversion can look like this:

转换可能如下所示:

int i = Integer.decode("0xA0");

or

int i = Integer.decode("#A0");

in which case the result will be: i = 160

在这种情况下,结果将是:i = 160

If you wise to convert an int value back to Hex form use the Integer method toHexString() like this:

如果明智地将int值转换回Hex形式,请使用Integer方法toHexString(),如下所示:

String s = Integer.toHexString(i);

This will give you s = "a0".

这将给你s =“a0”。

For more information on the conversions take a look at the java docs:

有关转换的更多信息,请查看java文档:

https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#decode(java.lang.String)