欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Kotlin中二进制、八进制、十六进制间的相互转换

时间:2023-07-04
Kotlin中进制之间的相互转换:

文本主要记录并描述Kotlin中二进制、八进制、十六进制之间的转换方法


二进制和十进制之间的转换 :

01.二进制转换为十进制:采用取余的方式获取位数,并利用Math.pow()方法进行计算

//二进制转换为十进制 val num: Long = 1000 val value = BinaryToHex(num) Log.i("hy55", "value==$value") val number = HexToBinary(10) Log.i("hy55", "number==$number") private fun BinaryToDecimal(value: Long): Int { var cValue = value var decimalNumber = 0 var remainder: Long var count = 0 while(cValue.toInt() != 0) { remainder = cValue % 10 cValue /= 10 //Math.pow()获取2的count.toDouble()次方 decimalNumber += (remainder * Math.pow(2.0, count.toDouble())).toInt() Log.i("BinaryToDecimal", "decimalNumber==$decimalNumber") ++count } return decimalNumber }

02.十进制转换为二进制:采用取余的方式获取位数

private fun DecimalToBinary(value: Int): Long { var cValue = value var remainder: Int var binaryNumber: Long = 0 var count = 1 while(cValue != 0) { remainder = cValue % 2 cValue /= 2 binaryNumber += (remainder * count).toLong() Log.i("DecimalToBinary", "remainder==$remainder") count *= 10 } return binaryNumber }


二进制和八进制之间的转换:

01.二进制转换为八进制:先转换为十进制

//二进制转换为八进制 private fun BinaryToOctal(value: Long): Int { var binaryNumber = value var octalNumber = 0 var decimalNumber = 0 var count = 0 //二进制转换为十进制 while(binaryNumber.toInt() != 0) { decimalNumber += (binaryNumber % 10 * Math.pow(2.0, count.toDouble())).toInt() ++count binaryNumber /= 10 } count = 1 //十进制转八进制 while (decimalNumber != 0) { octalNumber += decimalNumber % 8 * count decimalNumber /= 8 count *= 10 } return octalNumber }

02.八进制转换为二进制

//八进制转换为二进制 private fun OctalToBinary(value: Int): Long{ var octalNumber = value var decimalNumber = 0 var count = 0 var binaryNumber: Long = 0 //八进制转十进制 while (octalNumber != 0) { decimalNumber += (octalNumber % 10 * Math.pow(8.0, count.toDouble())).toInt() ++count octalNumber /= 10 } count = 1 //十进制转二进制 while (decimalNumber != 0) { binaryNumber += (decimalNumber % 2 * count).toLong() decimalNumber /= 2 count *= 10 } return binaryNumber }


二进制和十六进制之间的转换:

01.二进制转换为十六进制:先转换为十进制

//二进制转换为十六进制 private fun BinaryToHex(value: Long): Int { var binaryNumber = value var HexNumber = 0 var decimalNumber = 0 var count = 0 //二进制转换为十进制 while(binaryNumber.toInt() != 0) { decimalNumber += (binaryNumber % 10 * Math.pow(2.0, count.toDouble())).toInt() ++count binaryNumber /= 10 } count = 1 //十进制转十六进制 while (decimalNumber != 0) { HexNumber += decimalNumber % 16 * count decimalNumber /= 16 count *= 10 } return HexNumber }

02.十六进制转换为二进制:

//十六进制转换为二进制 private fun HexToBinary(value: Int): Long { var HexNumber = value var decimalNumber = 0 var count = 0 var binaryNumber: Long = 0 //十六进制转十进制 while (HexNumber != 0) { decimalNumber += (HexNumber % 10 * Math.pow(16.0, count.toDouble())).toInt() ++count HexNumber /= 10 } count = 1 //十进制转二进制 while (decimalNumber != 0) { binaryNumber += (decimalNumber % 2 * count).toLong() decimalNumber /= 2 count *= 10 } return binaryNumber }


总结:

01.重点掌握二进制和十进制之间的转换
02.灵活运用Math.pow()方法

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。