Format 32-character string with hyphens to become UUID
<?php
//$UUID = 42f704ab-4ae1-41c7-8c18-5558f944774
$UUID = "42f704ab4ae141c78c185558f9447748";
$UUID = substr($UUID, 0, 8) . '-' . substr($UUID, 8, 4) . '-' . substr($UUID, 12, 4) . '-' . substr($UUID, 16, 4) . '-' . substr($UUID, 20);
echo $UUID;
Creating a UUID from a string with no dashes
Clojure's #uuid
tagged literal is a pass-through to java.util.UUID/fromString
. And, fromString
splits it by the "-" and converts it into two Long
values. (The format for UUID is standardized to 8-4-4-4-12 hex digits, but the "-" are really only there for validation and visual identification.)
The straight forward solution is to reinsert the "-" and use java.util.UUID/fromString
.
(defn uuid-from-string [data]
(java.util.UUID/fromString
(clojure.string/replace data
#"(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})"
"$1-$2-$3-$4-$5")))
If you want something without regular expressions, you can use a ByteBuffer
and DatatypeConverter
.
(defn uuid-from-string [data]
(let [buffer (java.nio.ByteBuffer/wrap
(javax.xml.bind.DatatypeConverter/parseHexBinary data))]
(java.util.UUID. (.getLong buffer) (.getLong buffer))))
How to Create UUID with dashes, instead of without dashes using uuid-package for Python 3.6?
You just want to convert the UUID
into a string:
>>> import uuid
>>> print(uuid.uuid1())
74ba3af4-4d6d-11ea-989f-784f435149ee
>>> u = str(uuid.uuid1())
>>> u
'7d7b626c-4d6d-11ea-989f-784f435149ee'
Note that this is clearly documented:
str(uuid)
returns a string in the form12345678-1234-5678-1234-567812345678
where the 32 hexadecimal digits represent the UUID.
Is UUID without dashes/hyphens valid?
The production in RFC 4122 (section 3, page 4), defines UUID string representation as
UUID = time-low "-" time-mid "-"
time-high-and-version "-"
clock-seq-and-reserved
clock-seq-low "-" node
where each internal component is hex-encoded.
Therefore, f47ac10b58cc4372a5670e02b2c3d479
is not a valid UUID representation.
Format raw string to Java UUID in PHP
Your question doesn't make much sense but assuming you want to read the UUID in the correct format in Java you can do something like this:
import java.util.UUID;
class A
{
public static void main(String[] args){
String input = "f9e113324bd449809b98b0925eac3141";
String uuid_parse = input.replaceAll(
"(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})",
"$1-$2-$3-$4-$5");
UUID uuid = UUID.fromString(uuid_parse);
System.out.println(uuid);
}
}
Borrowed from maerics, see here: https://stackoverflow.com/a/18987428/4195825
Or in PHP you can do something like:
<?php
$UUID = "f9e113324bd449809b98b0925eac3141";
$UUID = substr($UUID, 0, 8) . '-' . substr($UUID, 8, 4) . '-' . substr($UUID, 12, 4) . '-' . substr($UUID, 16, 4) . '-' . substr($UUID, 20);
echo $UUID;
?>
Borrowed from fico7489: https://stackoverflow.com/a/33484855/4195825
And then you can send that to Java where you can create a UUID object using fromtString()
.
PHP Format preexisting V4 UUID
You question is poorly written and the example you gave is wrong.
The last part can only have have 12 chars, also, you specified 4 groups, when in fact the string is split in 5.
You can use substr, i.e.:
<?php
$uuid = "a935941636384aa0b7560617c641cc2e";
$part1 = substr($uuid, 0, 8);
$part2 = substr($uuid, 8, 4);
$part3 = substr($uuid, 12, 4);
$part4 = substr($uuid, 16, 4);
$part5 = substr($uuid, 20, 12);
Live Demo
Efficient method to generate UUID String in Java (UUID.randomUUID().toString() without the dashes)
Ended up writing something of my own based on UUID.java implementation. Note that I'm not generating a UUID, instead just a random 32 bytes hex string in the most efficient way I could think of.
Implementation
import java.security.SecureRandom;
import java.util.UUID;
public class RandomUtil {
// Maxim: Copied from UUID implementation :)
private static volatile SecureRandom numberGenerator = null;
private static final long MSB = 0x8000000000000000L;
public static String unique() {
SecureRandom ng = numberGenerator;
if (ng == null) {
numberGenerator = ng = new SecureRandom();
}
return Long.toHexString(MSB | ng.nextLong()) + Long.toHexString(MSB | ng.nextLong());
}
}
Usage
RandomUtil.unique()
Tests
Some of the inputs I've tested to make sure it's working:
public static void main(String[] args) {
System.out.println(UUID.randomUUID().toString());
System.out.println(RandomUtil.unique());
System.out.println();
System.out.println(Long.toHexString(0x8000000000000000L |21));
System.out.println(Long.toBinaryString(0x8000000000000000L |21));
System.out.println(Long.toHexString(Long.MAX_VALUE + 1));
}
Related Topics
Differencebetween $_Files["File"]["Type"] and End(Explode(".", $_Files["File"]["Name"]))
Routes in Codeigniter - Automatically
Setting a Cookie in an Ajax Request
Get Number of Weekdays in a Given Month
Getting Imagegrabscreen to Work
How Do We Implement Custom API-Only Authentication in Laravel
Sort Array by Value Alphabetically PHP
Multidimensional Arrays Nested to Unlimited Depth
How to Upload a File Using Jquery's $.Ajax Function with JSON and PHP
How to Detect Ambiguous and Invalid Datetime in PHP
Create Unique Poll/Vote/Survey in PHP
Run PHP Function Inside Jquery Click
How to Get a PHP Value from an HTML Form
How to Ignore a Moved-Header with File_Get_Contents in PHP
Using Mod_Rewrite with Xampp and Windows 7 - 64 Bit
Sqlite Database Hosted on Heroku Getting Automatically Reset