b2n

package module
v0.0.0-...-22fb58c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2019 License: BSD-3-Clause Imports: 2 Imported by: 12

README

Package b2n provides functions for parsing and conversion values from byte arrays and slices

Certain purpose:

Package b2n was created for parsing data from Teltonika UDP packets, package can be used for parsing values from data streams.

For example, have binary packet bs which is Teltonika UDP Codec 8 Extended packet according to the Teltonika Codec 8 documentation, on byte position 24 should be number of data which should be unsigned uint8, use ParseBs2Uint8 function to parse and convert the value.

package main

import (
    "encoding/hex"
    "fmt"

    "github.com/filipkroca/b2n"
)

func main() {
    // create HEX string raw
    var dataString = "00A1CAFE001B000F3335363330373034323434313031338E010000013FEBDD19C8000F0E9FF0209A718000690000120000001E09010002000300040016014703F0001504C8000C0900910A00440B004D130044431555440000B5000BB60005422E9B180000CD0386CE000107C700000000F10000601A460000013C4800000BB84900000BB84A00000BB84C00000000024E0000000000000000CF000000000000000001"

    // decode sting into a byte slice
    bs, _ := hex.DecodeString(dataString)

    // parse a value on Byte offset 24, is should be number of data according to the Teltonika documentation
    noOfData, err := b2n.ParseBs2Uint8(&bs, 24)
    if err != nil {
        fmt.Printf(err)
        return
    }

    fmt.Println("%T %v", noOfData)
}

Output should be

uint8 1

Full documentation HERE

Bytes slice to unsigned integer

ParseBs2Uint8

ParseBs2Uint8 takes a pointer to a byte slice, start byte and returns parsed Uint8 and error

Performance per core: 0.46 ns/op, 0 B/op, 0 allocs/op

DOCUMENTATION EXAMPLE

ParseBs2Uint16

ParseBs2Uint16 takes a pointer to a byte slice, start byte and returns parsed Uint16 and error

Performance per core: 3.35 ns/op, 0 B/op, 0 allocs/op

DOCUMENTATION EXAMPLE

ParseBs2Uint32

ParseBs2Uint32 takes a pointer to a byte slice, start byte and returns parsed Uint32 and error

Performance per core: 4.97 ns/op, 0 B/op, 0 allocs/op

DOCUMENTATION EXAMPLE

Bytes slice encoded with Two's complement to signed integer

Read more here Two's complement

ParseBs2Int8TwoComplement

ParseBs2Int8TwoComplement takes a pointer to a byte slice coded with Two Complement Arithmetic, start byte and returns parsed signed Int8 and error

Performance per core: 0.24 ns/op, 0 B/op, 0 allocs/op

DOCUMENTATION EXAMPLE

ParseBs2Int16TwoComplement

ParseBs2Int16TwoComplement takes a pointer to a byte slice coded with Two Complement Arithmetic, start byte and returns parsed signed Int16 and error

Performance per core: 4.52 ns/op, 0 B/op, 0 allocs/op

DOCUMENTATION EXAMPLE

ParseBs2Int32TwoComplement

ParseBs2Int32TwoComplement takes a pointer to a byte slice coded with Two Complement Arithmetic, start byte and returns parsed signed Int32 and error

Performance per core: 7.48 ns/op, 0 B/op, 0 allocs/op

DOCUMENTATION EXAMPLE

ParseBs2Int64TwoComplement

ParseBs2Int64TwoComplement takes a pointer to a byte slice coded with Two Complement Arithmetic, start byte and returns parsed signed Int64 and error

Performance per core: 11.1 ns/op, 0 B/op, 0 allocs/op

DOCUMENTATION EXAMPLE

IMEI number functions

This functions provide support for parsing and validating International Mobile Equipment Identity

For example, have binary packet bs which is Teltonika UDP Codec 8 packet

package main

import (
    "encoding/hex"
    "fmt"

    "github.com/filipkroca/b2n"
)

func main() {
    //Example packet Teltonika UDP Codec 8 007CCAFE0133000F33353230393430383136373231373908020000016C32B488A0000A7A367C1D30018700000000000000F1070301001500EF000342318BCD42DCCE606401F1000059D9000000016C32B48C88000A7A367C1D3001870000000000000015070301001501EF0003423195CD42DCCE606401F1000059D90002, IMEI is located starting byte 8

    // create a raw byte slice
    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}

    //parse and validate imei
    imei, err := ParseIMEI(&bs, 8, 15)
    if err != nil {
        fmt.Println("ExampleParseIMEI error", err)
    }
    fmt.Printf("%T %v", imei, imei)

}

According to the Teltonika Codec 8 documentation, on byte position 8 should be 15 digits long IMEI number. After parsing and validating

Output should be

string 352094081672179
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 digits, also do imei validation

Performance per core 235 ns/op, 16 B/op, 1 allocs/op

DOCUMENTATION EXAMPLE

ValidateIMEI

ValidateIMEI takes pointer to 15 digits long IMEI string, calculate checksum using the Luhn algorithm and return validity as bool

Performance per core 193 ns/op, 0 B/op, 0 allocs/op

DOCUMENTATION EXAMPLE

Documentation

Overview

Package b2n provides functions for parsing and conversion values from byte arrays and slices home page https:// github.com/filipkroca/b2n

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseBs2Int16TwoComplement

func ParseBs2Int16TwoComplement(bs *[]byte, offset int) (int16, error)

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

func ParseBs2Int32TwoComplement(bs *[]byte, offset int) (int32, error)

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

func ParseBs2Int64TwoComplement(bs *[]byte, offset int) (int64, error)

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

func ParseBs2Int8TwoComplement(bs *[]byte, offset int) (int8, error)

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

func ParseBs2Uint16(bs *[]byte, offset int) (uint16, error)

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

func ParseBs2Uint32(bs *[]byte, offset int) (uint32, error)

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

func ParseBs2Uint64(bs *[]byte, offset int) (uint64, error)

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

func ParseBs2Uint8(bs *[]byte, offset int) (uint8, error)

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

func ParseIMEI(bs *[]byte, offset int, length int) (string, error)

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

func ValidateIMEI(imei *string) bool

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL