亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

int64 的可變長度二進制補碼

int64 的可變長度二進制補碼

Go
慕工程0101907 2021-11-01 16:02:14
我正在嘗試編寫一個 Go 程序來解析 ans.1 BER 二進制補碼整數編碼。但是,整數可以具有 1、2、3 或 4 字節長度的編碼(取決于其大小)。根據規范(http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf)最左邊的位總是補碼。什么是干凈的方法來做到這一點?func ParseInt(b []byte) (int64, error) {    switch len(b) {    case 1:        // this works        return int64(b[0]&0x7f) - int64(b[0]&0x80), nil    case 2:        // left most byte of b[0] is -32768    case 3:        // left most byte of b[0] is -8388608    case 4:        // left most byte of b[0] is -2147483648 (and so on for 5, 6, 7, 8)    case 5:    case 6:    case 7:    case 8:    default:        return 0, errors.New("value does not fit in a int64")    }}ParseInt([]byte{0xfe})       // should return (-2, nil)ParseInt([]byte{0xfe, 0xff}) // should return (-257, nil)ParseInt([]byte{0x01, 0x00}) // should return (256, nil)
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號