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. Is it okay to cast from int to unsigned char *?

Is it okay to cast from int to unsigned char *?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
11 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.
  • C ChemmieBro

    I have a 32bit int that I'm trying to stuff into an array of bytes. Is it okay to do this? CharArray = new unsigned char [4]; CharArray = (unsigned char *)&IntegerValue ByteArray[Length-4] = CharArray[3] ByteArray[Length-3] = CharArray[2] ByteArray[Length-2] = CharArray[1] ByteArray[Length-1] = CharArray[0]

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

    Yes it's ok (to cast from int * to unsigned char *). Also is that a typo? You overwrite the allocated CharArray... should that have been: ByteArray = new unsigned char [Length]; CharArray = (unsigned char *)&IntegerValue Mark

    Mark Salsbery Microsoft MVP - Visual C++ :java:

    C 1 Reply Last reply
    0
    • M Mark Salsbery

      Yes it's ok (to cast from int * to unsigned char *). Also is that a typo? You overwrite the allocated CharArray... should that have been: ByteArray = new unsigned char [Length]; CharArray = (unsigned char *)&IntegerValue Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      C Offline
      C Offline
      ChemmieBro
      wrote on last edited by
      #3

      No, that's not a typo. The ByteArray is actually a different array type, but is still an array of unsigned char. If it is okay to get the IntegerValue into the unsigned char[4] then I'm okay. so... CharArray = new Unsigned Char [4]; CharArray = (unsigned char*)&IntegerValue ; That should still be fine. The 4 Unsigned Char bytes will then be populated with the bytes from the integer.

      M 1 Reply Last reply
      0
      • C ChemmieBro

        No, that's not a typo. The ByteArray is actually a different array type, but is still an array of unsigned char. If it is okay to get the IntegerValue into the unsigned char[4] then I'm okay. so... CharArray = new Unsigned Char [4]; CharArray = (unsigned char*)&IntegerValue ; That should still be fine. The 4 Unsigned Char bytes will then be populated with the bytes from the integer.

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

        ChemmieBro wrote:

        No, that's not a typo

        In that case you have a problem :) ... CharArray = new unsigned char [4]; CharArray = (unsigned char *)&IntegerValue //<--- overwrites the pointer allocated above!! The storage has already been allocated - it is the 4 bytes used by the IntegerValue variable. You only need the second line above... CharArray = (unsigned char *)&IntegerValue; Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        C 1 Reply Last reply
        0
        • C ChemmieBro

          I have a 32bit int that I'm trying to stuff into an array of bytes. Is it okay to do this? CharArray = new unsigned char [4]; CharArray = (unsigned char *)&IntegerValue ByteArray[Length-4] = CharArray[3] ByteArray[Length-3] = CharArray[2] ByteArray[Length-2] = CharArray[1] ByteArray[Length-1] = CharArray[0]

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

          Have you considered using a union?

          "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

          M C 2 Replies Last reply
          0
          • D David Crow

            Have you considered using a union?

            "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

            M Offline
            M Offline
            Maxwell Chen
            wrote on last edited by
            #6

            DavidCrow wrote:

            using a union

            I like your solution!

            Maxwell Chen

            1 Reply Last reply
            0
            • M Mark Salsbery

              ChemmieBro wrote:

              No, that's not a typo

              In that case you have a problem :) ... CharArray = new unsigned char [4]; CharArray = (unsigned char *)&IntegerValue //<--- overwrites the pointer allocated above!! The storage has already been allocated - it is the 4 bytes used by the IntegerValue variable. You only need the second line above... CharArray = (unsigned char *)&IntegerValue; Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              C Offline
              C Offline
              ChemmieBro
              wrote on last edited by
              #7

              I see that, now. Thank you. I just needed a quick answer and got it. I'm just re-using some code and they had done this same thing using an integer and a two shorts. I figured if it worked that way, it would work using an integer and four unsigned chars. Now I'll have to change the old stuff, too. Thanks again.

              1 Reply Last reply
              0
              • D David Crow

                Have you considered using a union?

                "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

                C Offline
                C Offline
                ChemmieBro
                wrote on last edited by
                #8

                No. This is existing code and I was just curious if I was allowed to do that or not. I thought I could but couldn't find anything out there to confirm that it would work correctly. I will look up Unions, though, for future reference. Thank you.

                M 1 Reply Last reply
                0
                • C ChemmieBro

                  No. This is existing code and I was just curious if I was allowed to do that or not. I thought I could but couldn't find anything out there to confirm that it would work correctly. I will look up Unions, though, for future reference. Thank you.

                  M Offline
                  M Offline
                  Maxwell Chen
                  wrote on last edited by
                  #9

                  ChemmieBro wrote:

                  No. This is existing code

                  You still can use union in the existing code! And you won't even need to cast.

                  ChemmieBro wrote:

                  I thought I could but couldn't find anything out there to confirm that it would work correctly.

                  You have the debugger of VC++ for you to watch the memory addresses and the values.

                  Maxwell Chen

                  M 1 Reply Last reply
                  0
                  • M Maxwell Chen

                    ChemmieBro wrote:

                    No. This is existing code

                    You still can use union in the existing code! And you won't even need to cast.

                    ChemmieBro wrote:

                    I thought I could but couldn't find anything out there to confirm that it would work correctly.

                    You have the debugger of VC++ for you to watch the memory addresses and the values.

                    Maxwell Chen

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

                    Maxwell Chen wrote:

                    You still can use union in the existing code!

                    Except for the swapping of byte order (if I read the original code correctly). Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    R 1 Reply Last reply
                    0
                    • M Mark Salsbery

                      Maxwell Chen wrote:

                      You still can use union in the existing code!

                      Except for the swapping of byte order (if I read the original code correctly). Mark

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      R Offline
                      R Offline
                      ramana g
                      wrote on last edited by
                      #11

                      Another way is... int nNum = 10; char Data[4] = {0}; memcpy(&Data, &nNum, 4);

                      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