惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Project Zero
Project Zero
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
The Register - Security
The Register - Security
C
Check Point Blog
Attack and Defense Labs
Attack and Defense Labs
L
LangChain Blog
Simon Willison's Weblog
Simon Willison's Weblog
S
Schneier on Security
Recorded Future
Recorded Future
GbyAI
GbyAI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Y
Y Combinator Blog
量子位
A
About on SuperTechFans
I
Intezer
T
Threat Research - Cisco Blogs
MongoDB | Blog
MongoDB | Blog
U
Unit 42
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 聂微东
NISL@THU
NISL@THU
The Hacker News
The Hacker News
G
Google Developers Blog
F
Full Disclosure
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Privacy & Cybersecurity Law Blog
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
T
Tenable Blog
Know Your Adversary
Know Your Adversary
Stack Overflow Blog
Stack Overflow Blog
K
Kaspersky official blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
C
Cybersecurity and Infrastructure Security Agency CISA
Martin Fowler
Martin Fowler
Schneier on Security
Schneier on Security

博客园 - 蓝天上的云℡

ARM NEON&Thumb加速指令集 事件编程之eventfd 事件驱动编程模式之EventLoop syslog日志查看器GUI图形界面,支持Windows/Linux/Macos开箱即用 ERRNO链条分析 ARM AMBA总线之AXI系 Linux Kernel Init Bootstrap 内核初始化启动 RK芯片平台GPIO控制 Linux USB子系统之Gadget设备端驱动 远程调试桥接-网络-ADB-GDB 斐讯N1盒子安装飞牛FNOS NAS 安卓以太网链路检测 安卓原生开发-Recovery使用minui绘制图形 蓝牙音频协议——安卓开发 RKxx系列的RK628摄像头配置影响HDMI出图 usb储存之BOT/UAS内核驱动 现代操作系统-音频处理技术1 Linux驱动底层 Linux驱动适配I2C/SPI例子 模块编译的pr_debug Linux内核/GKI内核主线更新-usb网卡名称变更引出的BUG 为Kernel代码生成VSCODE索引,快速跳转 安卓开机时间/性能优化 Linux/Golang/glibC系统调用 SGDMA与普通DMA Linux内核源码-存储驱动之 QSPI Flash
golang数据转换技巧
蓝天上的云℡ · 2024-04-29 · via 博客园 - 蓝天上的云℡

将uint32与byte[]互转

package main

import (
	"encoding/binary"
	"fmt"
)

func main() {
	// 一个长度为4的byte切片,表示一个负数
	bytes := []byte{0xFF, 0xFF, 0xFF, 0xFF}

	// 将byte切片转换为int32
	num := int32(binary.BigEndian.Uint32(bytes))
	fmt.Printf("Byte切片转换为Int32:%d\n", num)

	// 创建一个长度为4的byte切片
	ibytes := make([]byte, 4)

	// 将int32值写入byte切片中
	binary.BigEndian.PutUint32(ibytes, uint32(num))

	fmt.Printf("Int32写入Byte切片:%#v\n", ibytes)
}

在线运行效果 in https://go.dev/play/
image

json解析去除第一行不完整数据

    //首行数据不完整
	const jsonStream = `ft yourself!"}
	{"Name": "Ed", "Text": "Knock knock."}
	{"Name": "Sam", "Text": "Who's there?"}
	{"Name": "Ed", "Text": "Go fmt."}
	{"Name": "Sam", "Text": "Go fmt who?"}
	{"Name": "Ed", "Text": "Go fmt yourself!"}
`
	type Message struct {
		Name, Text string
	}

	strReader := strings.NewReader(data)
	bufReader := bufio.NewReader(strReader)
	_, _, err := bufReader.ReadLine()
	if err != nil {
		return
	}
	jdec := json.NewDecoder(bufReader)
	for {

		var m Message
		if err := jdec.Decode(&m); err == io.EOF {
			fmt.Println("EOF reached!")
			break
		} else if err != nil {
			fmt.Printf("decoded error: %+v\n", err)
			return
		}
		fmt.Printf("%+v\n", m)
	}
	

完整代码:

点击查看代码
package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"github.com/xluohome/serial"
	"io"
	"os"
	"strings"
)

var (
	Serial *SerialPort
)

const name = "COM1" //"/dev/ttyS1"

type SerialPort struct {
	Port *serial.Port
}

func (s *SerialPort) JsonDecoder() *json.Decoder {
	f, err := os.Open(name)
	if err != nil {
		fmt.Println("Serial port error: ", err)
	}
	reader := bufio.NewReader(f)
	return json.NewDecoder(reader)
}

type Message struct {
	//Time   string `json:"time"` // //10位十进制的unix时间戳
	Time int64 `json:"timestamp,string" ` // //10位十进制的unix时间戳
	Cat1 int   `json:"cat1,string"`
	Cat2 int   `json:"cat2,string"`
}

const data = `"}
{"timestamp":"6620727936541","cat1":"33580443","cat2":"4660"}
{"timestamp":"6620727936541","cat1":"33580443","cat2":"4660"}

`

func main() {

	test()

}

func test() {
	strReader := strings.NewReader(data)
	bufReader := bufio.NewReader(strReader)
	_, _, err := bufReader.ReadLine()
	if err != nil {
		return
	}
	jdec := json.NewDecoder(bufReader)
	jdec.DisallowUnknownFields()
	for {

		var m Message
		if err := jdec.Decode(&m); err == io.EOF {
			fmt.Println("EOF reached")
			break
		} else if err != nil {
			fmt.Printf("Serial port error: %+v\n", err)
			return
		}
		fmt.Printf("%+v\n", m)
	}

}

初始化

type MPort struct {
	Port *serial.Port
}

func (s MPort) Write(buf []byte) (n int, err error) {
	if s.Port == nil {
		s.Port = newConn()//单例
	}
	return s.Port.Write(buf)
}

可以在一个enter.go里统一完成全局变量初始化,而不需要注入

--------蓝天上的云_转载请注明出处.