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

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

將 BitArray 轉(zhuǎn)換為字節(jié)

將 BitArray 轉(zhuǎn)換為字節(jié)

C#
HUX布斯 2023-08-13 09:38:20
BitArray我有一個(gè)將值轉(zhuǎn)換為值的代碼byte[]。我也從 stackoverflow 獲得了代碼。代碼運(yùn)行得很好,我只是不明白一部分。當(dāng)代碼復(fù)制到BitArray使用Byte讀取BitArray.CopyTo()時(shí)byte按LSB 順序。有人可以幫我理解為什么轉(zhuǎn)換后的字節(jié)是 LSB 順序嗎?strBit (is a string value that consists of 1/0)byte[] myByte = new byte[50];List<string> list = Enumerable.Range(0, strBit.Length / 8)    .Select(i => strBit.Substring(i * 8, 8))    .ToList();for (int x = 0; x < list.Count; x++){    BitArray myBitArray = new BitArray(list[x].ToString().Select(c => c == '1').ToArray());    myBitArray.CopyTo(myByte, x);}示例輸出:  strBit[0] = 10001111  (BitArray)轉(zhuǎn)換為字節(jié)時(shí):  myByte[0] = 11110001 (Byte) (241/F1)
查看完整描述

1 回答

?
Qyouu

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

因?yàn)槲覀儚挠疫呴_始計(jì)算位,從左邊開始計(jì)算項(xiàng)目;例如對(duì)于


 BitArray myBitArray = new BitArray(new byte[] { 10 });

我們有(byte 10從右數(shù)):


 10 = 00001010 (binary)

            ^

            second bit (which is 1)

當(dāng)相應(yīng)數(shù)組的項(xiàng)目我們從左邊開始計(jì)數(shù)時(shí):


 {false, true, false, true, false, false, false, false}

           ^

           corresponding second BitArray item (which is true)

這就是為什么如果我們想要一個(gè)byte后面的數(shù)組,我們必須Reverse每個(gè)byte表示,例如Linq解決方案


  using System.Collections;

  using System.Linq;


  ...


  BitArray myBitArray = ...


  byte[] myByte = myBitArray

    .OfType<bool>()

    .Select((value, index) => new { // into chunks of size 8

       value,

       chunk = index / 8 })

    .GroupBy(item => item.chunk, item => item.value)

    .Select(chunk => chunk // Each byte representation

      .Reverse()           // should be reversed   

      .Aggregate(0, (s, bit) => (s << 1) | (bit ? 1 : 0)))

    .Select(item => (byte) item)

    .ToArray();


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

添加回答

舉報(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)