Thursday, January 16, 2025
HomeGolangCannot work out the right way to parse buffered array - Getting...

Cannot work out the right way to parse buffered array – Getting Assist


That is how the info is available in

[2 68 52 50 58 49 48 32]
[32 48 32 32 48 32 32 48]
[48 49 48 49 48 32 32 32]
[32 3 120]
[2 68 52 50 58 49 49 32]
[32 48 32 32 48 32 32 48]
[48 49 48 49 48 32 32 32]
[32 3 121]
[2 68 52 50 58 49 50 32]
[32 48 32 32 48 32 32 48]
[48 49 48 49 48 32 32 32]
[32 3 122]
[2 68 52 50 58 49 51 32]
[32 48 32 32 48 32 32 48]
[48 49 48 49 48 32 32 32]
[32 3 123]
[2 68 52 50 58 49 52 32]
[32 48 32 32 48 32 32 48]
[48 49 48 48 48 32 32 32]
[32 3 125]
[2 68 52 50 58 49 52 32]
[32 48 32 32 48 32 32 48]
[48 49 51 48 48 32 32 32]
[32 3 126 2 68 52 50 58 49 52 32 32 48 32 32 48]
[32 32 48 48 49 51 48 48]
[32 32 32 32 3 126]
[2 68 52 50 58 49 52 32]
[32 48 32 32 48 32 32 48]
[48 49 51 48 48 32 32 32]
[32 3 126]
[2 68 52 50 58 49 52 32]
[32 48 32 32 48 32 32 48]
[48 49 48 49 48 32 32 32]
[32 3 124 2 68 52 50 58 49 52 32 32 48 32 32 48]
[32 32 48 48 49 48 49 48]
[32 32 32 32 3 124]
[2 68 52 50 58 49 53 32]
[32 48 32 32 48 32 32 48]
[48 49 48 49 48 32 32 32]
[32 3 125]

That is how I demonstrated this

conn, err := web.Dial("tcp", "192.168.2.64:9001")

	if err != nil {
		fmt.Println("Connection not established")
	}

	fmt.Println(conn.LocalAddr())

	for {
		buffer := make([]byte, 1024)

		bytesRead, err := conn.Learn(buffer)

		if err != nil {
			fmt.Println(err)
		}

		fmt.Println(buffer[:bytesRead])

	}

That is this system I’m utilizing to try to parse it.

package deal essential

import (
	"bufio"
	"fmt"
	"io"
	"web"
)

func calculateXORChecksum(information []byte) byte {
	var checksum byte
	for _, b := vary information {
		checksum ^= b
	}
	return checksum
}

func findByte(information []byte) int {

	for index, aByte := vary information {
		if aByte == 2 {
			return index
		}
	}
	return -1
}
func readSTXETXWithChecksum(conn web.Conn) ([]byte, byte, error) {
	reader := bufio.NewReader(conn)

	// Learn till we get the ETX (ASCII 3) character
	message, err := reader.ReadBytes(3)
	if err != nil {
		return []byte(""), 0, err
	}

	checksumByte, err := reader.ReadByte()
	if err != nil {
		return []byte(""), 0, err

	}

	information := append(message, checksumByte) // All characters together with the checksum

	return information, checksumByte, nil
}

func essential() {
	conn, err := web.Dial("tcp", "192.168.2.64:9001")

	if err != nil {
		fmt.Println("Connection not established")
	}

	fmt.Println(conn.LocalAddr())

	for {
		fmt.Println()

		information, _, err := readSTXETXWithChecksum(conn)
		if err == io.EOF {
			fmt.Println("Connection closed by the peer")
		} else if err != nil {
			fmt.Println("Error studying message:", err)
		} else {
			fmt.Println("Acquired information:", []byte(information))

			fmt.Println("Acquired information:", string([]byte(information)[:len(data)-1]))

			fmt.Println()
		}

	}

}

The issue is after I get bytes array akin to [32 3 126 2 68 52 50 58 49 52 32 32 48 32 32 48] something after 3 and checksum byte will get discarded. This is because of the truth that in every itteration of the for loop a brand new scanner is asserted. Nonetheless if I declare a reader outdoors the for loop the buffer array is gonna get stuffed up sooner or later and there could also be lacking information as a brand new one is asserted when it’s full.

I really feel fairly silly for not with the ability to remedy one thing that appears easy.
I attempted one thing guide however cant get it proper and really feel like it’s not the precise strategy

buffer := make([]byte, 1024)
	var aLine []byte

	var remaining []byte
	for {

		numberRead, err := conn.Learn(buffer)

		if err != nil {
			fmt.Println("the error")
		}

		if len(remaining) != 0 {
			aLine = append(remaining, buffer[:numberRead]...)
		}
		// fmt.Println(aLine)

		foundEnd := findByte(buffer[:numberRead])
		if foundEnd != -1 {

			aLine = append(aLine, buffer[:foundEnd+1]...)
			fmt.Println(aLine)
			aLine = nil
			if numberRead > foundEnd+1 {
				leftover := buffer[foundEnd+1 : numberRead]
				remaining = append(remaining, leftover...)
			}

		} else {
			aLine = append(aLine, buffer[:numberRead]...)
		}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments