简体   繁体   English

将 long 编码为 int,反之亦然

[英]Encode long into an int and vice versa

If I have an application, Ryno, that produces unique increasing long numbers.如果我有一个应用程序 Ryno,它会生成唯一的递增长数。 I have another application, Cyan,that sends a message and needs a unique integer id.我有另一个应用程序 Cyan,它发送一条消息并需要一个唯一的整数 ID。 I would want to use the number from Ryno as the id for message in Cyan.我想使用 Ryno 的号码作为青色消息的 ID。 Is there a way to encode long to int.有没有办法将long编码为int。 Cyan will not send no more than 1 billion messages Cyan 不会发送不超过 10 亿条消息

It works just fine它工作得很好

    long f = 10292029202924l;
    System.out.println(f);
    int v = (int)f;
    System.out.println(v);
    System.out.println((f & xFFFFFFFF);

prints印刷

10292029202924 10292029202924
1287561708 1287561708
1287561708 1287561708

    f = -1L;
    v = (int)f;
    System.out.println(v);
    v = (int)(f & 0x7FFFFFFF); // mask off sign extension
    System.out.println(v);

prints印刷

-1 -1
2147483647 2147483647

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

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