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. Convert an int to char array rep

Convert an int to char array rep

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    Hi, Is there a more efficient way of converting an integer value into a char array representation. Hence, the char array would be set to size four because there are 4 bytes in an integer.

    I N D 3 Replies Last reply
    0
    • A Anonymous

      Hi, Is there a more efficient way of converting an integer value into a char array representation. Hence, the char array would be set to size four because there are 4 bytes in an integer.

      I Offline
      I Offline
      Ian Darling
      wrote on last edited by
      #2

      Do you mean converting an integer value into a string? If so, that's in the CP C++ FAQ here[^] -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

      A 1 Reply Last reply
      0
      • A Anonymous

        Hi, Is there a more efficient way of converting an integer value into a char array representation. Hence, the char array would be set to size four because there are 4 bytes in an integer.

        N Offline
        N Offline
        nde_plume
        wrote on last edited by
        #3

        Alternatively (to Ian's suggestion) if you mean to treat an integer as a four byte character array you can do so by casting: int i = getValue(); char* p = reinterpret_cast(i); However, that is a pretty scary thing to do, since obviously the string will not be guaranteed to be null terminated (unless i&0xFF is 0 on a big endian machine, or i&0xFF000000 is zero on a little endian machine, and also assuming that sizeof(int) == 4) Not recommended. Perhaps you could tell us what you are actually trying to do.

        A 1 Reply Last reply
        0
        • I Ian Darling

          Do you mean converting an integer value into a string? If so, that's in the CP C++ FAQ here[^] -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          No, more like binary rep only in char. I'm sending a stream of bytes.

          1 Reply Last reply
          0
          • N nde_plume

            Alternatively (to Ian's suggestion) if you mean to treat an integer as a four byte character array you can do so by casting: int i = getValue(); char* p = reinterpret_cast(i); However, that is a pretty scary thing to do, since obviously the string will not be guaranteed to be null terminated (unless i&0xFF is 0 on a big endian machine, or i&0xFF000000 is zero on a little endian machine, and also assuming that sizeof(int) == 4) Not recommended. Perhaps you could tell us what you are actually trying to do.

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            I'm trying to convert an integer value into an array of bytes. Char array is used cause it's exactly one byte. I have library func which sends bytes over a stream if given a pointer to char array head.

            N 1 Reply Last reply
            0
            • A Anonymous

              Hi, Is there a more efficient way of converting an integer value into a char array representation. Hence, the char array would be set to size four because there are 4 bytes in an integer.

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

              Something like this comes to mind:

              int nNum;
              char cBytes[4];

              cBytes[0] = (nNum >> 24) & 0xff;
              cBytes[1] = (nNum >> 16) & 0xff;
              cBytes[2] = (nNum >> 8) & 0xff;
              cBytes[3] = nNum & 0xff;


              Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

              1 Reply Last reply
              0
              • A Anonymous

                I'm trying to convert an integer value into an array of bytes. Char array is used cause it's exactly one byte. I have library func which sends bytes over a stream if given a pointer to char array head.

                N Offline
                N Offline
                nde_plume
                wrote on last edited by
                #7

                There are couple of issues you should be concerned with here. First of all, you need to send the integer in a representation that all readers will accept. As you probably know, different processor achitectures represent integers in different ways, consequently, most likely the best representation would be a simple ASCII stringL void convertMe(int i) { char s[MAX_CHAR]; itoa(i, s, 10); sendBytes(s); } where sendBytes is your sending function. If you don't care about endian-ness, you could also send the bytes directly like this: sendBytes(reinterpret_cast(&i), sizeof(i)); However this assumes that sendBytes is not designed to send a string, but a block of memory (hence the second parameter which is the length of the block to send.) If it is designed to send strings it will fail quite often since any zero byte within the integer will terminiate the string.

                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