github.com/virtao/GoTypeBytes@v0.0.0-20140410081032-5927a98cc663/README.md (about)

     1  GoTypeBytes
     2  ===========
     3  
     4  A basic type of conversion tool for Go. It can achieve mutual conversion between primitive types and arrays of bytes. 
     5  
     6  Types that can be converted:
     7  
     8  int/int16/int32/int64/uint/uint16/uint32/uint64/float32/float64 <=> []byte
     9  
    10  Sample code:
    11  
    12  
    13  
    14  ```
    15  package main
    16  
    17  import (
    18      "encoding/hex"
    19      "fmt"
    20      "github.com/virtao/GoTypeBytes"
    21  )
    22  
    23  func main() {
    24      fmt.Println("typeBytes库测试:")
    25      typeBytesTest()
    26  }
    27  
    28  func typeBytesTest() {
    29      fmt.Println("float32 size : ", typeBytes.FLOAT32_SIZE)
    30      fmt.Println("float64 size : ", typeBytes.FLOAT64_SIZE)
    31      fmt.Println("int size : ", typeBytes.INT_SIZE)
    32      fmt.Println("uint size : ", typeBytes.UINT_SIZE)
    33  
    34      var testInt int = 0x1f123400ff123400
    35      var testInt16 int16 = 0x1f12
    36      var testInt32 int32 = 0x1f123400
    37      var testInt64 int64 = 0x1f123400ff123400
    38      var testUint uint = 0xff123400ff123400
    39      var testUint16 uint16 = 0xff12
    40      var testUint32 uint32 = 0xff123400
    41      var testUint64 uint64 = 0xff123400ff123400
    42      var testFloat32 float32 = -1234.5678 //x86_64 PC上存储形式应该为:2b 52 9a c4
    43      var testFloat64 float64 = -1234.5678 //x86_64 PC上存储形式应该为:ad fa 5c 6d 45 4a 93 c0
    44  
    45      buf := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    46  
    47      var tmp []byte
    48      tmp = typeBytes.IntToBytes(testInt)
    49      fmt.Printf("IntToBytes : 0x%0x to %v, BigEndian = %v\n", testInt, hex.EncodeToString(tmp), hex.EncodeToString(typeBytes.DefaultToBigEndian(tmp)))
    50      fmt.Printf("Int16ToBytes : 0x%0x to %v\n", testInt16, hex.EncodeToString(typeBytes.Int16ToBytes(testInt16)))
    51      fmt.Printf("Int32ToBytes : 0x%0x to %v\n", testInt32, hex.EncodeToString(typeBytes.Int32ToBytes(testInt32)))
    52      fmt.Printf("Int64ToBytes : 0x%0x to %v\n", testInt64, hex.EncodeToString(typeBytes.Int64ToBytes(testInt64)))
    53      fmt.Printf("UintToBytes : 0x%0x to %v\n", testUint, hex.EncodeToString(typeBytes.UintToBytes(testUint)))
    54      fmt.Printf("Uint16ToBytes : 0x%0x to %v\n", testUint16, hex.EncodeToString(typeBytes.Uint16ToBytes(testUint16)))
    55      fmt.Printf("Uint32ToBytes : 0x%0x to %v\n", testUint32, hex.EncodeToString(typeBytes.Uint32ToBytes(testUint32)))
    56      fmt.Printf("Uint64ToBytes : 0x%0x to %v\n", testUint64, hex.EncodeToString(typeBytes.Uint64ToBytes(testUint64)))
    57      tmp = typeBytes.Float32ToBytes(testFloat32)
    58      fmt.Printf("Float32ToBytes : %f to %v, BigEndian = %v\n", testFloat32, hex.EncodeToString(tmp), hex.EncodeToString(typeBytes.DefaultToBigEndian(tmp)))
    59      fmt.Printf("Float64ToBytes : %f to %v\n", testFloat64, hex.EncodeToString(typeBytes.Float64ToBytes(testFloat64)))
    60  
    61      //数组长度刚好
    62      fmt.Printf("BytesToInt(8Bytes) : %s to 0x%0x\n", hex.EncodeToString(buf[2:10]), typeBytes.BytesToInt(buf[2:10]))
    63      //数组长度太大,截取前INT_SIZE个字节
    64      fmt.Printf("BytesToInt(>8Bytes) : %s to 0x%0x\n", hex.EncodeToString(buf), typeBytes.BytesToInt(buf))
    65      //数组长度太小,根据大小端字节序保存
    66      fmt.Printf("BytesToInt(2Bytes) : %s to 0x%0x\n", hex.EncodeToString(buf[:2]), typeBytes.BytesToInt(buf[:2]))
    67  }
    68  ```
    69  
    70  Output:
    71  
    72  ```
    73      typeBytes库测试:
    74      float32 size :  4
    75      float64 size :  8
    76      int size :  8
    77      uint size :  8
    78      IntToBytes : 0x1f123400ff123400 to 003412ff0034121f, BigEndian = 1f123400ff123400
    79      Int16ToBytes : 0x1f12 to 121f
    80      Int32ToBytes : 0x1f123400 to 0034121f
    81      Int64ToBytes : 0x1f123400ff123400 to 003412ff0034121f
    82      UintToBytes : 0xff123400ff123400 to 003412ff003412ff
    83      Uint16ToBytes : 0xff12 to 12ff
    84      Uint32ToBytes : 0xff123400 to 003412ff
    85      Uint64ToBytes : 0xff123400ff123400 to 003412ff003412ff
    86      Float32ToBytes : -1234.567749 to 2b529ac4, BigEndian = c49a522b
    87      Float64ToBytes : -1234.567800 to adfa5c6d454a93c0
    88      BytesToInt(8Bytes) : 030405060708090a to 0xa09080706050403
    89      BytesToInt(>8Bytes) : 0102030405060708090a to 0x807060504030201
    90      BytesToInt(2Bytes) : 0102 to 0x201
    91  ```