blob: a29e7ddfbaba78a6d0d1fcbee57e26e073adb33a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package ber
func UnmarshalInt(b []byte) (i int) {
neg := b[0]&0x80 != 0
for _, v := range b {
if neg {
v = ^v
}
i <<= 8
i |= int(v)
}
if neg {
i = ^i
}
return
}
func intLen(i int) (n int) {
for i > 127 {
n++
i >>= 8
}
for i < -128 {
n++
i >>= 8
}
return n + 1
}
func MarshalInt(i int) (b []byte) {
for n := intLen(i); n > 0; n-- {
b = append(b, byte(i>>uint((n-1)*8)))
}
return
}
|