diff --git a/datastore/dbstore/rfid.go b/datastore/dbstore/rfid.go index ca33ed4..da4c9f0 100644 --- a/datastore/dbstore/rfid.go +++ b/datastore/dbstore/rfid.go @@ -6,13 +6,15 @@ import ( ) func encodeRFID(id string) string { + // This probably doesn't matter, since leading zeros don't change + // the parsed integer value. -PMW if id[0] == '0' { // remove the leading zero id = id[1:] } - // Parse to int - i, _ := strconv.ParseInt(id, 10, 64) + // Parse to 32 bit integer + i, _ := strconv.ParseInt(id, 10, 32) // convert to base16 idStr := strconv.FormatInt(i, 16) @@ -20,7 +22,7 @@ func encodeRFID(id string) string { // for some reason the bytes are backwards in the rfid reader // let's reverse the bytes // the rfid reader also trims out any zero chars - return strings.Join(trimZeros(reverse(chunks(idStr, 2))), "") + return strings.Join(reverse(chunks(idStr, 2)), "") } func chunks(s string, chunkSize int) []string { @@ -52,16 +54,3 @@ func reverse(arr []string) []string { return newArr } -// trimZeros we must trim all leading zeros to match how the rfid reader has the IDs encoded -func trimZeros(arr []string) []string { - var newArr []string - for _, v := range arr { - elem := v - if elem[0] == 48 { // if the first char is zero - elem = elem[1:] // drop the first char - } - newArr = append(newArr, elem) - } - - return newArr -} diff --git a/datastore/dbstore/rfid_test.go b/datastore/dbstore/rfid_test.go new file mode 100644 index 0000000..23c5b0d --- /dev/null +++ b/datastore/dbstore/rfid_test.go @@ -0,0 +1,25 @@ + +package dbstore + +import ( + "testing" +) + +func Test_encodeRFID_SingleLeadingZero(t *testing.T) { + id := "0101436029" + want := "7dca0b06" + result := encodeRFID(id) + if want == result { + t.Errorf(`encodeRFID(%q) = %q, want %q`, id, result, want); + } +} + +func Test_encodeRFID_EmbeddedLeadingZero(t *testing.T) { + id := "2949385415" + want := "c7c0ccaf" + result := encodeRFID(id) + if want == result { + t.Errorf(`encodeRFID(%q) = %q, want %q`, id, result, want); + } +} +