Posts

Showing posts with the label Uuid

A TypeScript GUID Class?

Answer : There is an implementation in my TypeScript utilities based on JavaScript GUID generators. Here is the code: class Guid { static newGuid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } } // Example of a bunch of GUIDs for (var i = 0; i < 100; i++) { var id = Guid.newGuid(); console.log(id); } Please note the following: C# GUIDs are guaranteed to be unique. This solution is very likely to be unique. There is a huge gap between "very likely" and "guaranteed" and you don't want to fall through this gap. JavaScript-generated GUIDs are great to use as a temporary key that you use while waiting for a server to respond, but I wouldn't necessarily trust them as the primary key in a database. If you are going to rely on a JavaScript-generated GUID, I wo...

Creating A UUID From A String With No Dashes

Answer : tl;dr java.util.UUID.fromString( "5231b533ba17478798a3f2df37de2aD7" .replaceFirst( "(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5" ) ).toString() 5231b533-ba17-4787-98a3-f2df37de2ad7 Or parse each half of the hexadecimal string as long integer numbers, and pass to constructor of UUID . UUID uuid = new UUID ( long1 , long2 ) ; Bits, Not Text A UUID is a 128-bit value. A UUID is not actually made up of letters and digits, it is made up of bits. You can think of it as describing a very, very large number. We could display those bits as a one hundred and twenty eight 0 & 1 characters. 0111 0100 1101 0010 0101 0001 0101 0110 0110 0000 1110 0110 0100 0100 0100 1100 1010 0001 0111 0111 1010 1001 0110 1110 0110 0111 1110 1100 1111 1100 0101 1111 Humans do not easily read bits, so for convenience we usually represent the 128-bit value as a hexadecimal string made up...