1

I'm trying to to convert a simple hash-function from C to Go.

What is the difference between these C and Go scripts and how do I fix the Go code?

C -> Results in {FB;01;4C|64:KDY;KMT;KYR;KT0;TKK;PAC;UD01;UD02;UD03;ID01;ID02;ID03;SYS|124A}

int   sum;
char* pChar;
char  s[8];

msg = "{FB;01;4C|64:KDY;KMT;KYR;KT0;TKK;PAC;UD01;UD02;UD03;ID01;ID02;ID03;SYS|"
sum = 0;
pChar = msg + 1; // sum starts after the opening {
while (*pChar != 0) {
  sum += (int)*pChar++;
}
sprintf(s, "%04X}", sum);
strcat(msg, s);

Go -> Results in {FB;01;4C|64:KDY;KMT;KYR;KT0;TKK;PAC;UD01;UD02;UD03;ID01;ID02;ID03;SYS|004A}

msg := "{FB;01;4C|64:KDY;KMT;KYR;KT0;TKK;PAC;UD01;UD02;UD03;ID01;ID02;ID03;SYS|"
var sum uint8
for i := 1; i < len(msg); i++ {
    sum += msg[i]
}
s := fmt.Sprintf("%04X}", sum)
req := strings.Join([]string{msg, s}, "")
fmt.Println(req)
2
  • sum in your C code has type int, which is at least a 16-bit signed integer (but more likely to be a 32-bit signed integer). In your Go code, you made sum an 8-bit unsigned integer (uint8). Also, if that's your actual C code, you're using strcat with a string literal as the first parameter, meaning it cannot be modified, so whatever program is using that piece of code will likely halt execution. Commented Aug 19, 2018 at 13:15
  • Excellent, please provide this as an answer. However, how can I convert that to bytes? invalid operation: sum += msg[i] (mismatched types uint32 and byte) Commented Aug 19, 2018 at 14:05

2 Answers 2

4

You need to make "var sum" an "uint16", otherwise it'll never go above 00FF.

Sign up to request clarification or add additional context in comments.

1 Comment

thx, that's to the point. Chrono's answer was more detailed but you are perfectly right
2

In your C code, the type of sum is int, which is a signed integer type at least 16 bits in size1.

However, the type of sum is uint8 in your Go code, an unsigned integer type limited to 8 bits.

Judging by your format string %04X}, you probably are wanting a 16-bit value.

To fix the Go code, just change uint8 to int and use sum += int(msg[i]) to make the compiler happy. If you want to keep the value of sum strictly 16-bit, you could use uint16 instead and sum += uint16(msg[i]).

If you're wondering why you need to wrap msg[i] in uint16(...), it's because you're converting the value to a different type. C has "integer promotion" rules that automatically convert a value that has a width less than int to a value of type int. Go, however, has no such rules and simply refuses to compile, stating that the types are incompatible.


By the way, you could simply do this in your Go code due to its automatic memory management:

req := msg + fmt.Sprintf("%04X}", sum)
fmt.Println(req)

or even:

s := fmt.Sprintf("%04X}", sum)
fmt.Println(msg + sum)

Nothing wrong with your current approach; it's just extra verbose.


1 int is required to be at least 16-bit in C and at least 32-bit in Go, but it is commonly 32-bit these days in both languages. You should be aware, however, that it could be 64-bit on some current systems and could very well be 64-bit by default at some point in the future for either language; they're not required to keep their data type sizes synchronized. You should keep this in mind when adding the values in msg to ensure the value of sum is 16-bit (or just use uint16).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.