简体   繁体   English

在Java中将字符串(rfc4122)编码为Number,在PHP中解码

[英]Encode String (rfc4122) to Number in Java, decode in PHP

In my use case, a javascript tracker generate a unique ID for a visitor whenever he/she visits the site, using the following formula : 在我的用例中,每当访问者访问站点时,javascript跟踪器都会使用以下公式为访问者生成唯一的ID:

function generateUUID(){
    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);
    });
}

It generates strings like this (rfc4122): 它生成这样的字符串(rfc4122):

"3314891e-285e-40a7-ac59-8b232863bead"

Now I need to encode that string in a Number (eg BigInteger in Java) that can be read by Mahout. 现在,我需要将该字符串编码为Mahout可以读取的Number(例如Java中的BigInteger)。 And likewise, restore it (in PHP) to display results. 同样,还原它(在PHP中)以显示结果。 Is there any fast, consistent and reliable way to do that? 是否有任何快速,一致和可靠的方法来做到这一点?

Some solutions are: 一些解决方案是:

  • Mapping each possible char (alphanumeric + '-') to a number [1..M] and summing each char position accordingly 将每个可能的字符(字母数字+'-')映射到数字[1..M]并相应地求和每个字符位置
  • get 2 longs from md5 hash 从md5哈希中获得2个多头
  • keep a hash map in memory 在内存中保留哈希图

Any ideas appreciated! 任何想法表示赞赏!

If Mahout can use a compound ID of two longs, you can use: 如果Mahout可以使用两个long的复合ID,则可以使用:

UUID uuid = UUID.fromString(string);
long l1 = uuid.getMostSignificantBits();
long l2 = uuid.getLeastSignificantBits();

If you really are stuck with one long, then I'd agree with your idea to use a portion of a hash based on the entire UUID 如果您确实坚持了很长时间,那么我会同意您的想法,即根据整个UUID使用哈希的一部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM