使用golang如何實現(xiàn)javauuid的序列化-創(chuàng)新互聯(lián)

使用golang如何實現(xiàn)java uuid的序列化?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

成都創(chuàng)新互聯(lián)致力于成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計,成都網(wǎng)站設(shè)計,集團網(wǎng)站建設(shè)等服務(wù)標準化,推過標準化降低中小企業(yè)的建站的成本,并持續(xù)提升建站的定制化服務(wù)水平進行質(zhì)量交付,讓企業(yè)網(wǎng)站從市場競爭中脫穎而出。 選擇成都創(chuàng)新互聯(lián),就選擇了安全、穩(wěn)定、美觀的網(wǎng)站建設(shè)服務(wù)!

java生成的固定的uuid:85bb94b8-fd4b-4e1c-8f49-3cedd49d8f28的序列化

package main

import (
  "encoding/binary"
  "encoding/json"
  "fmt"
  "log"
  "os"
  "strings"
  "time"

  "github.com/Shopify/sarama"
  "github.com/google/uuid"
)

const (
  DATE_TIME_PATTERN = ""
  STREAM_MAGIC   = 0xaced
  STREAM_VERSION  = 5
  TC_STRING     = 0x74
  TC_OBJECT     = 0x73
  TC_CLASSDESC   = 0x72
  SC_SERIALIZABLE  = 0x02
  TC_ENDBLOCKDATA  = 0x78
  TC_NULL      = 0x70
)

func main() {
  uuidTest()
}

func uuidTest() {
  f, _ := os.Create("uuid-go.out")
  defer f.Close()

  f.Write(ShortBytes(STREAM_MAGIC))
  f.Write(ShortBytes(STREAM_VERSION))
  f.Write([]byte{TC_OBJECT})
  f.Write([]byte{TC_CLASSDESC})

  className := "java.util.UUID"
  classNameLen := len(className)

  f.Write(ShortBytes(uint16(classNameLen)))
  f.Write([]byte(className))

  sid := -4856846361193249489

  f.Write(LongBytes(uint64(sid)))

  //flags
  f.Write([]byte{2})

  //fields length
  f.Write(ShortBytes(2))

  //field type code
  f.Write([]byte{'J'})

  f1 := "leastSigBits"
  f1Len := len(f1)

  f.Write(ShortBytes(uint16(f1Len)))
  f.Write([]byte(f1))

  //filed type code
  f.Write([]byte{'J'})

  f2 := "mostSigBits"
  f2Len := len(f2)

  f.Write(ShortBytes(uint16(f2Len)))
  f.Write([]byte(f2))

  f.Write([]byte{TC_ENDBLOCKDATA})
  f.Write([]byte{TC_NULL})

  leastSigBits := -8121893460813967576

  f.Write(LongBytes(uint64(leastSigBits)))

  mostSigBits := -8810284723775779300

  f.Write(LongBytes(uint64(mostSigBits)))

}

func ShortBytes(i uint16) []byte {
  bytes := make([]byte, 2)

  binary.BigEndian.PutUint16(bytes, i)

  return bytes
}

func LongBytes(i uint64) []byte {
  bytes := make([]byte, 8)

  binary.BigEndian.PutUint64(bytes, i)

  return bytes
}

func BigEndian() { // 大端序
  // 二進制形式:0000 0000 0000 0000 0001 0002 0003 0004
  var testInt int32 = 0x01020304 // 十六進制表示
  fmt.Printf("%d use big endian: \n", testInt)

  var testBytes []byte = make([]byte, 4)
  binary.BigEndian.PutUint32(testBytes, uint32(testInt)) //大端序模式
  fmt.Println("int32 to bytes:", testBytes)

  convInt := binary.BigEndian.Uint32(testBytes) //大端序模式的字節(jié)轉(zhuǎn)為int32
  fmt.Printf("bytes to int32: %d\n\n", convInt)
}

func LittleEndian() { // 小端序
  //二進制形式: 0000 0000 0000 0000 0001 0002 0003 0004
  var testInt int32 = 0x01020304 // 16進制
  fmt.Printf("%d use little endian: \n", testInt)

  var testBytes []byte = make([]byte, 4)
  binary.LittleEndian.PutUint32(testBytes, uint32(testInt)) //小端序模式
  fmt.Println("int32 to bytes:", testBytes)

  convInt := binary.LittleEndian.Uint32(testBytes) //小端序模式的字節(jié)轉(zhuǎn)換
  fmt.Printf("bytes to int32: %d\n\n", convInt)
}

func Int64ToBytes(i int64) []byte {
  var buf = make([]byte, 8)
  binary.BigEndian.PutUint64(buf, uint64(i))
  return buf
}

新聞標題:使用golang如何實現(xiàn)javauuid的序列化-創(chuàng)新互聯(lián)
瀏覽路徑:http://bm7419.com/article6/gdiig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈定制開發(fā)、面包屑導(dǎo)航網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司