Hi,

I need to read 4 bytes from file to form a 32-bit unsigned integer X. The first byte you read goes into the most-significant byte of X, the 2nd byte you read goes into the 2nd most-significant byte of X, the 3rd byte you read goes into the 3rd most-significant byte of X, and the 4th byte you read goes into the least-significant byte of X.

Here is the content of the file:

000000: 29 52 61 96 e1 bd 74 f9 1c d1 3c d6 6a 61 64 71 )Ra~~~t~.~<~jadq
000010: f8 41 8f 8d 55 de a1 b0 6a 21 8d 98 54 62 94 83 ~A~~U~~~j!~~Tb~~
000020: 6c 1b f4 5f 33 84 f2 e7 88 57 ce 98 31 29 08 5d l.~_3~~~~W~~1).]
000030: dd 14 25 82 52 c2 46 95 19 df f6 46 ae bf 54 d0 ~.%~R~F~.~~F~~T~
000040: c6 9a 47 79 5f 61 a4 3b e1 35 59 64 6f 08 67 1e ~~Gy_a~;~5Ydo.g.
000050: a9 9c 9a 05 dd a1 82 74 fc 66 16 38 c6 d7 0d 46 ~~~.~~~t~f.8~~.F
000060: 32 56 4b 3a d3 bf c7 da 2b 99 90 7c 8d cc eb b4 2VK:~~~~+~~|~~~~
000070: df bc 58 4b 12 62 c6 77 8d df 3b b0 f0 e1 5c 12 ~~XK.b~w~~;~~~\.
000080: 2d 53 ee 63 40 28 01 28 5a 48 6d 24 56 69 dc f2 -S~c@(.(ZHm$Vi~~
000090: ae 61 54 2e 65 8d b0 54 05 09 4a c2 2d 8f 9f d0 ~aT.e~~T..J~-~~~
0000a0: d6 09 12 3e e8 6a 45 0c c6 ac b6 8d 52 c8 1d 32 ~..>~jE.~~~~R~.2
0000b0: 59 81 51 7a e3 c9 7d df ab 03 28 6e 4a 2a 16 62 Y~Qz~~}~~.(nJ*.b
0000c0: b5 f6 a1 e7 79 99 70 b9 fe f0 28 c7 34 62 bd 10 ~~~~y~p~~~(~4b~.
0000d0: 70 dc d2 e8 8c 80 9a cc 6d 84 13 09 a7 0b ef 52 p~~~~~~~m~..~.~R
0000e0: 59 5b 97 0c 3d c2 aa ba da 11 89 5a 35 60 e4 74 Y[~.=~~~~.~Z5`~t
0000f0: a1 3d 28 34 81 63 2c d2 97 06 43 d1 cc b5 32 95 ~=(4~c,~~.C~~~2~
000100: b2 48 8e 4b 3c 10 7a d3 7b 09 fa bc b7 d9 2d c2 ~H~K<.z~{.~~~~-~
000110: 29 a8 90 b2 39 77 ec 3e f5 bd d4 52 dc 8c 8b 70 )~~~9w~>~~~R~~~p
000120: af f9 9f dd f4 ec 20 96 97 6a 47 2c bb c7 a4 32 ~~~~~~ ~~jG,~~~2
000130: 55 12 d9 05 d6 82 ed 6e 49 77 a6 42 b3 f0 08 b9 U.~.~~~nIw~B~~.~
000140: 2b 1d e3 71 6b 8d 74 29 88 1d c5 1b e9 50 26 0f +.~qk~t)~.~.~P&.
000150: 35 59 c6 b6 19 17 fe 8e a7 ba d6 2e 7a 21 d3 e3 5Y~~..~~~~~.z!~~
000160: 1c 70 ef 6a 2d a3 4f 6e fe 25 4a e1 38 67 85 ef .p~j-~On~%J~8g~~
000170: d6 a6 c7 bb ca e1 07 07 48 a0 24 a2 4f 90 08 39 ~~~~~~..H~$~O~.9
000180: f3 11 22 2b ed a0 50 3f 87 57 49 02 be 86 6f d9 ~."+~~P?~WI.~~o~
000190: c7 92 b6 eb 26 0a c4 b8 30 b6 08 f1 4b 02 43 ed ~~~~&.~~0~.~K.C~
0001a0: 2f 4b cb 0f 5e 1d a3 08 56 5f f7 54 db 40 6b 3d /K~.^.~.V_~T~@k=

Could anyone tell me how to do this???

Dani AI

Generated

If you need the first 4 bytes to become a 32-bit value in big-endian order (byte0 as MSB through byte3 as LSB), do not read straight into an unsigned int and hope it works. That depends on your CPU’s endianness and on the size of unsigned int. Instead, read 4 raw bytes and assemble them yourself. For your file, those bytes are 0x29 0x52 0x61 0x96, so X should be 0x29526196.

is right about fopen’s mode: use binary. ’s idea of reading into a byte buffer is the right direction; here’s a minimal C++ version that is portable and explicit about byte order.

#include <cstdint>
#include <fstream>
#include <array>
#include <iostream>

int main() {
    std::ifstream f("rndfile.bin", std::ios::binary);
    if (!f) { std::cerr << "open failed\n"; return 1; }

    std::array<uint8_t,4> b{};
    if (!f.read(reinterpret_cast<char*>(b.data()), b.size())) {
        std::cerr << "short read\n"; return 1;
    }

    uint32_t X = (uint32_t)b[0] << 24 |
                 (uint32_t)b[1] << 16 |
                 (uint32_t)b[2] << 8  |
                 (uint32_t)b[3];

    std::cout << std::hex << std::showbase << X << "\n"; // 0x29526196
}

Notes:

  • Always open binary data with "rb"/std::ios::binary. On Windows, text mode can corrupt bytes.
  • Use fixed-width types (uint8_t, uint32_t) so sizes are guaranteed.
  • Cast to uint32_t before shifting to avoid sign/overflow issues.
  • Want bytes from somewhere else in the file? Seek first: f.seekg(offset, std::ios::beg);.
  • If you stick with C fread, read into uint8_t buf[4] and combine with the same shifts; check that fread returns 4.

Recommended Answers

All 7 Replies

Hi,

I need to read 4 bytes from file to form a 32-bit unsigned integer X. The first byte you read goes into the most-significant byte of X, the 2nd byte you read goes into the 2nd most-significant byte of X, the 3rd byte you read goes into the 3rd most-significant byte of X, and the 4th byte you read goes into the least-significant byte of X.

Here is the content of the file:

000000: 29 52 61 96 e1 bd 74 f9 1c d1 3c d6 6a 61 64 71 )Ra~~~t~.~<~jadq
000010: f8 41 8f 8d 55 de a1 b0 6a 21 8d 98 54 62 94 83 ~A~~U~~~j!~~Tb~~
000020: 6c 1b f4 5f 33 84 f2 e7 88 57 ce 98 31 29 08 5d l.~_3~~~~W~~1).]
000030: dd 14 25 82 52 c2 46 95 19 df f6 46 ae bf 54 d0 ~.%~R~F~.~~F~~T~
000040: c6 9a 47 79 5f 61 a4 3b e1 35 59 64 6f 08 67 1e ~~Gy_a~;~5Ydo.g.
000050: a9 9c 9a 05 dd a1 82 74 fc 66 16 38 c6 d7 0d 46 ~~~.~~~t~f.8~~.F
000060: 32 56 4b 3a d3 bf c7 da 2b 99 90 7c 8d cc eb b4 2VK:~~~~+~~|~~~~
000070: df bc 58 4b 12 62 c6 77 8d df 3b b0 f0 e1 5c 12 ~~XK.b~w~~;~~~\.
000080: 2d 53 ee 63 40 28 01 28 5a 48 6d 24 56 69 dc f2 -S~c@(.(ZHm$Vi~~
000090: ae 61 54 2e 65 8d b0 54 05 09 4a c2 2d 8f 9f d0 ~aT.e~~T..J~-~~~
0000a0: d6 09 12 3e e8 6a 45 0c c6 ac b6 8d 52 c8 1d 32 ~..>~jE.~~~~R~.2
0000b0: 59 81 51 7a e3 c9 7d df ab 03 28 6e 4a 2a 16 62 Y~Qz~~}~~.(nJ*.b
0000c0: b5 f6 a1 e7 79 99 70 b9 fe f0 28 c7 34 62 bd 10 ~~~~y~p~~~(~4b~.
0000d0: 70 dc d2 e8 8c 80 9a cc 6d 84 13 09 a7 0b ef 52 p~~~~~~~m~..~.~R
0000e0: 59 5b 97 0c 3d c2 aa ba da 11 89 5a 35 60 e4 74 Y[~.=~~~~.~Z5`~t
0000f0: a1 3d 28 34 81 63 2c d2 97 06 43 d1 cc b5 32 95 ~=(4~c,~~.C~~~2~
000100: b2 48 8e 4b 3c 10 7a d3 7b 09 fa bc b7 d9 2d c2 ~H~K<.z~{.~~~~-~
000110: 29 a8 90 b2 39 77 ec 3e f5 bd d4 52 dc 8c 8b 70 )~~~9w~>~~~R~~~p
000120: af f9 9f dd f4 ec 20 96 97 6a 47 2c bb c7 a4 32 ~~~~~~ ~~jG,~~~2
000130: 55 12 d9 05 d6 82 ed 6e 49 77 a6 42 b3 f0 08 b9 U.~.~~~nIw~B~~.~
000140: 2b 1d e3 71 6b 8d 74 29 88 1d c5 1b e9 50 26 0f +.~qk~t)~.~.~P&.
000150: 35 59 c6 b6 19 17 fe 8e a7 ba d6 2e 7a 21 d3 e3 5Y~~..~~~~~.z!~~
000160: 1c 70 ef 6a 2d a3 4f 6e fe 25 4a e1 38 67 85 ef .p~j-~On~%J~8g~~
000170: d6 a6 c7 bb ca e1 07 07 48 a0 24 a2 4f 90 08 39 ~~~~~~..H~$~O~.9
000180: f3 11 22 2b ed a0 50 3f 87 57 49 02 be 86 6f d9 ~."+~~P?~WI.~~o~
000190: c7 92 b6 eb 26 0a c4 b8 30 b6 08 f1 4b 02 43 ed ~~~~&.~~0~.~K.C~
0001a0: 2f 4b cb 0f 5e 1d a3 08 56 5f f7 54 db 40 6b 3d /K~.^.~.V_~T~@k=

Could anyone tell me how to do this???

//The value of the first four bytes would be:

byte0=0x29,byte1=0x52 ,byte2= 0x61 ,byte3=0x96

What have you tried? What did it give you?

I just know that the values of byte0,byte1,byte2 and byte3..
But please explain me how to fetch these values??

First step will be to read from a file. You can use fread for that.

Now the 4 bytes that you want to read will be in a character array.
The next part is to assign these bytes to form a 4 byte unsigned number. This code will get you started.... But you will have to modify it to fulfill your specification.

int main()
{
        int y=0;
        char* ip;
        ip=(char*)&y;

        *ip=10;
        *(ip+1)=11;
        *(ip+2)=12;             //We are accessing individual bytes of a 
                                        // 4 byte integer
        *(ip+3)=13;
        printf("%u %u %u %u\n",(ip),(ip+1),(ip+2),(ip+3));
        
         printf("%x\n",y);

        return 0;
}

I just know that the values of byte0,byte1,byte2 and byte3..
But please explain me how to fetch these values??

What have you tried? What did it give you?

Thanks for the reply.
Could you please explain why have you assigned 10,11,12 and 13 to ip??

What I am doing is:

unsigned int X=0;
 FILE *fp ;
         fp = fopen("rndfile.bin","r");

         if( fp == NULL )
         {
               puts ( "cannot open file" ) ;
               exit(1) ;
         }
	fread(&X, 4, 1, fp);

For the 3rd f'ing time, WHAT DID IT GIVE YOU????

And look up the fopen() function for the proper use of the 2nd parameter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.