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. combining chars

combining chars

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

    hey all, So I am reading a file into a char buffer[] and need to combine two of the values in the buffer into an int value. The Documentation for my device says that values 5 and 6 in the array are one value with MSB first. How can I put these values into an int? thanks - legit

    M S M 3 Replies Last reply
    0
    • L legit

      hey all, So I am reading a file into a char buffer[] and need to combine two of the values in the buffer into an int value. The Documentation for my device says that values 5 and 6 in the array are one value with MSB first. How can I put these values into an int? thanks - legit

      M Offline
      M Offline
      Mohammad A Gdeisat
      wrote on last edited by
      #2

      lets assume that u have a buffer of type (char *) called charbuffer you can convert it like this (I havent tested it but it should work): short *intbuffer = (short*)charbuffer; now every consecutive two chars in charbuffer will be treated as 1 short (16-bit) integer in intbuffer. regards Mohammad

      And ever has it been that love knows not its own depth until the hour of separation

      M 1 Reply Last reply
      0
      • L legit

        hey all, So I am reading a file into a char buffer[] and need to combine two of the values in the buffer into an int value. The Documentation for my device says that values 5 and 6 in the array are one value with MSB first. How can I put these values into an int? thanks - legit

        S Offline
        S Offline
        S Douglas
        wrote on last edited by
        #3

        const char* str_int = "777"; int i = atoi(str_int); Convert a string to integer.[^]


        I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:

        1 Reply Last reply
        0
        • L legit

          hey all, So I am reading a file into a char buffer[] and need to combine two of the values in the buffer into an int value. The Documentation for my device says that values 5 and 6 in the array are one value with MSB first. How can I put these values into an int? thanks - legit

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          int intvalue = ((buffer[5] << 8) | buffer[6]) & 0x0000FFFF; Edit: Forgot a parenthesis :)

          1 Reply Last reply
          0
          • M Mohammad A Gdeisat

            lets assume that u have a buffer of type (char *) called charbuffer you can convert it like this (I havent tested it but it should work): short *intbuffer = (short*)charbuffer; now every consecutive two chars in charbuffer will be treated as 1 short (16-bit) integer in intbuffer. regards Mohammad

            And ever has it been that love knows not its own depth until the hour of separation

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            Mohammad A Gdeisat wrote:

            short *intbuffer = (short*)charbuffer; now every consecutive two chars in charbuffer will be treated as 1 short (16-bit) integer in intbuffer.

            FYI that won't work on an Intel x86 machine in his case because the most significant byte is FIRST in the char array. Mark

            M 1 Reply Last reply
            0
            • M Mark Salsbery

              Mohammad A Gdeisat wrote:

              short *intbuffer = (short*)charbuffer; now every consecutive two chars in charbuffer will be treated as 1 short (16-bit) integer in intbuffer.

              FYI that won't work on an Intel x86 machine in his case because the most significant byte is FIRST in the char array. Mark

              M Offline
              M Offline
              Mohammad A Gdeisat
              wrote on last edited by
              #6

              then u can use masking and shifting to swap between hi-order and low-order bytes. Regards, Mohammad

              And ever has it been that love knows not its own depth until the hour of separation

              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