dongxuxian6930 2014-03-16 20:57
浏览 491
已采纳

Golang上的十六进制总和

Thanks for reading my question. I am trying to count ASTM checksum on Golang but couldn't figure it out how to convert string or byte to hexadecimal that is countable by myself and Google. Please let me request help, thanks.

At Golang, how to convert a character to hexadecimal that can allow performing a sum?

Example:

// Convert character "a" to hex 0x61 ( I understand this will not work for my case as it became a string.)
hex := fmt.Sprintf("%x","a")
// sum the 0x61 with 0x01 so it will become 0x62 = "b"
fmt.Printf("%v",hex + 0x01)

Thank you so much and please have a nice day.

Thanks for everyone answering my question! peterSO and ANisus answers both solved my problem. Please let me choose ANisus's reply as answer as it including ASTM special character in it. I wish StackOverflow could choose multiple answers. Thanks for everybody answering me and please have a nice day!

  • 写回答

4条回答 默认 最新

  • duanma8207 2014-03-16 21:25
    关注

    Intermernet's answer shows you how to convert a hexadecimal string into an int value.

    But your question seems to suggest that you want to want to get the code point value of the letter 'a' and then do aritmetics on that value. To do this, you don't need hexadecimal. You can do the following:

    package main
    
    import "fmt"
    
    func main() {
        // Get the code point value of 'a' which is 0x61
        val := 'a'
    
        // sum the 0x61 with 0x01 so it will become 0x62 = 'b'
        fmt.Printf("%v", string(val + 0x01))
    }
    

    Result:

    b

    Playground: http://play.golang.org/p/SbsUHIcrXK

    Edit:

    Doing the actual ASTM checksum from a string using the algorithm described here can be done with the following code:

    package main
    
    import (
        "fmt"
    )
    
    const (
        ETX = 0x03
        ETB = 23
        STX = 0x02
    )
    
    func ASTMCheckSum(frame string) string {
    
        var sumOfChars uint8
    
        //take each byte in the string and add the values
        for i := 0; i < len(frame) ; i++ {      
            byteVal := frame[i]
            sumOfChars += byteVal
    
            if byteVal == STX {
                sumOfChars = 0
            }
    
            if byteVal == ETX || byteVal == ETB {
                break
            }
        }
    
        // return as hex value in upper case
        return fmt.Sprintf("%02X", sumOfChars)
    }
    
    func main() {
        data := "\x025R|2|^^^1.0000+950+1.0|15|||^5^||V||34001637|20080516153540|20080516153602|34001637\x033D
    "
        //fmt.Println(data)
        fmt.Println(ASTMCheckSum(data))
    }
    

    Result:

    3D

    Playground: http://play.golang.org/p/7cbwryZk8r

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 centos7.6进不去系统,卡在数字7界面
  • ¥15 Tensorflow采用interpreter.allocate_tensors()分配内存出现ValueError: vector too long报错
  • ¥15 使用CGenFF在线生成血红素辅基拓扑结构遇到问题
  • ¥15 在fragment使用okhttp同步上传文件,能不能在fragment销毁后还可以继续上传文件?
  • ¥20 matlab代码实现可达矩阵形成骨骼矩阵
  • ¥15 关于地板的木纹和图库中的匹配的
  • ¥30 机器学习预测疾病模型流程疑问
  • ¥50 2048Python实现
  • ¥15 使用ads进行低噪放仿真没有结果且不报错
  • ¥15 关于#python#的问题:有偿求一个千寻框架找书机器人插件
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部