2013-09-24 6 views

Répondre

4

Vous pouvez utiliser le package d'encodage/binaire. Ex: (http://play.golang.org/p/5s_-hclYJ0)

package main 

import (
    "encoding/binary" 
    "fmt" 
) 

func main() { 
    buf := make([]byte, 1024) 

    // Put uint16 320 (big endian) into buf at offster 127 
    binary.BigEndian.PutUint16(buf[127:], 320) 

    // Put uint16 420 (little endian) into buf at offster 127 
    binary.LittleEndian.PutUint16(buf[255:], 420) 

    // Retrieve the uint16 big endian from buf 
    result := binary.BigEndian.Uint16(buf[127:]) 
    fmt.Printf("%d\n", result) 

    // Retrieve the uint16 little endian from buf 
    result = binary.LittleEndian.Uint16(buf[255:]) 
    fmt.Printf("%d\n", result) 

    // See the state of buf (display only 2 bytes from the given often as it is uint16) 
    fmt.Printf("%v, %v\n", buf[127:129], buf[255:257]) 
} 
Questions connexes