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. Converting integers from array to integer variables

Converting integers from array to integer variables

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
8 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.
  • M Offline
    M Offline
    Mike Zolna
    wrote on last edited by
    #1

    I have an array of numbers say array[0]=-10, and array[1]=23. Furthermore, lets say that they represent a most significant bit, and a least significant bit, so in reality the number is -1023. How do I combine both elements of the array into a signed integer that I can store?? Regards Mike Zolna

    M R 3 Replies Last reply
    0
    • M Mike Zolna

      I have an array of numbers say array[0]=-10, and array[1]=23. Furthermore, lets say that they represent a most significant bit, and a least significant bit, so in reality the number is -1023. How do I combine both elements of the array into a signed integer that I can store?? Regards Mike Zolna

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Using your simple example, int number = array[0]*100 + array[1]; --Mike-- http://home.inreach.com/mdunn/ Time is an illusion; lunchtime doubly so.

      M 1 Reply Last reply
      0
      • M Michael Dunn

        Using your simple example, int number = array[0]*100 + array[1]; --Mike-- http://home.inreach.com/mdunn/ Time is an illusion; lunchtime doubly so.

        M Offline
        M Offline
        Mike Zolna
        wrote on last edited by
        #3

        Thanks, but what if the number is 102 and not 1023, that wont work. I dont know that magnitude of the numbers until they are joined together Mike

        M D 2 Replies Last reply
        0
        • M Mike Zolna

          Thanks, but what if the number is 102 and not 1023, that wont work. I dont know that magnitude of the numbers until they are joined together Mike

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          I don't know what your program is like, so it's hard to say. But if you don't know what the final result should look like, then you won't be able to calculate the final result. ;) You'll need some sort of separate indicator to tell you how the result is split up among the array elements. --Mike-- http://home.inreach.com/mdunn/ Time is an illusion; lunchtime doubly so.

          M 1 Reply Last reply
          0
          • M Mike Zolna

            I have an array of numbers say array[0]=-10, and array[1]=23. Furthermore, lets say that they represent a most significant bit, and a least significant bit, so in reality the number is -1023. How do I combine both elements of the array into a signed integer that I can store?? Regards Mike Zolna

            R Offline
            R Offline
            RockNix
            wrote on last edited by
            #5

            If your numbers are in range of -signed char- it`s very easy to combine these numbers to an integer: int result = ( array[0] << 8 ) + array[1]; This command shifts the number stored in array[0] 8 Bit up (left) and adds the number stored in array[1]; Hope that helps Greatings Mario /// -------------------- www.klangwerker.de rocknix@lycos.de --------------------

            1 Reply Last reply
            0
            • M Mike Zolna

              I have an array of numbers say array[0]=-10, and array[1]=23. Furthermore, lets say that they represent a most significant bit, and a least significant bit, so in reality the number is -1023. How do I combine both elements of the array into a signed integer that I can store?? Regards Mike Zolna

              R Offline
              R Offline
              RockNix
              wrote on last edited by
              #6

              If your numbers are in range of -signed char- it`s very easy to combine these numbers to an integer: int result = ( array[0] << 8 ) + array[1]; This command shifts the number stored in array[0] 8 Bit up (left) and adds the number stored in array[1]. Shifting a byte by 8 is equal to a multiplikation with 0xff - but shifting is much faster I think. Hope that helps Greatings Mario /// -------------------- www.klangwerker.de rocknix@lycos.de --------------------

              1 Reply Last reply
              0
              • M Mike Zolna

                Thanks, but what if the number is 102 and not 1023, that wont work. I dont know that magnitude of the numbers until they are joined together Mike

                D Offline
                D Offline
                David Fedolfi
                wrote on last edited by
                #7

                Well, this is not efficient, but shoudl serve as a starting point for you: long lResult = 0; int nMult; for (int x = 0; x< nNum; x++) { nMult = 1; while (nMult <= nArray[x]) nMult *= 10; lResult *= nMult; lResult += nArray[x]; } Note though that if your array has any significant size then you will overflow the integer you're trying ot store it into.

                1 Reply Last reply
                0
                • M Michael Dunn

                  I don't know what your program is like, so it's hard to say. But if you don't know what the final result should look like, then you won't be able to calculate the final result. ;) You'll need some sort of separate indicator to tell you how the result is split up among the array elements. --Mike-- http://home.inreach.com/mdunn/ Time is an illusion; lunchtime doubly so.

                  M Offline
                  M Offline
                  Mike Zolna
                  wrote on last edited by
                  #8

                  Actually, The elements of the array are from 0-255, or 00 to FF in hex. I need to combine both bytes and turn them into a signed character for processing. Regards Mike

                  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