不能確定,如果這是你在找什麼,但它是當我到達這個頁面時我正在尋找什麼。我有java.util.UUID/fromString
在Mongo中創建UUID作爲主鍵,我想在UI中使用普通字符串UUID。我正在使用node-mongodb-native
。
var Binary = require('mongodb').Binary;
var toUUID, toBinData;
module.exports.toUUID = toUUID = function(binId) {
var hex = binId.toString('hex');
return
hex.substr(0, 8) + '-' +
hex.substr(8, 4) + '-' +
hex.substr(12, 4) + '-' +
hex.substr(16, 4) + '-' +
hex.substr(20, 12);
};
module.exports.toBinData = toBinData = function(uuid) {
var buf = new Buffer(uuid.replace(/-/g, ''), 'hex');
return new Binary(buf, Binary.SUBTYPE_UUID_OLD);
};
更新
原來,雖然上面的作品就好了(因爲它的轉換類似於雙向)也不會產生相同的字符串UUID我在我的Clojure代碼見。但同樣的uuidhelpers救援 - 下面的Java遺留UUID的作品。
var Binary = require('mongodb').Binary;
var toJUUID, toBinData;
module.exports.toJUUID = toJUUID = function(binId) {
var hex = binId.buffer.toString('hex');
var msb = hex.substr(0, 16);
var lsb = hex.substr(16, 16);
msb = msb.substr(14, 2) + msb.substr(12, 2) + msb.substr(10, 2) + msb.substr(8, 2) + msb.substr(6, 2) + msb.substr(4, 2) + msb.substr(2, 2) + msb.substr(0, 2);
lsb = lsb.substr(14, 2) + lsb.substr(12, 2) + lsb.substr(10, 2) + lsb.substr(8, 2) + lsb.substr(6, 2) + lsb.substr(4, 2) + lsb.substr(2, 2) + lsb.substr(0, 2);
hex = msb + lsb;
return hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12);
};
module.exports.toBinData = toBinData = function(uuid) {
var hex = uuid.replace(/[{}-]/g, "");
var msb = hex.substr(0, 16);
var lsb = hex.substr(16, 16);
msb = msb.substr(14, 2) + msb.substr(12, 2) + msb.substr(10, 2) + msb.substr(8, 2) + msb.substr(6, 2) + msb.substr(4, 2) + msb.substr(2, 2) + msb.substr(0, 2);
lsb = lsb.substr(14, 2) + lsb.substr(12, 2) + lsb.substr(10, 2) + lsb.substr(8, 2) + lsb.substr(6, 2) + lsb.substr(4, 2) + lsb.substr(2, 2) + lsb.substr(0, 2);
hex = msb + lsb;
return new Binary(new Buffer(hex, 'hex'), Binary.SUBTYPE_UUID_OLD);
};
遵循相同的複製/粘貼方法,您可以從助手中剝離工作的C#代碼。你只需要處理緩衝區有點不同。
謝謝,我放棄了,只是寫了我需要使用C#的工具,所以我不會有UUID問題。希望這可以幫助別人! – 2014-11-24 20:40:14