# attiny 412 serial number

**URL:** <https://forum.arduino.cc/t/attiny-412-serial-number/680241>\
**Category:** 3rd Party Boards\
**Created:** [October 27, 2020, 4:24pm UTC](https://forum.arduino.cc/t/attiny-412-serial-number/680241 "2020-10-27T16:24:21Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [October 28, 2020, 2:48am UTC](https://forum.arduino.cc/t/attiny-412-serial-number/680241/2 "2020-10-28T02:48:49Z")

</div>

Your strategy is fine, but your code has a bug, and is more complicated than it needs to be.  
The usual problem with using the built-in serial number is that it isn't the same size (bytes) as the "unique IDs" that you might need (ie ethernet addresses, which are 6 bytes.)

The bug:

```cpp
  msg[21]=0x00;    //zero terminate string for tx library

```

arrays are 0-based, so the 21st element of msg is msg[20]; Right now, you're overwriting data one past the end of the array. Who knows what that'll do...  
Simpler:  
The chip serial number is is normal memory, and is sequential, so you can eliminate the intermediate array and do something like:

```cpp
#define SERNUM_SIZE 10  // Number of bytes in the on-chip serial number
byte msg[SERNUM_SIZE*2 + 1];  //array for ASCII HEX representation of serial number
const byte *serno = &SIGROW.SERNUM0;  // pointer to the sernums
  for (byte i=0;i<SERNUM_SIZE;++i) //convert 10 byte serial number to 20 byte ASCII HEX serial number
  {
    msg[2*i]=table[(serno[i]>>4) &0x0F];  //convert high nibl. Shift right 4 & mask & lookup
    msg[2*i+1]=table[serno[i] &0x0F];      //low nibl. Mask & lookup
  }
  msg[2*SERNUM_SIZE] = 0;

```

---

_[View the full topic](https://forum.arduino.cc/t/attiny-412-serial-number/680241)._
