Coder Social home page Coder Social logo

bit's Introduction

bit

Go Reference Go Report Card MIT License

This package provides encoding for converting byte sequences to bit strings.
For example, a byte represented by the hexadecimal number ff would be encoded as 11111111 .

Inspired by the standard package encoding/hex. I also got some implementation hints from it.

Why is this necessary?

Byte sequences can be output as bit strings using the standard package fmt as follows

for i := 0; i < len(src); i++ {
    fmt.Printf("%08b", src[i])
}

In some cases, this is sufficient. However, this does not implement io.Reader or io.Writer , so flexible handling such as stream support is not possible.
In addition, it is tedious to output in a human-readable format like xxd -b.

Therefore, outputting bit strings using the standard package fmt is inconvenient, for example, when debugging a program that evaluates binaries.

I created this package for more flexible handling (e.g. io.Reader and io.Writer support, Or Dump() output support like xxd -b ).

Usage

Here are the basics. If you want more details, please refer to example_test or package documentation.

Encode

Encode the given byte sequences.

src := []byte("Hello Gopher!")

dst := make([]byte, EncodedLen(len(src)))
Encode(dst, src)

fmt.Printf("%s\n", dst)

// Output:
// 01001000011001010110110001101100011011110010000001000111011011110111000001101000011001010111001000100001

Decode

Decode takes as input a bit-encoded byte sequences.
Eight characters represent one byte, so the input must be a multiple of 8 bytes.

src := []byte("01001000011001010110110001101100011011110010000001000111011011110111000001101000011001010111001000100001")

dst := make([]byte, DecodedLen(len(src)))
n, err := Decode(dst, src)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%s\n", dst[:n])

// Output:
// Hello Gopher!

Dump

Dump returns output like xxd -b .

dump := Dump([]byte("dump test"))
fmt.Printf("%s\n", dump)

// Output:
// 00000000: 01100100 01110101 01101101 01110000 00100000 01110100  dump t
// 00000006: 01100101 01110011 01110100                             est

LICENSE

MIT

bit's People

Contributors

convto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.