endian

package
v0.0.0-...-ce94876 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2019 License: MIT, Apache-2.0 Imports: 2 Imported by: 0

README

GoEndian

A tool to detect byte order for golang.

A sample test code :

package main

import (
    "encoding/binary"
    "fmt"
    "github.com/virtao/GoEndian"
)

func main() {
    printEndian()
    useEndian()
}

func printEndian() {
    fmt.Println("Machine byte order : ")
    if endian.IsBigEndian() {
        fmt.Println("Big Endian")
    } else {
        fmt.Println("Little Endian")
    }
}

func useEndian() {
    var iTest int32 = 0x12345678
    var bTest []byte = make([]byte, 4)
    fmt.Println("Int32 to Bytes : ")

    fmt.Println("0x12345678 to current endian : ")
    endian.Endian.PutUint32(bTest, uint32(iTest))
    fmt.Println(bTest)

    fmt.Println("0x12345678 to big endian : ")
    binary.BigEndian.PutUint32(bTest, uint32(iTest))
    fmt.Println(bTest)

    fmt.Println("0x12345678 to little endian : ")
    binary.LittleEndian.PutUint32(bTest, uint32(iTest))
    fmt.Println(bTest)

}

The result output:

    Machine byte order : 
    Little Endian
    Int32 to Bytes : 
    0x12345678 to current endian : 
    [120 86 52 18]
    0x12345678 to big endian : 
    [18 52 86 120]
    0x12345678 to little endian : 
    [120 86 52 18]

Documentation

Index

Constants

View Source
const INT_SIZE int = int(unsafe.Sizeof(0))

以下代码判断机器大小端

Variables

保存机器大小端

Functions

func IsBigEndian

func IsBigEndian() bool

func IsLittleEndian

func IsLittleEndian() bool

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL