Created
July 1, 2017 20:32
-
-
Save awnumar/f876bbedd4b014ce858c04d879ed9ef5 to your computer and use it in GitHub Desktop.
A small script outlining binary encoding int64 and uint64. (https://play.golang.org/p/t_xT3PcpcK)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"encoding/binary" | |
) | |
func main() { | |
// | |
// Largest signed int64 in smallest byte slice. | |
// | |
var signedInt64 int64 = 9223372036854775807 | |
a := make([]byte, 10) | |
binary.PutVarint(a, signedInt64) | |
decodedSignedInt64, _ := binary.Varint(a) | |
fmt.Println(a, decodedSignedInt64) | |
// | |
// Largest unsigned int64 in smallest byte slice. | |
// | |
var unsignedInt64 uint64 = 18446744073709551615 | |
b := make([]byte, 10) | |
binary.PutUvarint(b, unsignedInt64) | |
decodedUnsignedInt64, _ := binary.Uvarint(b) | |
fmt.Println(b, decodedUnsignedInt64) | |
} | |
// Output: | |
// [254 255 255 255 255 255 255 255 255 1] 9223372036854775807 | |
// [255 255 255 255 255 255 255 255 255 1] 18446744073709551615 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment