3

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!

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
user3220657
  • 45
  • 1
  • 6

4 Answers4

2

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\r\x033D\r\n"
    //fmt.Println(data)
    fmt.Println(ASTMCheckSum(data))
}

Result:

3D

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

ANisus
  • 74,460
  • 29
  • 162
  • 158
  • Really thanks for answering ! didn't know that using a single quotation will change a string to a code point. And thanks for the implementation for astm including the special characters too ! Really thanks for your time and please have a nice day! – user3220657 Mar 17 '14 at 06:45
  • Yepp, single quote means [rune literal](http://golang.org/ref/spec#Rune_literals). Comes in handy now and then. Also, you might consider adding error checks such as when no ETX or ETB's are found and so on. And you are welcome. Happy Go coding! :) – ANisus Mar 17 '14 at 06:55
1

You can use ParseInt from the strconv package.

ParseInt interprets a string s in the given base (2 to 36) and returns the corresponding value i. If base == 0, the base is implied by the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    start := "a"
    result, err := strconv.ParseInt(start, 16, 0)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%x", result+1)
}

Playground

Intermernet
  • 18,604
  • 4
  • 49
  • 61
  • Really thanks for answering ! Sorry for my unclear explanation, actually i need to covert words like "hijklmn" one by one to hex and get the total sum value of it . Thanks for teaching me about ParseInt that i can use in other place.Thanks for your time and please have a nice day ! – user3220657 Mar 17 '14 at 06:07
0

For example,

package main

import "fmt"

func ASTMCheckSum(data []byte) []byte {
    cs := byte(0)
    for _, b := range data {
        cs += b
    }
    return []byte(fmt.Sprintf("%02X", cs))
}

func main() {
    data := []byte{0x01, 0x08, 0x1f, 0xff, 0x07}
    fmt.Printf("%x\n", data)
    cs := ASTMCheckSum(data)
    fmt.Printf("%s\n", cs)
}

Output:

01081fff07
2E
peterSO
  • 158,998
  • 31
  • 281
  • 276
  • Really thanks for answering ! This exactly solved my problem ! Been spending the whole day try to figure it out but couldn't solve it myself. Really thanks a lot !! – user3220657 Mar 17 '14 at 06:13
0

You do not want to "convert a character to hex" because hexadecimal (and decimal and binary and all other base-N representations of integers) are here for displaying numbers to humans and consuming them back. A computer is free to actually store the number it operates on in any form it wishes; while most (all?) real-world computers store them in binary form—using bits, they don't have to.

What I'm leading you to, is that you actually want to convert your character representing a number using hexadecimal notation ("display form") to a number (what computers operate on). For this, you can either use the strconv package as already suggested or roll your own simple conversion code. Or you can just grab one from the encoding/hex standard package—see its fromHexChar function.

kostix
  • 51,517
  • 14
  • 93
  • 176
  • Really thanks for answering my question ! Thanks for leading me to the correct point. I think i was misunderstood or less knowledge on the actual hex and integer that how they are going on. I will re learn about it base on the information that you have pointed and provided me. Thanks a lot ! – user3220657 Mar 17 '14 at 06:57