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. Data Conversion Byte to Integer

Data Conversion Byte to Integer

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
7 Posts 4 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.
  • Q Offline
    Q Offline
    quantimizer
    wrote on last edited by
    #1

    Hello, I am working on some financial data .I am picking up data from the dump file in a byte type variable . I have encountered a statement that is confusing me a lot a simple example or statement might be helpful : "empsalary are two-byte integers, high order first, and negatives are signed magnitude. Users may have to swap the bytes and/or convert negatives to the complement they use. This can be done by putting the low order byte first, then turning off bit 15 (the high order bit), then multiplying by -1. For positive numbers, only the bytes are switched." what does the above statement really means??

    P CPalliniC 2 Replies Last reply
    0
    • Q quantimizer

      Hello, I am working on some financial data .I am picking up data from the dump file in a byte type variable . I have encountered a statement that is confusing me a lot a simple example or statement might be helpful : "empsalary are two-byte integers, high order first, and negatives are signed magnitude. Users may have to swap the bytes and/or convert negatives to the complement they use. This can be done by putting the low order byte first, then turning off bit 15 (the high order bit), then multiplying by -1. For positive numbers, only the bytes are switched." what does the above statement really means??

      P Offline
      P Offline
      Peter Weyzen
      wrote on last edited by
      #2

      You are about to learn about endian issues. You can read all about it on the wikipedia[^] This is an long standing issue -- and is based on the processor on your computer. For all of us Windows, we operate as "little endian" -- whereas mac and java folks operate in "big endian" format. Endianness is just how the processor stores numbers in byte format. There's some handy functions used for socket programming (but also available generally) -- see ntohs()[^]

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

      D 1 Reply Last reply
      0
      • Q quantimizer

        Hello, I am working on some financial data .I am picking up data from the dump file in a byte type variable . I have encountered a statement that is confusing me a lot a simple example or statement might be helpful : "empsalary are two-byte integers, high order first, and negatives are signed magnitude. Users may have to swap the bytes and/or convert negatives to the complement they use. This can be done by putting the low order byte first, then turning off bit 15 (the high order bit), then multiplying by -1. For positive numbers, only the bytes are switched." what does the above statement really means??

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        IMHO the above statement is clear: values are stored inside the file as two bytes integers, with most significative byte first, for instance 1000 (that is 03E8h in hexadecimal notation) is stored as follows:

        first byte = 3 (03h); second byte = 232 (E8h)

        that is:

        3 * 256 + 232 = 768 + 232 = 1000.

        On the other hand, negative numbers are stored using Sign and magnitude representation (see [^]), hence -1000 will become 83E8h in hexadecimal notation ( 8h=1000binary, i.e. sign bit ON). Whenever you need to retrieve values you have to always check the sign bit. I give a C code snippet (error checking is left as an exercise to the reader...:)):

        unsigned char a,b;
        int value;

        /* read first byte */
        fread(&a, sizeof(a), 1, fd);
        /* read second byte */
        fread(&b, sizeof(b), 1, fd);
        /* get magnitude, i.e. absolute value */
        value = ((a & 0x7F) << 8 ) | b;
        /* check sign */
        if ( a & 0x80)
        {
        value = -value;
        }
        /* now value contains the (possibly negative?!? :-D) employee's salary!*/

        Hope that helps :)

        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.

        In testa che avete, signor di Ceprano?

        Q 1 Reply Last reply
        0
        • CPalliniC CPallini

          IMHO the above statement is clear: values are stored inside the file as two bytes integers, with most significative byte first, for instance 1000 (that is 03E8h in hexadecimal notation) is stored as follows:

          first byte = 3 (03h); second byte = 232 (E8h)

          that is:

          3 * 256 + 232 = 768 + 232 = 1000.

          On the other hand, negative numbers are stored using Sign and magnitude representation (see [^]), hence -1000 will become 83E8h in hexadecimal notation ( 8h=1000binary, i.e. sign bit ON). Whenever you need to retrieve values you have to always check the sign bit. I give a C code snippet (error checking is left as an exercise to the reader...:)):

          unsigned char a,b;
          int value;

          /* read first byte */
          fread(&a, sizeof(a), 1, fd);
          /* read second byte */
          fread(&b, sizeof(b), 1, fd);
          /* get magnitude, i.e. absolute value */
          value = ((a & 0x7F) << 8 ) | b;
          /* check sign */
          if ( a & 0x80)
          {
          value = -value;
          }
          /* now value contains the (possibly negative?!? :-D) employee's salary!*/

          Hope that helps :)

          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.

          Q Offline
          Q Offline
          quantimizer
          wrote on last edited by
          #4

          Thanks a Lot CPallini , by the way your mentioned code can go in error state ???

          CPalliniC 1 Reply Last reply
          0
          • Q quantimizer

            Thanks a Lot CPallini , by the way your mentioned code can go in error state ???

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

            Well, you have always to check the return value of the fread function. :)

            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.

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • P Peter Weyzen

              You are about to learn about endian issues. You can read all about it on the wikipedia[^] This is an long standing issue -- and is based on the processor on your computer. For all of us Windows, we operate as "little endian" -- whereas mac and java folks operate in "big endian" format. Endianness is just how the processor stores numbers in byte format. There's some handy functions used for socket programming (but also available generally) -- see ntohs()[^]

              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Peter Weyzen wrote:

              For all of us Windows, we operate as "little endian" -- whereas mac and java folks operate in "big endian" format. Endianness is just how the processor stores numbers...

              So if the processor ultimately determines how the numbers are stored, what difference does the OS or the programming language make?


              "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              P 1 Reply Last reply
              0
              • D David Crow

                Peter Weyzen wrote:

                For all of us Windows, we operate as "little endian" -- whereas mac and java folks operate in "big endian" format. Endianness is just how the processor stores numbers...

                So if the processor ultimately determines how the numbers are stored, what difference does the OS or the programming language make?


                "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                P Offline
                P Offline
                Peter Weyzen
                wrote on last edited by
                #7

                it was just a generalization -- about how windows runs on intel, and intel is little endian

                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

                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