Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to convert bytes array into int

how to convert bytes array into int

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structurestutorial
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    huma satti
    wrote on last edited by
    #1

    hello i want to read sequenece numbers from the packets that are in a dump file. problem is that i hav read the header into byte array. and want to convert the sequence numbers into integer. my code is unsigned char buff[2]; buff[0]=header[22]; buff[1]=header[23]; unsigned int t; n=(buff[0]<<24)|(buff[1]<<16); header is byte array containing header. byet 22 and 23 is sequqnce number and i want to convert it into int. but this code is not working properly. can anybody help me.

    I C C K 4 Replies Last reply
    0
    • H huma satti

      hello i want to read sequenece numbers from the packets that are in a dump file. problem is that i hav read the header into byte array. and want to convert the sequence numbers into integer. my code is unsigned char buff[2]; buff[0]=header[22]; buff[1]=header[23]; unsigned int t; n=(buff[0]<<24)|(buff[1]<<16); header is byte array containing header. byet 22 and 23 is sequqnce number and i want to convert it into int. but this code is not working properly. can anybody help me.

      I Offline
      I Offline
      Iain Clarke Warrior Programmer
      wrote on last edited by
      #2

      1/ Read on the posting guidlines - particularly the pre tag. It marks out what is code, and what is text. 2/ You declare t, then don't use it. 3/ You use n, but don;t declare it. Without knowing things like type, we can;t help you much. 3/ buff [0] is a uchar. (buf [0] << 24) is also a uchar - and only has room for 8 bits, and will therefore end up as 0. 4/ Lastly, some help:

      DWORD dwResult = 0;

      dwResult |= header [22]; // assuming header is a big 'ol array of bytes.
      dwResult <<8; // shift it across a bit.
      dwResult |= header [23;
      dwResult <<= 16; // shift it some more.

      Enjoy, Iain.

      Iain Clarke appears because CPallini still cares.

      C 1 Reply Last reply
      0
      • H huma satti

        hello i want to read sequenece numbers from the packets that are in a dump file. problem is that i hav read the header into byte array. and want to convert the sequence numbers into integer. my code is unsigned char buff[2]; buff[0]=header[22]; buff[1]=header[23]; unsigned int t; n=(buff[0]<<24)|(buff[1]<<16); header is byte array containing header. byet 22 and 23 is sequqnce number and i want to convert it into int. but this code is not working properly. can anybody help me.

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        So, if I understand you well, you have an array of two bytes and you want to 'convert' this two bytes into ONE integer ? If yes, then simply do something like this: int Value = buff[0] + buff[1] * 256; Depending on the byte ordering, you will perhaps have to invert them.

        Cédric Moonen Software developer
        Charting control [v1.3]

        1 Reply Last reply
        0
        • H huma satti

          hello i want to read sequenece numbers from the packets that are in a dump file. problem is that i hav read the header into byte array. and want to convert the sequence numbers into integer. my code is unsigned char buff[2]; buff[0]=header[22]; buff[1]=header[23]; unsigned int t; n=(buff[0]<<24)|(buff[1]<<16); header is byte array containing header. byet 22 and 23 is sequqnce number and i want to convert it into int. but this code is not working properly. can anybody help me.

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          It depends on byte ordering of your packet, i.e Big Endian:

          unsigned int n = (buff[0] << 8) | buff[1];

          Little Endian:

          unsigned int n = (buff[1] << 8) | buff[0];

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          modified on Thursday, April 17, 2008 8:27 AM

          1 Reply Last reply
          0
          • I Iain Clarke Warrior Programmer

            1/ Read on the posting guidlines - particularly the pre tag. It marks out what is code, and what is text. 2/ You declare t, then don't use it. 3/ You use n, but don;t declare it. Without knowing things like type, we can;t help you much. 3/ buff [0] is a uchar. (buf [0] << 24) is also a uchar - and only has room for 8 bits, and will therefore end up as 0. 4/ Lastly, some help:

            DWORD dwResult = 0;

            dwResult |= header [22]; // assuming header is a big 'ol array of bytes.
            dwResult <<8; // shift it across a bit.
            dwResult |= header [23;
            dwResult <<= 16; // shift it some more.

            Enjoy, Iain.

            Iain Clarke appears because CPallini still cares.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Are you sure he wants the original number times 65536? :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            I 1 Reply Last reply
            0
            • C CPallini

              Are you sure he wants the original number times 65536? :-D

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              I Offline
              I Offline
              Iain Clarke Warrior Programmer
              wrote on last edited by
              #6

              No - but he did do a shift by 24 bits on one byte, and 16 bits on another. Given that I was guessing at his code (ie, taking his word for which was the high byte, etc), it was one of the few bits of solid data he'd actually provided. Iain.

              Iain Clarke appears because CPallini still cares.

              1 Reply Last reply
              0
              • H huma satti

                hello i want to read sequenece numbers from the packets that are in a dump file. problem is that i hav read the header into byte array. and want to convert the sequence numbers into integer. my code is unsigned char buff[2]; buff[0]=header[22]; buff[1]=header[23]; unsigned int t; n=(buff[0]<<24)|(buff[1]<<16); header is byte array containing header. byet 22 and 23 is sequqnce number and i want to convert it into int. but this code is not working properly. can anybody help me.

                K Offline
                K Offline
                krmed
                wrote on last edited by
                #7

                If I understand correctly, your sequence number you want is in bytes buf[22] and buf[23]. If that's true, you can use a cast to retrieve the value:

                unsigned short SeqNo;
                SeqNo = *(short*)&buf[22];

                Note that if it's only two bytes, it's a short and not an int. Hope that helps.

                Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups