How to convert binary code to text in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strconv"
)

// Function to convert binary string to text
func bin2text(binTxt string) string {
    text := ""

    // Process the binary string in chunks of 8 bits
    for i := 0; i < len(binTxt); i += 8 {
        // Extract an 8-bit chunk
        binaryChunk := binTxt[i : i + 8]

        // Convert the binary chunk to an integer
        asciiValue, err := strconv.ParseInt(binaryChunk, 2, 64)
        if err != nil {
            fmt.Println("Error parsing binary string:", err)
            return ""
        }

        // Convert the integer to a character and append to the result
        text += string(rune(asciiValue))
    }

    return text
}

func main() {
    binaryInput := "0101000001110010011011110110011101110010011000010110110101101101011010010110111001100111"
    
    textOutput := bin2text(binaryInput)

    fmt.Println(textOutput)
}




/*
run:

Programming

*/

 



answered Apr 14, 2025 by avibootz

Related questions

...