aboutsummaryrefslogtreecommitdiff
path: root/go/util/bytes.go
diff options
context:
space:
mode:
authorchrislusf <chris.lu@gmail.com>2016-04-10 00:24:22 -0700
committerchrislusf <chris.lu@gmail.com>2016-04-10 00:24:22 -0700
commitb39c384d6dfa63133990a1e190525ef3133f1530 (patch)
treed39b3ce93533f57ea8992ef782289d9ce58a3804 /go/util/bytes.go
parent113392bce88fbb2f3191ff1dfe8d8675c9c8b119 (diff)
downloadseaweedfs-b39c384d6dfa63133990a1e190525ef3133f1530.tar.xz
seaweedfs-b39c384d6dfa63133990a1e190525ef3133f1530.zip
Revert "Merge pull request #284 from thinxer/binary"
This reverts commit 3523ad523929870aa8d4a7741ee8e152cfd40489, reversing changes made to 5d100994b1b9c6679113c8dd73f97aff85397f02.
Diffstat (limited to 'go/util/bytes.go')
-rw-r--r--go/util/bytes.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/go/util/bytes.go b/go/util/bytes.go
new file mode 100644
index 000000000..dfa4ae665
--- /dev/null
+++ b/go/util/bytes.go
@@ -0,0 +1,45 @@
+package util
+
+// big endian
+
+func BytesToUint64(b []byte) (v uint64) {
+ length := uint(len(b))
+ for i := uint(0); i < length-1; i++ {
+ v += uint64(b[i])
+ v <<= 8
+ }
+ v += uint64(b[length-1])
+ return
+}
+func BytesToUint32(b []byte) (v uint32) {
+ length := uint(len(b))
+ for i := uint(0); i < length-1; i++ {
+ v += uint32(b[i])
+ v <<= 8
+ }
+ v += uint32(b[length-1])
+ return
+}
+func BytesToUint16(b []byte) (v uint16) {
+ v += uint16(b[0])
+ v <<= 8
+ v += uint16(b[1])
+ return
+}
+func Uint64toBytes(b []byte, v uint64) {
+ for i := uint(0); i < 8; i++ {
+ b[7-i] = byte(v >> (i * 8))
+ }
+}
+func Uint32toBytes(b []byte, v uint32) {
+ for i := uint(0); i < 4; i++ {
+ b[3-i] = byte(v >> (i * 8))
+ }
+}
+func Uint16toBytes(b []byte, v uint16) {
+ b[0] = byte(v >> 8)
+ b[1] = byte(v)
+}
+func Uint8toBytes(b []byte, v uint8) {
+ b[0] = byte(v)
+}