vikeria
0.01D
V2EX  ›  Java

java 中的 int 和 c 中的 unsigned int 之间的处理

  •  
  •   vikeria · Oct 27, 2015 via Android · 3283 views
    This topic created in 3878 days ago, the information mentioned may be changed or developed.
    项目原因,需要和 c 进程进行交互,使用 netty 发字节流消息。比如说定义接口
    c 端结构体有 unsinged int 字段, java 端如何定义呢, int 的话会比 c 端能取到的值要小,使用 long 的话,像 buffer 中写入时字节又过多。
    求解各位是怎么解决的呢?
    7 replies    2015-10-28 17:11:19 +08:00
    ljbha007
        1
    ljbha007  
       Oct 27, 2015
    如果 C 那边 int 是 32 位的话 那 java 只能用 long 写字节流的时候自己手动 把最后 32 位写到字节流里
    SoloCompany
        2
    SoloCompany  
       Oct 28, 2015
    read / write 用 int ,使用的时候可以赋值给 long ,对于小于 0 的 int 赋值后要加上 4G
    vikeria
        3
    vikeria  
    OP
       Oct 28, 2015
    @SoloCompany 呃,能问下 4G 是什么吗- -
    Comdex
        4
    Comdex  
       Oct 28, 2015
    不明觉厉
    vikeria
        5
    vikeria  
    OP
       Oct 28, 2015
    @Comdex 嘛~有点困扰的小问题,一般内网系统也不会取到那么大的值,除非运行了好多好多年了
    SoloCompany
        6
    SoloCompany  
       Oct 28, 2015
    @vikeria x = x & 0xFFFFFFFFL
    vikeria
        7
    vikeria  
    OP
       Oct 28, 2015
    @SoloCompany soga. thx a lot.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3399 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 00:34 · PVG 08:34 · LAX 17:34 · JFK 20:34
    ♥ Do have faith in what you're doing.