Documentation ¶
Overview ¶
Package b2n provides functions for parsing and conversion values from byte arrays and slices home page https:// github.com/filipkroca/b2n
Index ¶
- func ParseBs2Int16TwoComplement(bs *[]byte, offset int) (int16, error)
- func ParseBs2Int32TwoComplement(bs *[]byte, offset int) (int32, error)
- func ParseBs2Int64TwoComplement(bs *[]byte, offset int) (int64, error)
- func ParseBs2Int8TwoComplement(bs *[]byte, offset int) (int8, error)
- func ParseBs2Uint16(bs *[]byte, offset int) (uint16, error)
- func ParseBs2Uint32(bs *[]byte, offset int) (uint32, error)
- func ParseBs2Uint64(bs *[]byte, offset int) (uint64, error)
- func ParseBs2Uint8(bs *[]byte, offset int) (uint8, error)
- func ParseIMEI(bs *[]byte, offset int, length int) (string, error)
- func ValidateIMEI(imei *string) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseBs2Int16TwoComplement ¶
ParseBs2Int16TwoComplement takes a pointer to a byte slice coded with Two Complement Arithmetic, offset and returns parsed signed Int16 coded with Two Complement Arithmetic and error
Example ¶
// sample data bs := []byte{0xFF, 0xFF, 0x80, 0x00, 0x7F, 0xFF, 0x00, 0x00} // run test decoded, err := ParseBs2Int16TwoComplement(&bs, 0) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int16TwoComplement(&bs, 2) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int16TwoComplement(&bs, 4) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int16TwoComplement(&bs, 6) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded)
Output: int16 -1 int16 -32768 int16 32767 int16 0
func ParseBs2Int32TwoComplement ¶
ParseBs2Int32TwoComplement takes a pointer to a byte slice coded with Two Complement Arithmetic, offset and returns parsed signed Int32 coded with Two Complement Arithmetic and error
Example ¶
// sample data bs := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00} // run test decoded, err := ParseBs2Int32TwoComplement(&bs, 0) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int32TwoComplement(&bs, 4) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int32TwoComplement(&bs, 8) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int32TwoComplement(&bs, 12) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded)
Output: int32 -1 int32 -2147483648 int32 2147483647 int32 0
func ParseBs2Int64TwoComplement ¶
ParseBs2Int64TwoComplement takes a pointer to a byte slice coded with Two Complement Arithmetic, offset and returns parsed signed Int64 coded with Two Complement Arithmetic and error
Example ¶
// sample data bs := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // run test decoded, err := ParseBs2Int64TwoComplement(&bs, 0) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int64TwoComplement(&bs, 8) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int64TwoComplement(&bs, 16) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int64TwoComplement(&bs, 24) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded)
Output: int64 -1 int64 -9223372036854775808 int64 9223372036854775807 int64 0
func ParseBs2Int8TwoComplement ¶
ParseBs2Int8TwoComplement takes a pointer to a byte slice coded with Two Complement Arithmetic, offset and returns parsed signed Int8 and error
Example ¶
// sample data bs := []byte{0xFF, 0x80, 0x51, 0x00} // run test decoded, err := ParseBs2Int8TwoComplement(&bs, 0) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int8TwoComplement(&bs, 1) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int8TwoComplement(&bs, 2) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded) // run test decoded, err = ParseBs2Int8TwoComplement(&bs, 3) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v\n", decoded, decoded)
Output: int8 -1 int8 -128 int8 81 int8 0
func ParseBs2Uint16 ¶
ParseBs2Uint16 a pointer to a byte slice, offset and returns parsed int16 and error
Example ¶
// sample data bs := []byte{0xAB, 0xCD, 0xAB, 0xFF} // run test decoded, err := ParseBs2Uint16(&bs, 0) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v", decoded, decoded)
Output: uint16 43981
func ParseBs2Uint32 ¶
ParseBs2Uint32 a pointer to a byte slice, offset and returns parsed int32 and error
Example ¶
// sample data bs := []byte{0xAB, 0xCD, 0xAB, 0xFF} // run test decoded, err := ParseBs2Uint32(&bs, 0) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v", decoded, decoded)
Output: uint32 2882382847
func ParseBs2Uint64 ¶
ParseBs2Uint64 a pointer to a byte slice, offset and returns parsed int64 and error
Example ¶
// sample data bs := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} // run test decoded, err := ParseBs2Uint64(&bs, 0) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v", decoded, decoded)
Output: uint64 18446744073709551615
func ParseBs2Uint8 ¶
ParseBs2Uint8 a pointer to a byte slice, offset and returns parsed Uint8 and error
Example ¶
// sample data bs := []byte{0xAB, 0xFF, 0x12} // run test decoded, err := ParseBs2Uint8(&bs, 1) if err != nil { fmt.Println(err) return } fmt.Printf("%T %v", decoded, decoded)
Output: uint8 255
func ParseIMEI ¶
ParseIMEI takes a pointer to a byte slice including IMEI number encoded as ASCII, IMEI length, offset and returns IMEI as string and error. If len is 15 chars, also do imei validation
Example ¶
// Example packet Teltonika UDP Codec 8 007CCAFE0133000F33353230393430383136373231373908020000016C32B488A0000A7A367C1D30018700000000000000F1070301001500EF000342318BCD42DCCE606401F1000059D9000000016C32B48C88000A7A367C1D3001870000000000000015070301001501EF0003423195CD42DCCE606401F1000059D90002, IMEI is located starting byte 8 var bs = []byte{0x00, 0x7C, 0xCA, 0xFE, 0x01, 0x33, 0x00, 0x0F, 0x33, 0x35, 0x32, 0x30, 0x39, 0x34, 0x30, 0x38, 0x31, 0x36, 0x37, 0x32, 0x31, 0x37, 0x39, 0x08} imei, err := ParseIMEI(&bs, 8, 15) if err != nil { fmt.Println("ExampleParseIMEI error", err) } fmt.Printf("%T %v", imei, imei)
Output: string 352094081672179
func ValidateIMEI ¶
ValidateIMEI takes pointer to 15 digits long IMEI string, calculate checksum using the Luhn algorithm and return validity as bool
Example ¶
imei := "352094081852920" fmt.Println(ValidateIMEI(&imei))
Output: true
Types ¶
This section is empty.