乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Lua:字節(jié)數(shù)組與int互轉(zhuǎn)

       quasiceo 2014-01-19

      Lua:字節(jié)數(shù)組與int互轉(zhuǎn)  

      2011-10-21 10:43:49|  分類: IT |  標(biāo)簽:lua   |舉報(bào) |字號 訂閱

      converting from bytes to int (taking care of endianness at byte level, and signedness):

      --------------------------------------------------------------------------------------------
      require 'struct'
      -- convert character codes to a Lua string - this may come from your source
      local str = string.char(0x00, 0x1d, 0xff, 0x23, 0x44, 0x32)
      -- format string: < = little endian, In = unsigned int (n bytes)
      local u16, u32 = struct.unpack('<I2I4', str)
      print(u16, u32) --> 7424    843326463
      ---------------------------------------------------------------------------------------------

      function bytes_to_int(str,endian,signed) -- use length of string to determine 8,16,32,64 bits
          local t={str:byte(1,-1)}
          if endian=="big" then --reverse bytes
              local tt={}
              for k=1,#t do
                  tt[#t-k+1]=t[k]
              end
              t=tt
          end
          local n=0
          for k=1,#t do
              n=n+t[k]*2^((k-1)*8)
          end
          if signed then
              n = (n > 2^(#t-1) -1) and (n - 2^#t) or n -- if last bit set, negative.
          end
          return n
      end

      function int_to_bytes(num,endian,signed)
          if num<0 and not signed then num=-num print"warning, dropping sign from number converting to unsigned" end
          local res={}
          local n = math.ceil(select(2,math.frexp(num))/8) -- number of bytes to be used.
          if signed and num < 0 then
              num = num + 2^n
          end
          for k=n,1,-1 do -- 256 = 2^8 bits per char.
              local mul=2^(8*(k-1))
              res[k]=math.floor(num/mul)
              num=num-res[k]*mul
          end
          assert(num==0)
          if endian == "big" then
              local t={}
              for k=1,n do
                  t[k]=res[n-k+1]
              end
              res=t
          end
          return string.char(unpack(res))
      end

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多