scala字符串转int
The hex string is also known as Hexadecimal Number String is a number created using hexadecimal number system i.e. base 16 number system.
十六进制字符串也称为十六进制数字字符串是使用十六进制数字系统(即基数16的数字系统)创建的数字。
Integer in Scala is a numerical value that uses base 10 values.
Scala中的Integer是一个以10为底的值的数值。
将十六进制字符串转换为int (Converting hex string to int)
A hexadecimal string can be converted to integer type using the parseInt() method of the Integer class that will parse the given string with a given base to an integer value.
可以使用Integer类的parseInt()方法将十六进制字符串转换为整数类型,该方法会将具有给定基数的给定字符串解析为整数值。
Syntax:
句法:
val int_value = Integer.parseInt(hex_string , base)
在Scala中将十六进制字符串转换为int的程序 (Program to convert hex string to int in Scala)
object MyObject {
def convertHexStringToInt(hexString : String): Int = {
return Integer.parseInt(hexString, 16)
}
def main(args: Array[String]) {
val hexString : String = "10DEA"
println("The Hex String is " + hexString)
println("The String converted to decimal is " + convertHexStringToInt(hexString))
}
}
Output:
输出:
The Hex String is 10DEA
The String converted to decimal is 69098
Explanation:
说明:
In the above code, we have created a function convertHexStringToInt() which will convert the given hex String to an int. The convert uses the parseInt() method which is available in the Integer class. The function takes a string as a parameter and returns the integer which is string conversion of hexString. This returned value is printed using the println method.
在上面的代码中,我们创建了一个函数convertHexStringToInt() ,它将给定的十六进制String转换为int。 转换使用Integer类中可用的parseInt()方法。 该函数将字符串作为参数,并返回整数,该整数是hexString的字符串转换。 该返回值使用println方法打印。
翻译自: https://www.includehelp.com/scala/how-to-convert-hex-string-to-int-in-scala.aspx
scala字符串转int