MESSAGE
DATE | 2016-12-06 |
FROM | Christopher League
|
SUBJECT | Re: [Learn] png data format
|
From learn-bounces-at-nylxs.com Tue Dec 6 17:46:55 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id 5CA5A161312; Tue, 6 Dec 2016 17:46:55 -0500 (EST) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from liucs.net (contrapunctus.net [174.136.110.10]) by mrbrklyn.com (Postfix) with ESMTP id 2CD89160E77; Tue, 6 Dec 2016 17:46:51 -0500 (EST) Received: from localhost (pool-98-113-34-169.nycmny.fios.verizon.net [98.113.34.169]) by liucs.net (Postfix) with ESMTPSA id 46A25E096; Tue, 6 Dec 2016 17:46:50 -0500 (EST) From: Christopher League To: Ruben Safir , learn-at-nylxs.com, hangout In-Reply-To: <20f5341f-a0b0-b9d9-0c45-11e5a308c12d-at-panix.com> References: <66105244-4afa-4330-b0c2-0661bde965fd-at-mrbrklyn.com> <87bmwpf857.fsf-at-contrapunctus.net> <878trtf7qw.fsf-at-contrapunctus.net> <87h96gemdn.fsf-at-contrapunctus.net> <20f5341f-a0b0-b9d9-0c45-11e5a308c12d-at-panix.com> User-Agent: Notmuch/0.21 (http://notmuchmail.org) Emacs/25.1.1 (x86_64-unknown-linux-gnu) Date: Tue, 06 Dec 2016 17:46:48 -0500 Message-ID: <87d1h4ekvb.fsf-at-contrapunctus.net> MIME-Version: 1.0 Subject: Re: [Learn] png data format X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: multipart/mixed; boundary="===============1390911853==" Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
--===============1390911853== Content-Type: multipart/alternative; boundary="=-=-="
--=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Ruben Safir writes:
> Right but why does the for loop automatically reverse the order > > WHen you loop it does this > > |00|00|00|0d| > =E2=86=91 > > |00|00|00|0d| > =E2=86=91 > > |00|00|00|0d| > =E2=86=91 > > |00|00|00|0d| > =E2=86=91 > > But as a whole it reads it as > > 0d000000
The *for loop* doesn't really reverse the order. That order IS THE WAY THAT INTEL STORES INTEGERS AS BYTES.
You can verify it by taking an integer and casting its address as a pointer to bytes. Byte #0 will be the least significant byte. Byte #3 will be the most significant byte. That's what LITTLE-ENDIAN *MEANS*.
~~~~ {.cpp} #include #include #include using namespace std; int main() { uint32_t number =3D 0x89ABCDEF; uint8_t* arrayOfBytes =3D (uint8_t*) &number; // Explicit cast required assert(arrayOfBytes[0] =3D=3D 0xEF); // Least-significant byte is first assert(arrayOfBytes[1] =3D=3D 0xCD); // (assuming little-endian) assert(arrayOfBytes[2] =3D=3D 0xAB); assert(arrayOfBytes[3] =3D=3D 0x89); // Most-significant byte is last for(int i =3D 0; i < 4; i++) { cout << hex << (int)arrayOfBytes[i] << endl; }
// Or here it is in reverse -- initializing an array of bytes, // and then treating it as an integer: uint8_t secondArray[4] =3D { 0x12, 0x34, 0x56, 0x78 }; uint32_t* numberPointer =3D (uint32_t*) secondArray; // Cast required uint32_t secondNumber =3D *numberPointer; assert(secondNumber =3D=3D 0x78563412); cout << hex << secondNumber << endl;
return 0; } ~~~~
If you run this program on a big-endian architecture, all the assertions will fail. Because in big-endian, byte #0 is the most significant byte.
Adapting this to your program, the PNG file contained the bytes, in this order: 0x00, 0x00, 0x00, 0x0D.
But when you have those four bytes, IN THAT ORDER, and you address them as a single 32-bit integer on a little-endian architecture, you will see 0x0D000000. Because, remember, it expects to see the least significant byte first, and the most significant byte last.
CL
--=-=-= Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
1.0, user-scalable=3Dyes">
Ruben Safir mrbrklyn-at-panix.com= writes:
Right but why does the for loop automatically reverse the order
WHen you loop it does this
|00|00|00|0d| =E2=86=91
|00|00|00|0d| =E2=86=91
|00|00|00|0d| =E2=86=91
|00|00|00|0d| =E2=86=91
But as a whole it reads it as
0d000000
The for loop doesn=E2=80=99t really reverse the order. That ord= er IS THE WAY THAT INTEL STORES INTEGERS AS BYTES.
You can verify it by taking an integer and casting its address as a poin= ter to bytes. Byte #0 will be the least significant byte. Byte #3 will be t= he most significant byte. That=E2=80=99s what LITTLE-ENDIAN MEANS.=
ceCode cpp">#include <iostream> #include <cstdint> #include <cassert> using namespace std; int main() { uint32_t number =3D 0x89AB= CDEF; uint8_t* arrayOfBytes =3D (>uint8_t*) &number; // Explicit cast required=
assert(arrayOfBytes[0] =3D=3D bn">0xEF); // Least-significant byte is firstpan> assert(arrayOfBytes[1] =3D=3D bn">0xCD); // (assuming little-endian) assert(arrayOfBytes[2] =3D=3D bn">0xAB); assert(arrayOfBytes[3] =3D=3D bn">0x89); // Most-significant byte is lastn> for(int i =3D class=3D"dv">0; i < 4; i++) { cout << hex << (int)arrayOfBy= tes[i] << endl; }
// Or here it is in reverse -- initializing an array= of bytes, // and then treating it as an integer: uint8_t secondArray[4n>] =3D { 0x12, 0x34, <= span class=3D"bn">0x56, 0x78 }; uint32_t* numberPointer =3D (t">uint32_t*) secondArray; // Cast required uint32_t secondNumber =3D *numberPointer; assert(secondNumber =3D=3D 0x78563412); cout << hex << secondNumber << endl;
return 0; }
If you run this program on a big-endian architecture, all the assertions= will fail. Because in big-endian, byte #0 is the most significant byte.
Adapting this to your program, the PNG file contained the bytes, in this= order: 0x00, 0x00, 0x00, 0x0D.
But when you have those four bytes, IN THAT ORDER, and you address them = as a single 32-bit integer on a little-endian architecture, you will see 0x= 0D000000. Because, remember, it expects to see the least significant byte f= irst, and the most significant byte last.
CL
--=-=-=--
--===============1390911853== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline
_______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
--===============1390911853==--
|
|