第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從 Java 中的字節(jié)數(shù)組創(chuàng)建一個(gè) 48 位有符號(hào)值

從 Java 中的字節(jié)數(shù)組創(chuàng)建一個(gè) 48 位有符號(hào)值

繁星coding 2023-01-05 17:11:21
我正在嘗試從字節(jié)數(shù)組中提取 6 個(gè)字節(jié)并將它們轉(zhuǎn)換為 48 位有符號(hào)整數(shù)值(即 Java 長整數(shù))。如何做到這一點(diǎn)?編輯:例如,如果字節(jié)數(shù)組具有以下之一:byte[] minInt48 = new byte[] { (byte)0x80, 0, 0, 0, 0, 0 }; byte[] maxInt48 = new byte[] { (byte)0x7F, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };如何將其解析為 Java 原語(即 Java long)以保留符號(hào)值?
查看完整描述

1 回答

?
慕尼黑8549860

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊

首先,您需要了解符號(hào)擴(kuò)展。您需要將每個(gè)字節(jié)視為無符號(hào)值。


      long v = 0;

      byte q = -2; // unsigned value of 254

      v = v + q;

      System.out.println(v); // prints -2 which is not what you want.

      v = 0;

      v = v + (q & 0xFF); // mask off sign extension from q.

      System.out.println(v); // prints 254 which is correct.

這是一種方法。


     long val = 0;

     byte[] bytes = { -1, 12, 99, -121, -3, 123

     };

     for (int i = 0; i < bytes.length; i++) {

        // shift the val left by 8 bits first.

        // then add b.  You need to mask it with 0xFF to

        // eliminate sign extension to a long which will

        // result in an incorrect conversion.

        val = (val << 8) | ((i == 0 ? bytes[i]

              : bytes[i] & 0xFF));

     }

     System.out.println(val);


查看完整回答
反對(duì) 回復(fù) 2023-01-05
  • 1 回答
  • 0 關(guān)注
  • 170 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)