您好,登錄后才能下訂單哦!
本篇內容主要講解“golang網絡字節與基本類型的轉換方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“golang網絡字節與基本類型的轉換方法”吧!
網絡通信需要將go基本數據類型轉為字節. go如何做?
c類型 | go類型 | 字節長度 |
---|---|---|
signed char | int8 | 1 |
unsigned char | uint8 | 1 |
_Bool | bool | 1 |
short | int16 | 2 |
unsigned short | uint16 | 2 |
int | int32 | 4 |
unsigned int | uint32 | 4 |
float | float32 | 4 |
long long | int64 | 8 |
unsigned l long | uint64 | 8 |
double | float64 | 8 |
char[] | uint8 | 1 |
int8/uint->byte或 byte->int8/uint8
1個字節強制類型轉換會超范圍
// int8 ->byte var a int8 = -1 byte(a) // 正常 255 //byte->int8 int8(byte(255)) //異常 constant 255 overflows int8 // byte->int8 var a byte = 255 int8(a) //正常 -1
/int8/uint8/int16/uint16/int32/uint32/int64/uint64/float32/float64->[]byte
var a int16 =1 buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, &a) fmt.Println(buf.Bytes())
[]byte ->int8/uint8/int16/uint16/int32/uint32/int64/uint64/float32/float64
b :=[]byte{64, 9, 30, 184, 81, 235, 133, 31} var a float64 binary.Read(bytes.NewBuffer(b), binary.BigEndian, &a) fmt.Println(a)
import ( "bytes" "encoding/binary" "reflect" ) //================================= // 數字-->字節 //================================= func Number2Bytes(value interface{}) []byte{ result := make([]byte, 0) switch v := value.(type){ case int8: return append(result, uint8(v)) case int16: binary.BigEndian.PutUint16(result, uint16(v)) return result case int32: binary.BigEndian.PutUint32(result, uint32(v)) return result case int64: binary.BigEndian.PutUint64(result, uint64(v)) return result case uint8: return append(result, uint8(v)) case uint16: binary.BigEndian.PutUint16(result, v) return result case uint32: binary.BigEndian.PutUint32(result, v) return result case uint64: binary.BigEndian.PutUint64(result, v) return result case float32: buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, &v) return buf.Bytes() case float64: buf := new(bytes.Buffer) binary.Write(buf, binary.BigEndian, &v) return buf.Bytes() } return nil } //================================= // 字節-->數字 //================================= func Bytes2Number(data []byte, kind reflect.Kind) interface{}{ switch kind{ case reflect.Int8: return int8(data[0]) case reflect.Int16: return int16(binary.BigEndian.Uint16(data)) case reflect.Int32: return int32(binary.BigEndian.Uint32(data)) case reflect.Int64: return int64(binary.BigEndian.Uint64(data)) case reflect.Uint8: return data[0] case reflect.Uint16: return binary.BigEndian.Uint16(data) case reflect.Uint32: return binary.BigEndian.Uint32(data) case reflect.Uint64: return binary.BigEndian.Uint64(data) case reflect.Float32: var v float32 buf := bytes.NewBuffer(data) binary.Read(buf, binary.BigEndian, &v) return v case reflect.Float64: var v float64 buf := bytes.NewBuffer(data) binary.Read(buf, binary.BigEndian, &v) return v } return nil }
到此,相信大家對“golang網絡字節與基本類型的轉換方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。