Generating a simple 2 byte checksum

2,091 views
Skip to first unread message

pietersyd...@gmail.com

unread,
Nov 25, 2014, 12:41:37 PM11/25/14
to golan...@googlegroups.com
I am having a hard time implementing a checksum in golang.

The checksum should sum [n]Bytes, mod the total with 256 and return the result as [2]byte

Checksum = ( 0x30 + 0x30 + 0x30 + 0x37 ) mod 0x0100 = 0xC7

Return :

byte[0] should be C  = 0x43 
byte[1] should be 7  = 0x37

Can you please guide me here. This is what I have sofar:


func generateCheckSum(bytesArr []byte) (checkSum1, checkSum2 byte) {

total := 0

for _,value := range bytesArr {
total += int(value)
}

mod := (total % 0x0100)

//From here I don't know how to split the 'mod' for checkSum1 and checkSum2

return
    
}

Regards
Pieter

Jérôme Champion

unread,
Nov 25, 2014, 12:58:21 PM11/25/14
to golan...@googlegroups.com, pietersyd...@gmail.com
If you mod the result with 256, why do you need to return two bytes?

pietersyd...@gmail.com

unread,
Nov 25, 2014, 1:10:57 PM11/25/14
to golan...@googlegroups.com, pietersyd...@gmail.com
Hi Jérôme,

I need to generate a checksum to communicate over serial with a connected device. It is sent over serial with the rest of the packet to verify that nothing changed. 

The checksum should take up 2 bytes as shown below.






Matthew Zimmerman

unread,
Nov 25, 2014, 1:53:52 PM11/25/14
to pietersyd...@gmail.com, golang-nuts
http://play.golang.org/p/N79eeR-Sco ?

uint16 is effectively 2 bytes

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pietersyd...@gmail.com

unread,
Nov 25, 2014, 2:03:28 PM11/25/14
to golan...@googlegroups.com, pietersyd...@gmail.com
Hi Matthew,

Thanks for the reply.

This snippet posted by Daniel shows the required output. But there is one more step required after it. 

I need to convert C to '0x43' and 7 to '0x37' their byte equivalents.


Regards

Daniel Ortiz Pereira da Silva

unread,
Nov 25, 2014, 2:52:34 PM11/25/14
to golan...@googlegroups.com, pietersyd...@gmail.com
Hi Pieter,

I think this solve your problem.

Nick Patavalis

unread,
Nov 25, 2014, 5:56:54 PM11/25/14
to golan...@googlegroups.com, pietersyd...@gmail.com
Much simpler (and faster):

Uli Kunitz

unread,
Nov 25, 2014, 5:59:49 PM11/25/14
to golan...@googlegroups.com, pietersyd...@gmail.com
The task appears to be to add the bytes of a sequence modulo 256 and return the resulting byte as a hexadecimal string of two bytes.

Note that bytes are added always modulo 256 in Go. Creating the hexadecimal presentation of a byte and returning the string can be done by fmt.Sprintf. So the whole task can be done by this simple program.


The string can be converted to a byte slice but this requires an allocation and an additional copy, but it doesn't appear to be required.

Nick Patavalis

unread,
Nov 25, 2014, 6:04:04 PM11/25/14
to golan...@googlegroups.com, pietersyd...@gmail.com
You don't need fmt.Printf() for something as simple as this...

(see previous post)

/npat

Nick Patavalis

unread,
Nov 25, 2014, 6:06:47 PM11/25/14
to golan...@googlegroups.com, pietersyd...@gmail.com
Marginally improved:

Uli Kunitz

unread,
Nov 25, 2014, 6:10:39 PM11/25/14
to golan...@googlegroups.com, pietersyd...@gmail.com
Sure, but this way I cannot introduce a bug.

Your solution is also not perfect; a const hex = "0123456789ABCDEF" will be sufficient. The byte slice conversion will require an allocation in each call of the function.

Nick Patavalis

unread,
Nov 25, 2014, 6:16:00 PM11/25/14
to golan...@googlegroups.com, pietersyd...@gmail.com
On Wednesday, November 26, 2014 1:10:39 AM UTC+2, Uli Kunitz wrote:
Sure, but this way I cannot introduce a bug.

Your solution is also not perfect; a const hex = "0123456789ABCDEF" will be sufficient. The byte slice conversion will require an allocation in each call of the function.

Yep, see previous post (Though I'm pretty sure the slice would go to the stack. Still const hex="..." is better).

What bug?

/npat

Reply all
Reply to author
Forward
0 new messages