瀏覽代碼

Merge pull request #1 from michael-grosshans/master

Fixed UTF-8 Encoding Problems & problem with signed/unsigned integers
Ned McClain 9 年之前
父節點
當前提交
2661553a04
共有 1 個文件被更改,包括 10 次插入7 次删除
  1. 10 7
      ber.go

+ 10 - 7
ber.go

@@ -255,12 +255,8 @@ func ReadPacket(reader io.Reader) (*Packet, error) {
 	return p, nil
 	return p, nil
 }
 }
 
 
-func DecodeString(data []byte) (ret string) {
-	for _, c := range data {
-		ret += fmt.Sprintf("%c", c)
-	}
-
-	return
+func DecodeString(data []byte) (string) {
+	return string(data)
 }
 }
 
 
 func DecodeInteger(data []byte) (ret uint64) {
 func DecodeInteger(data []byte) (ret uint64) {
@@ -294,7 +290,14 @@ func EncodeInteger(val uint64) []byte {
 		mask = mask >> 8
 		mask = mask >> 8
 	}
 	}
 
 
-	return out.Bytes()
+	byteSlice := out.Bytes()
+
+	// if first bit in first byte is 1 it is necessary to append a leading 00 byte to make signum clear
+	// otherwise it happens that a request with messageId => 02 01 80 get response with messageId => 02 04 FF FF FF 80
+	if len(byteSlice) > 0 && byteSlice[0]&byte(128) != 0 {
+		return append([]byte{0x00}, byteSlice...)
+	}
+	return byteSlice
 }
 }
 
 
 func DecodePacket(data []byte) *Packet {
 func DecodePacket(data []byte) *Packet {