scala字符串转int_如何在Scala中将十六进制字符串转换为int?

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值