How to convert a number to bytearray in bit order

I am trying to unzip some data created in VB6 using the zlib API.

I read that this is possible with the qUncompress function: http://doc.trolltech.com/4.4/qbytearray.html#qUncompress

I read the data from QDataStream via readRawBytes into a char array, which I then converted to QByteArray for decompression. I have a compressed length and an expected decompressed length, but I am not getting anything from qUncompress.

However, I need to add the expected decompressed length in large end format. Has anyone done this and provided an example?

+3
source share
6 answers

VB6 , , . , vb6 used() . - , .

qUncompress docs, QByteArray, 5 ( , , 1).

, qArr, - Size. "big-endian" .

qArr(1) = int(Size/(256*256*256))
qArr(2) = 255 And int(Size/256*256)
qArr(3) = 255 And int(Size/256)
qArr(4) = 255 And int(Size)

?

endian, (qArr (4) - qArr (1)) .

+2

.

Private Type LongByte
    H1 As Byte
    H2 As Byte
    L1 As Byte
    L2 As Byte
End Type

Private Type LongType
    L As Long
End Type

Function SwapEndian(ByVal LongData as Long) as Long
  Dim TempL As LongType
  Dim TempLB As LongByte
  Dim TempVar As Long

  TempL.L = LongData
  LSet TempLB = TempL
'Swap is a subroutine I wrote to swap two variables
  Swap TempLB.H1, TempLB.L2
  Swap TempLB.H2, TempLB.L1
  LSet TempL = TempLB
  TempVar = TempL.L

  SwapEndian = TempVar
End Function

FileIO, TempLB

LSET VB6

.NET, . MemoryStream . int16/int32/int64. , LSET MemoryStream .

Framework 1.1 , BitConvertor, .

Private Structure Int32Byte
    Public H1 As Byte
    Public H2 As Byte
    Public L1 As Byte
    Public L2 As Byte
    Public Function Convert() As Integer
        Dim M As New MemoryStream()
        Dim bR As IO.BinaryReader
        Dim bW As New IO.BinaryWriter(M)
        Swap(H1, L2)
        Swap(H2, L1)
        bW.Write(H1)
        bW.Write(H2)
        bW.Write(L1)
        bW.Write(L2)
        M.Seek(0, SeekOrigin.Begin)
        bR = New IO.BinaryReader(M)
        Convert = bR.ReadInt32()
    End Function
End Structure
+1

, C, zlib. zlib zlib. zlib: http://www.zlib.net/.

, , zlib ?

0
//int length;
byte[] bigEndianBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(length))

:

//byte[] bigEndianBytes;
int length = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bigEndianBytes))
0

, , VB , qUncompress VB, , ++ qUncompress.

Mike G VB. ++, : QByteArray, zlib uncompress . Qt qCompress qUncompress (corelib/tools/qbytearray.cpp) .

qCompress (nbytes) bazip, :

bazip[0] = (nbytes & 0xff000000) >> 24;
bazip[1] = (nbytes & 0x00ff0000) >> 16;
bazip[2] = (nbytes & 0x0000ff00) >> 8;
bazip[3] = (nbytes & 0x000000ff);

bazip - QByteArray

, uncompress , qUncompress

baunzip.resize(len);
res = ::uncompress((uchar*)baunzip.data(), &len,
                        (uchar*)data+4, nbytes-4);

where baunzip is a QByteArray. In your case, you will lose +4 and -4, since your data does not have a length previous to this.

0
source

Thanks for your help, it was helpful.

I got code that works with:

        char slideStr[currentCompressedLen];
        int slideByteRead = in.readRawData(slideStr, currentCompressedLen);
        QByteArray aInCompBytes = QByteArray(slideStr, slideByteRead);
        aInCompBytesPlusLen = aInCompBytes;
        aInCompBytesPlusLen.prepend(QByteArray::number(currentUnCompressedLen));
        aInUnCompBytes.resize(currentUnCompressedLen);
        aInUnCompBytes = qUncompress(aInCompBytesPlusLen);
0
source

Source: https://habr.com/ru/post/1697976/


All Articles