SoFunction
Updated on 2025-04-10

Example code for implementing binary and decimal conversion in Go language

Recently, I was preparing for the software designer exam for the software exam. During the learning process, I encountered many knowledge points related to computer basic computing. I happened to be learning Go language recently, so I implemented the calculation method in Go language.
It is still in the process of learning. If you have any questions, please feel free to correct me.

Binary to decimal

/*
    Conversion rules: 11001 From the end to the beginning, with 2 as the base, incremented from 0 as an exponent * binary number, and then add these binary numbers to obtain a decimal number.
     11001 = 1 * 2^0 + 0 * 2^1 + 0 * 2^2 + 1 * 2^3 + 1 * 2^4 = 1 + 0 + 0 + 8 + 16 = 25
     This rule also applies to other binary conversions to decimal. You only need to replace the base number with the corresponding binary number. This method is called "rights expansion method"

     Note: Binary numbers also have decimal points, the difference is that the exponent on the left of the decimal point is a positive number, and the exponent on the right is a negative number.
     For example: 11.01 = 1 * 2^-2 + 0 * 2^-1 + 1 * 2^0 + 1 * 2^1 = 0.25 + 0 + 1 + 2 = 3.25
 */
func binaryToDecimal(val string) string  {
    // Get binary character string    // Use regular verification before use [0-1]|[0-1].[0-1]    var text = val
    // Index v1    var v1 float64 = 0
    var  len  =  len(text)
    // Check whether the decimal points are included    contains := (text, ".")
    if contains {
        index := (text, ".")
        if index == (len -1) {
            text = text[0 : len-1]
        } else {
            v3  :=len - index
            v3--
            v1 = float64(0 - v3)
        }
    }
    (v1)
    // result    var result float64
    for i := len -1; i >= 0; i-- {
        u := string(text[i])
        if u == "." {
            continue
        }
        v2,error := (u, 64)
        if error != nil {
            ("Conversion failed",error)
        }
        // Multiplier        pow := (2, v1)
        //Accumulate numerical values        result = result + (v2 * pow)
        // Exponential increment        v1++
    }
    // There is a bug here, and the decimal point that is not determined how many decimal places are followed by the decimal point in the decimal point    return (int64(result), 10)
}

Decimal to binary

/*
    Decimal conversion method of binary: Dividing decimal number by 2 to get the remainder
  */
func  decimalToBinary(val string) string  {
    number, err := (val, 10, 64)
    if err != nil {
        ("Digital conversion failed",err)
        return ""
    }

    // Check whether the number is negative    var bool  = number < 0

    if bool {
        number = 0 -number
    }

    var result  = ""
    for true {
        if number == 1 {
            result = (result,number)
            break
        }
        ///// Divider        var v1 = number / 2
        ///// Remaining number        var v2 = number % 2
        // Take the remainder and splice the binary number        result = (result,v2)
        number = v1
    }
    
    // Invert the string    var finalResult  = ""
    var len = len(result)
    for i := len - 1; i >= 0; i-- {
        finalResult = (finalResult,string(result[i]))
    }
    // If it is a negative number, add the symbol    if bool {
        finalResult = ("-",finalResult)
    }
    return finalResult
}

Summarize

  • The method of converting R-digit to decimal numbers is called the weight expansion method, which refers to the exponent
  • The exponent is negative on the right side of the decimal point, and the left side is positive. For example, the exponents of the binary number "1110.01" are in turn -2 -1 0 1 2 3

This is the article about the example code for the Go language to implement binary and decimal conversion. For more related contents of the Go language binary and decimal conversion, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!