I've got a UUID (128-bit number) represented as a decimal number and I need to parse it into numeric form in C. My target data structure is a char[16] and using a bignum library is not an option. Can someone point me to a suitable algorithm?
Asked
Active
Viewed 1,226 times
1
-
2When you work with uuids then you work with RPC. Surely you've got a runtime support library for it. The common name for the helper function is UuidFromString. – Hans Passant Feb 23 '11 at 21:17
-
1That would parse a uuid in the normal format (aaaabbbb-cccc-dddd-eeee-ffffgggghhhh, hexadecimal). "Represented as a decimal number" is not the normal format. – zwol Feb 23 '11 at 21:38
-
4You have a string containing the decimal representation? Where did this come from? I'd file a complaint! – David Heffernan Feb 23 '11 at 21:43
-
This is for an ASN.1 OID parser. The {joint-iso-itu-t(2) uuid(25)} OID arc allows registration of a UUID but ASN.1 requires decimal representation of arc IDs. So, for example, the "motorola" arc with UUID 3be753a0-051d-11de-8329-0002a5d5c51b is shown as {joint-iso-itu-t(2) uuid(25) motorola(79625568443717255337188391839044322587)} – Trevor Feb 23 '11 at 21:46
-
This has been asked here before. Have a look at: http://stackoverflow.com/questions/2652760/ – David Heffernan Feb 23 '11 at 21:51
1 Answers
1
The naïve algorithm is quite simple:
char number[] = "79625568443717255337188391839044322587";
unsigned char uuid[16] = { 0 };
for (char* n = number; *n; n++) {
unsigned c = *n - '0';
for (int i = 0; i < 16; i++) {
c += uuid[i] * 10;
uuid[i] = c % 256;
c /= 256;
}
}
This is simpler than a typical bignum operation because the multiplier 10 is smaller than the unit size and the number length is fixed.
You could speed it up by working in larger units than char (e.g. uint32_t
), as long as c
is larger still (e.g. uint64_t
), though I doubt it's necessary.

aaz
- 5,136
- 22
- 18