Thread: using the pointers and hexadecimal to manipulate integer varaiable

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    using the pointers and hexadecimal to manipulate integer varaiable

    Hello friends,

    I have program where my intention is to change the integer variable using a pointer .

    Here the integer variable is unsigned int val = 0xabcd1234;

    it has to be changed frin the cd to ef .

    At the end we need to display the integer variable to check the integer value.

    As per my understanding i written the program as below

    Code:
    #include <stdio.h>
    
    int main()
    {
            unsigned int val = 0xabcd1234;
            unsigned char *ptr;
            int i;
            ptr = (unsigned char *)&val;
    
    
            printf("%d \n",val);
    
            for (i=0;i<4;i++)
            {
                    printf("%02x \n",*ptr);
                    if (i == 2)
                    {
                            *ptr = (0x22 |*ptr );
                    }
                    ptr++;
            }
    
            ptr = (unsigned char *)&val;
    
            printf("After change \n");
            printf("%d \n",val);
    
            for (i=0;i<4;i++)
            {
                    printf("%02x \n",*ptr);
                    ptr++;
            }
    
    
            return 0;
    }
    output


    -1412623820
    34
    12
    cd
    ab
    After change
    -1410395596
    34
    12
    ef
    ab


    Doubts are

    1) Is it correct to display the value in integer format ?

    2) Event after we keeping the unsigned integer why does it still display like the
    value like "-1412623820"

    3) Do we have any other shortcut / method than using the bit wise operator "|" ( OR)?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have program where my intention is to change the integer variable using a pointer .
    Why do you want to use a pointer, and why a pointer to unsigned char?

    1) Is it correct to display the value in integer format ?
    You are the one who came up with the requirements, so this is your decision to make.

    2) Event after we keeping the unsigned integer why does it still display like the
    value like "-1412623820"
    Notice the &#37;d format specifier. You are printing the unsigned integer as a signed integer.

    3) Do we have any other shortcut / method than using the bit wise operator "|" ( OR)?
    It depends on what you are trying to do. After all, taking your requirements at face value, the program is terribly trivial:
    Code:
    #include <stdio.h>
    
    int main()
    {
        unsigned int val = 0xabcd1234;
        unsigned int *ptr = &val;
        *ptr = 0xabef1234;
        printf("%x\n", val);
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    Code:
    #include <stdio.h>
    
    int main()
    {
            int iVal = 0x12345678;
            char cVal = 0x9a;
            short sVal = 0xbcde;
     
            unsigned char msg[15];
    
            bzero(msg,sizeof(msg));
    
            sprintf(msg,"%x%x%x",iVal,cVal,sVal);
    
            printf("%s",msg);
    
    }
    output :

    12345678ffffff9affffbcde

    i donot understand why i get "fffff" in between the varaibles in msg.

    i know these are all might be very trivial for some people here, before posting here i indeed try a bit in the google when confirmed that the things i found are not satisfactory or confirmatiory i post here.

    thanks friends for all your knowledge sharing

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    bzero() is undefined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because char's and short's get "promoted" to ints when they get passed to *printf; and since these are negative values, they get filled with 1s on the left to keep the value.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    Hello friends,

    Iam trying to understanding the binary notation representation of basic data types in the memory locations

    My environment is like below.

    my compiler is gcc in linux environment.

    I did the following two cases through c program.



    1) lets consider the integer variable in program and it stores integer "4",its binary representation
    would be like 100 so in the
    8 byte (32 bit) the representation is 00000000 00000000 00000000 00000100

    2) lets consider the char variable and stores char "a" which is the binary value is 01100001 and it has 1 byte
    size so filled in exactly same like what i shown above.
    and in print also i get like 0xa (printing like a pointer type with %p).

    now if in my system if i do char ch = 0xbc , it does not give any warning and it stores
    this two characters in the 8 bits and prints like

    0xffffffbc

    the ff are for padding on the left , as tabstop said they promoted to int and hence they become
    negative in this process and to represent that 1's this "f" s got filled.

    as b and c are in hexadecimal it directly goes and remaining are filled f.

    if i try g it gives me warning like "invalid suffix "g" on integer constant"

    this is because its not g is not an hexadecimal.

    first tell me is this understanding correct ?

    second how come two chars are going into 8 bits in this char variable example

    if one char it does print like 0xa if two chars then 0xffffffbc why is it so ?

    if i put another char in the varible like "0xabc" in char variable gives me warning like below

    " warning: large integer implicitly truncated to unsigned type"

    prints like 0xffffffac what happend to b here ?


    Thanks guys

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    First: yes. Second: one hexadecimal digit corresponds to four bits, so bc is eight bits (not eight bytes); since a character is only eight bits, there's nothing to fill (at the moment), and since the most significant bit is set, the answer is (may be, but in this case is) interpreted as negative. 0xabc is twelve bits, which is too big for an eight-bit char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers + Malloc = Painloc
    By Chalks in forum C Programming
    Replies: 9
    Last Post: 10-19-2008, 01:10 PM
  2. Replies: 16
    Last Post: 10-03-2008, 04:35 PM