Golang 将十进制转换为八进制

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 - 导入包 fmtstrconv

  • 步骤 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:使用单独函数将十进制转换为八进制的Go程序

步骤

  • 第一步 - 导入fmt和strconv包。

  • 第二步 - 初始化一个名为toOctal()的函数,其中包含将十进制值转换为八进制的逻辑。

  • 第三步 - 开始main()函数。

  • 第四步 - 声明并初始化整数变量。

  • 第五步 - 调用toOctal()函数,并将整数值作为参数传递给它。

  • 第六步 - 使用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。

  • 返回结果并将其打印在屏幕上。

结论

我们已成功编译和执行了一个使用上述2种不同技巧将十进制转换为八进制的golang程序。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程