Hex

This is a discussion on Hex within the C Programming forums, part of the General Programming Boards category; I am trying to get the program to print the address in hex numbers ex. 0X2000. What can I do ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    Hex

    I am trying to get the program to print the address in hex numbers ex. 0X2000.
    What can I do to get my program to print it like that.
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    void cube(double*);
    
    
    main(){
    	
    	double variable=3.43;
    	int* pVariable;
    	
    	
    	printf("Value of variable is %.2lf\n",variable);
    	printf("Address of variable is %p\n",&variable);
    	
    	pVariable=&variable;
    	cube(&variable);
    
    	printf ("New value for variable is %lf\n",variable);
    	
    
    	
    	system("Pause");
    }
    
    void cube(double* pVariable){
    	printf("The number %.2lf\n",*pVariable);
    	*pVariable=*pVariable * *pVariable * *pVariable;
    	printf("The address of pVariable is %p\n",pVariable);
    }

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    printf("0x&#37;08X", 1024);

    Change 8 to whatever length you want it to be
    Last edited by 39ster; 04-01-2008 at 10:38 PM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    how do i get the program to do that??

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Code:
    void cube(double* pVariable){
    	printf("The number &#37;.2lf\n",*pVariable);
    	*pVariable=*pVariable * *pVariable * *pVariable;
    	printf("The address of pVariable is 0x%08X\n",pVariable);
    }
    8 should be enough for a 32 bit value. 16 for 64 bit addresses.
    Last edited by 39ster; 04-01-2008 at 10:45 PM.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    isnt that actually me inputing the value?
    How do I get the program to find the location and print it by itself? using *int and pVariable.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Just replace the cube function with the one i wrote.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    its not working its giving em errors on this line
    printf("The address of pVariable is "0x&#37;08X"\n",pVariable);

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    The problem I see is that the standard (C99, at least) specifies that &#37;p is the format specifier for pointers, and you have to cast the pointer to void*.

    On the other hand, it is likely that %p will cause the address to be printed in hex, though that is not guaranteed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Sorry, try now. (I edited my post)

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i just tried that and its giving em errors.
    what do i do to make sure it prints hex

  11. #11
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    According to laserlight you can do this:
    Code:
    void cube(double* pVariable){
    	printf("The number &#37;.2lf\n",*pVariable);
    	*pVariable=*pVariable * *pVariable * *pVariable;
    	printf("The address of pVariable is 0x%p\n",pVariable);
    }
    Btw why dont you tell us the errors?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    what do i do to make sure it prints hex
    In the first place, are you sure it does not print in hex? Try:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int num = 0;
        int *ptr = &num;
        printf("&#37;p\n", (void*)ptr);
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    thanks that worked.
    the only problem is that my assignement says that i have to use &#37;p to print hex.
    is there any othe way to print hex using %p

  14. #14
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by goran00 View Post
    thanks that worked.
    the only problem is that my assignement says that i have to use &#37;p to print hex.
    is there any othe way to print hex using %p
    I dont think you're meant to ask us to do your homework. I already posted how to do it like that anyways (look up)

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    the only problem is that my assignement says that i have to use &#37;p to print hex.
    Your assignment instructions are correct.

    is there any othe way to print hex using %p
    My example and 39ster's most recent example both print using %p. 39ster's example just adds a "0x" in front, but that has no bearing on whether the address is actually printed in hex. What number base %p prints in is implementation defined, but hex is most likely.

    EDIT:
    the above program prints
    0012FF60
    i was looking to get it to print something with an x in the middle
    ex. 0x40tt
    0012FF60 is quite definitely a number expressed in hexadecimal. What you want is to add a "0x" in front, as demonstrated by 39ster. However, note that 39ster's example is not quite correct since 39ster neglected to cast the address to void*.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 05:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  4. Replies: 3
    Last Post: 01-23-2006, 06:25 PM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 08:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21