Re: [Dev-C++] Some extra bytes written to file?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2003-03-07 02:30:54
|
-------Original Message-------=0D =0D From: "J=F6rgen"=0D Date: =D0=E1=F1=E1=F3=EA=E5=F5=DE, 7 =CC=DC=F1=F4=E9=EF=F2 2003 12:59:43 = =F0=EC=0D To: Ioannis Vranos; dev...@li...=0D Subject: Re: [Dev-C++] Some extra bytes written to file?=0D =0D See comments...=0D =0D =0D [comment] =0D What I'm trying to do is to reduce the size of the written file. Casting = to unsigned long gives=0D me no reduction in size.=0D E.g. =0D 4 * char =3D 4 * 8 =3D 32 bits =3D 4 bytes. =0D 4 * unsigned long =3D 4 * 32 =3D 128 bits =3D 32 bytes=0D =0D =0D =0D =3D=3D> At first as i said you apply casting wrong. =0D =0D =3D=3D> For example you wrote:=0D =0D foo << ((unsigned char) (value[i][0] & 0x000000FF)); =0D =0D =0D =3D=3D> But it should be:=0D =0D foo << (unsigned char) (value[i][0]) & 0x000000FF; =0D =0D or better:=0D =0D foo << static_cast<unsigned char> (value[i][0]) & 0x000000FF; =0D =0D =0D =3D=3D> or better you should have used bitset<unsigned whatever> from the beginning.=0D =0D =0D =3D=3D> After that, you are do these bitwise things to reduce the size? =0D =0D =3D=3D> Keep in mind that for size reduction, binary file mode is better = (but with the cost of non=0D file portability).=0D =0D =0D [comment]=0D =0D First. There are no negative integers (int) in the data so the problem shouldn't be happening.=0D Second. Lets say that the value of the int to be casted is 130. In bits i= t is written=0D 00000000 00000000 00000000 10000010=0D Then it is masket with =0D 00000000 00000000 00000000 11111111=0D and the result is=0D 00000000 00000000 00000000 10000010=0D Now it is casted to unsigned char and we got the following bits=0D 10000010=0D The value is still 130. If it were a signed char the bitrepresentation wo= uld still be=0D 10000010=0D but the value would be -126=0D =0D =0D =3D=3D> You can assume nothing about the implementation. You can assume a= fter the casting=0D which you did wrong. Also for values larger than numeric_limits<unsigned char>::max() you=0D get data truncation.=0D =0D =0D =0D =0D =0D Ioannis Vranos=0D =0D * Programming pages: http://www.noicys.freeurl.com=0D * Alternative URL: http://run.to/noicys=20 |