github.com/danil/iso8583@v0.21.0/tsysbitmap64r/tsysbitmap64r.go (about) 1 package tsysbitmap64r 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "strconv" 7 ) 8 9 // Bitmap holds bits with indexes within range from 1 to 64 10 type Bitmap [8]byte 11 12 func New(p []byte) Bitmap { 13 var b Bitmap 14 copy(b[:], p[:8]) 15 return b 16 } 17 18 func NewString(s string) (Bitmap, error) { 19 i, err := strconv.ParseUint(s, 2, 64) 20 if err != nil { 21 return Bitmap{}, err 22 } 23 p := make([]byte, 8) 24 binary.BigEndian.PutUint64(p, i) 25 return New(p), nil 26 } 27 28 // Get returns true or false for the indexes of the bits in range from 1 to 64 29 func (b *Bitmap) Get(i int) bool { 30 if i < 1 || i > 64 { 31 panic("index out of range from 1 to 64") 32 } 33 i -= 1 34 return b[i/8]&(1<<uint(7-i%8)) != 0 35 } 36 37 // Set sets true value for the indexes of the bits in range from 1 to 64 38 func (b *Bitmap) Set(i int) { 39 if i < 1 || i > 64 { 40 panic("index out of range from 1 to 64") 41 } 42 i -= 1 43 b[i/8] |= 1 << uint(7-i%8) 44 } 45 46 func (b Bitmap) String() string { 47 return fmt.Sprintf("%064s", strconv.FormatUint(binary.BigEndian.Uint64(b[:]), 2)) 48 } 49 50 func (b Bitmap) MarshalISO8583() ([]byte, error) { 51 if b.Get(13) { 52 return append(b[:], 0x52), nil 53 } 54 return b[:], nil 55 }