integer to hex (another question)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mahmoodn
    New Member
    • May 2009
    • 77

    integer to hex (another question)

    consider this array:
    Code:
      char key[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
    I want to put it in a loop such that all 16 element change in each iteration. Some thing like this:

    Code:
    for ( int i = 0; i < 100; i++ ) {
       [B]j = hex(i)[/B] [B][I]// what is this[/I][/B]
       char key[16] = {j, j+1, j+2, j+3, j+4, j+5, j+6, j+7, j+8, j+9, j+10, j+11, j+12, j+13, j+14, j+15};
    }
    How can I do that? what the conversion function? I saw many threads that they wanted to put a hex value on the output stream. I want a function. Does c++ provide such function?

    Thanks,
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Do you mean something like this?

    Code:
    #include <ios>
    
    ...
    
    int value = 0x256;
    cout << hex << value;

    Comment

    • mahmoodn
      New Member
      • May 2009
      • 77

      #3
      I know that. I don't want to put the variable on the output stream. I want to convert an integer variable to a hex variable and store that.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Ummmm, do you mean as a string, or as another integer type?

        Code:
        // convert integer to hex string
        
        #include <ios>
        #include <strstream>
        
        ...
        
        int value = 0x0256;
        ostrstream hexOut;
        hexOut << hex << value;
        Last edited by Oralloy; Aug 17 '10, 02:08 PM. Reason: Fix malformed tag.

        Comment

        • mahmoodn
          New Member
          • May 2009
          • 77

          #5
          > do you mean as a string, or as another integer type?

          To something that I can use it the array

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            Then don't worry about making a conversion.

            At the "machine level", integers are typically represented as an ordered sequence of bits. How that gets displayed on your monitor is quite another thing, entirely.

            As for code, you can write any of these, they are equivalent:
            Code:
            int x = 0x0256;  // hex
            Code:
            int x = 01126;   // octal
            Code:
            int x = 598;     // decimal
            Does that make sense?

            Comment

            • mahmoodn
              New Member
              • May 2009
              • 77

              #7
              so if I have this:
              char key[16]={0xA,...}

              and want to put it in a loop
              for ( i=0; i<100; i++ )
              char key[16]={i,i+1,....}

              but the result is:
              key={0,1,2,..., 15}

              however I want this:
              key={0,1,2,..., 0xA,0xB,...,0xF }

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                It doesn't matter. Run this through your compiler and see what comes out....

                Code:
                #include <ios>
                #include <iostream>
                
                int main(void)
                {
                  int v0 = 0x0256;
                  int v1 = 598;
                
                  if (v0 == v1)
                    cout << "wow, they're equal!" << endl;
                  else
                    cout << "drat, Oralloy made a mistake." << endl;
                
                  cout << "v0(default) = " << v0 << endl;
                  cout << "v1(default) = " << v1 << endl;
                
                  cout << "v0(dec) = " << dec << v0 << endl;
                  cout << "v0(hex) = " << hex << v0 << endl;
                
                  cout << "v1(dec) = " << dec << v1 << endl;
                  cout << "v1(hex) = " << hex << v1 << endl;
                
                  return 0;
                }
                Last edited by Oralloy; Aug 17 '10, 02:27 PM. Reason: added closing code tag

                Comment

                • mahmoodn
                  New Member
                  • May 2009
                  • 77

                  #9
                  I know that ostream works. it is fine. However I need another thing.

                  Again I have to say, this is my code
                  for ( i=0; i<100; i++ )
                  char key[16]={i,i+1,....}

                  I want the key to be
                  {0x1,0x2,0x3,0x 4,....,0xA,0xB, 0xC,0xD,0xE,0xF }

                  I don't want to use ostream.

                  Comment

                  • Oralloy
                    Recognized Expert Contributor
                    • Jun 2010
                    • 988

                    #10
                    @mahmoodn,

                    What I'm saying is that the representation of an integer is irrespective of how it is expressed as a constant in your code.

                    On (most) modern computers, the representation of an integer is as 32 binary bits, it's neither decimal or hexadecimal.

                    Did you run my little bit of code? Especially the "if" statement? The point of that was to demonstrate that "(0x0256 == 598)" is always true.

                    In other words, writing
                    Code:
                    char key[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
                    is functionally identical to
                    Code:
                    char key[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
                    Again, they're the same thing once compiled. They only look different in your source code file.

                    Does that make any sort of sense?

                    Comment

                    • mahmoodn
                      New Member
                      • May 2009
                      • 77

                      #11
                      Thanks.
                      I thought you are saying about ostream. I ran the code but didn't attention to the content of 'if'.

                      I now understand. :)

                      Comment

                      • Oralloy
                        Recognized Expert Contributor
                        • Jun 2010
                        • 988

                        #12
                        So, please help me - I know I have difficulty expressing myself on occasion, and would like your feedback.

                        In my third post, starting with "Then don't worry about making a conversion", how could I have made the answer clearer?

                        Comment

                        • mahmoodn
                          New Member
                          • May 2009
                          • 77

                          #13
                          Don't worry. It was my fault... :D

                          Comment

                          Working...