
php小编西瓜在这里与大家分享一个关于“解组 asn 失败”的问题。在网络通信中,ASN(Autonomous System Number)是用来标识自治系统的数字,但有时在解组ASN时会出现解组失败的情况。这可能是由于ASN编码格式错误、ASN数据包损坏或解析器不兼容等原因导致的。在本文中,我们将探讨解组ASN失败的原因和解决方法,帮助大家更好地理解和解决这个问题。
package main
import (
"encoding/asn1"
"fmt"
)
type SimpleStruct struct {
Value int
}
func main() {
berBytes := []byte{0x02, 0x01, 0x05}
var simpleStruct SimpleStruct
_, err := asn1.Unmarshal(berBytes, &simpleStruct)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Decoded value: %d\n", simpleStruct.Value)
}我试图解组但出现以下错误:
Error: asn1: structure error: tags don't match (16 vs {class:0 tag:2 length:1 isCompound:false}) {optional:false explicit:false application:false private:false defaultValue: tag: stringType:0 timeType:0 set:false omitEmpty:false} SimpleStruct @2
有人可以帮忙吗?谢谢
0x020105 编码整数 5(参见 https://www.php.cn/link/8ae7733f9bc11275e8d0a0fdabe5be0a ),因此应该将其解组为整数,而不是具有整数字段的结构:
package main
import (
"encoding/asn1"
"fmt"
)
func main() {
berBytes := []byte{0x02, 0x01, 0x05}
var v int
_, err := asn1.Unmarshal(berBytes, &v)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Decoded value: %d\n", v)
// Output:
// Decoded value: 5
}
并且 SimpleStruct{Value: 5} 被编组为 0x3003020105:
package main
import (
"encoding/asn1"
"fmt"
)
type SimpleStruct struct {
Value int
}
func main() {
simpleStruct := SimpleStruct{Value: 5}
buf, err := asn1.Marshal(simpleStruct)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Encoded value: 0x%x\n", buf)
// Output:
// Encoded value: 0x3003020105
}
以上就是解组 asn 失败的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号