Golang程序 将十进制转换为八进制
在这篇文章中,你将知道如何在Go语言中把十进制数字转换为八进制。为此,我们将使用strconv包的 FormatInt() 函数。
当我们说将十进制数转换为八进制数时,这意味着将基数为10的数字转换为基数为8的数字。基值是指表示该数值所需的数字数。
例1:Go语言程序使用库函数将十进制数转换为八进制数
语法
func FormatInt(input int64, base int) string
for [condition | ( init; condition; increment) | Range] {
statement(s);
}
func FormatInt(input int64, base int) string:FormatInt()函数用于将整数值转换为另一个基值。这个函数需要两个参数,一个是64位的整数值,另一个是我们要转换的基数值。然后,这个函数将最终结果返回为一个字符串,我们可以将其存储在一个单独的变量中并打印在屏幕上。
算法
- 第1步– 导入软件包fmt和strconv。
-
第2步– 启动函数 main()。
-
第3步 – 声明并初始化整数变量。
-
第4步–调用 FormatInt() 函数。
-
第5步 – 使用fmt包打印结果。
例子
//GO LANGUAGE PROGRAM TO CONVERT DECIMAL NUMBER TO OCTAL USING DIFFERENT FUNCTION
package main
// fmt package provides the function to print anything
//strconv package provides the required function to convert from decimal to octal
import (
"fmt"
"strconv"
)
// this is the main function
func main() {
//initialize the decimal value to a 64-bit int type variable naming num
var num int64
num = 15
// storing the result of conversion in a seperate function called oct_num
oct_num := strconv.FormatInt(num, 8)
// printing the octal number
fmt.Println("Decimal number", num, "is converted to octal as", oct_num)
// taking another decimal number
num = 34
// storing the result of conversion in a seperate function called oct_num
oct_num = strconv.FormatInt(num, 8)
// printing the octal number
fmt.Println("Decimal number", num, "is converted to octal as", oct_num)
//initialize the decimal value
num = 26
// storing the result of conversion in a seperate function called oct_num
oct_num = strconv.FormatInt(num, 8)
// printing the octal number
fmt.Println("Decimal number", num, "is converted to octal as", oct_num)
}
输出
Decimal number 15 is converted to octal as 17
Decimal number 34 is converted to octal as 42
Decimal number 26 is converted to octal as 32
代码的描述
-
首先,我们必须导入fmt包和strconv包。fmt包允许我们在屏幕上打印任何东西,而strconv方法包含了我们可以用来将十进制数字转换成八进制数字的函数。
-
然后我们需要初始化一个64位的整数类型的变量来存储我们希望转换为八进制的整数值。
-
在这个例子中,我们将使用strconv.FormatInt()函数将整数转换为八进制。
-
将你希望转换的整数值作为第一个参数传给函数,将基值作为第二个参数传给函数。
-
将结果存储在一个单独的变量中。在这个例子中,我们使用了oct_num变量来存储结果。
-
然后我们可以使用fmt.Println()函数将结果打印在屏幕上。
例2:使用单独的函数将十进制转换为八进制的Golang程序
算法
-
第1步 – 导入软件包fmt和strconv。
-
第2步 – 初始化一个 toOctal() 函数,它将包含将十进制值转换为八进制的逻辑。
-
第3步 – 启动函数 main()。
-
第4步 – 声明并初始化整数变量。
-
第5步 – 通过传递整数值作为参数,调用 toOctal() 函数。
-
第6步 – 使用 fmt.Println() 函数打印结果。
例子
// GO LANGUAGE PROGRAM TO CONVERT DECIMAL NUMBER TO OCTAL USING FUNCTION
package main
import (
"fmt"
)
// fmt package provides the function to print anything
//strconv package provides the required function to convert from decimal to octal
// initializing and defining a function to convert decimal to octal
func toOctal(number int) int {
// defining variables and assigning them values
octal := 0
counter := 1
remainder := 0
for number != 0 {
remainder = number % 8
number = number / 8
octal += remainder * counter
counter *= 10
}
return octal
}
// this is the main function
func main() {
var num int
//initialize the decimal value
num = 154
// calling the toOctal() function and storing its result in oct_num() function
oct_num := toOctal(num)
// printing the octal number
fmt.Println("Decimal number", num, "is converted to octal as", oct_num)
}
输出
Decimal number 154 is converted to octal as 232
代码的描述
-
首先,我们必须导入fmt包,它允许我们在屏幕上打印任何东西。
-
然后,我们需要初始化整数类型的变量来存储我们希望转换的整数值。
-
调用 toOctal() 函数,将整数值作为参数传给它。
-
在这个函数中,我们使用for循环作为while循环来存储特定的八进制表示。
-
首先,我们需要存储数字除以8的余数,然后我们需要跳过最后一位数字,对剩余的数字重复这一过程。
-
然后,我们需要将结果存储在八进制变量中。为了将下一个数字存储在百位上,以此类推,我们需要将计数器变量乘以10。
-
返回结果并打印在屏幕上。
总结
我们已经成功地编译并执行了一个golang程序,使用上述两种不同的技术将十进制转换为八进制。