check version 0.1.1 for more readable code
| 31 | |
| 32 | // check version 0.1.1 for more readable code |
| 33 | void UUID::generate() |
| 34 | { |
| 35 | uint32_t ar[4]; |
| 36 | for (uint8_t i = 0; i < 4; i++) |
| 37 | { |
| 38 | ar[i] = _random(); |
| 39 | // store binary version globally ? |
| 40 | // _ar[i] = ar[i]; |
| 41 | } |
| 42 | |
| 43 | // Conforming to RFC 4122 Specification |
| 44 | // - byte 7: four most significant bits ==> 0100 --> always 4 |
| 45 | // - byte 9: two most significant bits ==> 10 --> always {8, 9, A, B}. |
| 46 | // |
| 47 | // patch bits for version 1 and variant 4 here |
| 48 | if (_mode == UUID_MODE_VARIANT4) |
| 49 | { |
| 50 | ar[1] &= 0xFFF0FFFF; // remove 4 bits. |
| 51 | ar[1] |= 0x00040000; // variant 4 |
| 52 | ar[2] &= 0xFFFFFFF3; // remove 2 bits |
| 53 | ar[2] |= 0x00000008; // version 1 |
| 54 | } |
| 55 | |
| 56 | |
| 57 | // process 16 bytes build up the char array. |
| 58 | for (uint8_t i = 0, j = 0; i < 16; i++) |
| 59 | { |
| 60 | // multiples of 4 between 8 and 20 get a -. |
| 61 | // but note we are doing 2 digits in one loop. |
| 62 | if ((i & 0x1) == 0) |
| 63 | { |
| 64 | if ((4 <= i) && (i <= 10)) _buffer[j++] = '-'; |
| 65 | } |
| 66 | |
| 67 | // process one byte at the time instead of a nibble |
| 68 | uint8_t nr = i / 4; |
| 69 | uint8_t xx = ar[nr]; |
| 70 | uint8_t ch = xx & 0x0F; |
| 71 | _buffer[j++] = (ch < 10) ? '0' + ch : ('a' - 10) + ch; |
| 72 | |
| 73 | ch = (xx >> 4) & 0x0F; |
| 74 | ar[nr] >>= 8; |
| 75 | _buffer[j++] = (ch < 10) ? '0' + ch : ('a' - 10) + ch; |
| 76 | } |
| 77 | _buffer[36] = 0; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | char * UUID::toCharArray() |