英文:
Go decode ssl serial number
问题
我有一个由其他应用程序以二进制编码的 SSL 序列号 7b:c9:91:be:0b:be:08:2f:3a:97:60:84:f3:f8:4a:f2:d4:30:57:e2,我想将其从二进制解码为字符串形式。也许有一个库或一组函数可以实现这个功能。
英文:
I have ssl serial number 7b:c9:91:be:0b:be:08:2f:3a:97:60:84:f3:f8:4a:f2:d4:30:57:e2, binary encoded by other application in \u001c\ufffdدc\ufffd\u001e\ufffd\ufffd\ufffdN\ufffdhr\u001a\ufffd䶓\ufffd
How can I decode ssl serial number from binary to string view? Maybe there is a library or a set of functions for this?
答案1
得分: 1
对于最基本的情况,你需要像这样的代码:https://go.dev/play/p/gp74V27lmAv
它遍历二进制编码的串口数据的每个字节,并将其转换为十六进制。它使用“:”作为字节之间的分隔符。
这段代码只是一个示例,可以进行大量改进,特别是如果你知道这些串口数据的长度。然后,你可以通过传递各个字节来使用单个fmt.Sprintf()
调用进行转换,例如如果你的串口数据长度为6个字节,可以使用fmt.Sprintf("%x:%x:%x:%x:%x:%x", b[0], b[1], b[2], b[3], b[4], b[5])
。当然,你首先需要进行所有相关的验证。
英文:
For the most basic case you need something like this: https://go.dev/play/p/gp74V27lmAv
It goes over each byte of the binary encoded serial and converts it to hex. It separates the bytes with a ":" delimiter.
The code is an example and can be vastly improved, especially if you know how long those serials are. Then you can convert them with a single fmt.Sprintf()
call by passing the individual bytes, e.g. fmt.Sprintf("%x:%x:%x:%x:%x:%x", b[0], b[1], b[2], b[3], b[4], b[5])
if your serial is 6 bytes long. You will obviously need to do all the relevant validation first.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论