Thread: Printing Hex Value of a char

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Printing Hex Value of a char

    I want to see the Hex representation of an 8 character long aray of char. The value printed is not correct. I'm not really sure on the syntax associated with printing Hex notation.
    Code:
    # include <stdio.h>
    
    int
    main ()
    {
    	char Name[8];
    
    	printf("Enter an 8 letter name:\n\n");
    
    	scanf("%s", &Name);
    
    	printf("name entered: %s\n\n", Name);
    	printf("name entered in Hex: %08x\n\n", Name);
    
    	return 0;
    }
    Thanks,

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    try:
    Code:
    # include <stdio.h>
    
    int
    main ()
    {
    	char Name[9]={0x0};
        char *p=NULL;
    	printf("Enter an 8 letter name:\n\n");
    
    	fgets(Name, sizeof(Name),stdin);
    
    	printf("name entered: %s\n\n", Name);
    	printf("name entered in Hex:");
    	for(p=Name;*p && *p>' ';p++)
    	{
    	    printf("%02x",*p);
    	}
        printf("\n");
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char Name[9];
    
    	printf("Enter an 8 letter name:\n\n");
    
    	scanf("%s", Name);
    
    	printf("name entered: %s\n\n", Name);
    	printf("name entered in Hex: %08x %08x\n\n", *(int *)Name, *(int *)(Name+4));
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Quote Originally Posted by swoopy
    Try this.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char Name[9];
    
    	printf("Enter an 8 letter name:\n\n");
    
    	scanf("%s", Name);
    
    	printf("name entered: %s\n\n", Name);
    	printf("name entered in Hex: %08x %08x\n\n", *(int *)Name, *(int *)(Name+4));
    
    	return 0;
    }

    this code reverses the characters somehow. It prints the first 4 in reverse order and then the next 4 in reverse, but fill the empty array space with CC INSTEAD OF 20.

    I can print it one byte at a time. That seems to work. But I just did that by trial and error, to get the right format.
    Could someone tell me what is the proper format for ptinting the hex. What is put in the SOMETHING %SOMETHINGx and how deos it effect the ptintout?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >It prints the first 4 in reverse order and then the next 4 in reverse
    Oops, sorry about that, when printing something cast as int, it will print msb to lsb. It's better to do like Jim's code, and use a loop.

    >but fill the empty array space with CC INSTEAD OF 20.
    This I can't explain, unless you left off some parentheses.

    Here's another example.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char Name[9];
    
    	printf("Enter an 8 letter name:\n\n");
    
    	scanf("%s", Name);
    
    	printf("name entered: %s\n\n", Name);
    
    	int i;
    	int len = strlen(Name);
    	for (i=0; i<len; i++)
    	{
    	   printf("%02x",Name[i]);
    	   if ((i+1)%4 == 0)
    	      printf(" ");
    	}
    	printf("\n");
    
    	return 0;
    }
    >What is put in the SOMETHING %SOMETHINGx and how deos it effect the ptintout?
    %x and %X can only be used on integer types, like char, int, long. To print arrays you need to do it one element at a time, or fool it using casts.
    Last edited by swoopy; 05-25-2005 at 09:45 AM.

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Yep, that's how I wrote my code, in a loop. Thanks lot.

    A.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I've seen this too many times in this thread.
    Code:
    scanf("%s", Name);
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)

    [edit]Or at least prop it up a bit.
    Code:
    if ( scanf("%8s", Name) == 1 ) { /* ... */ }
    Last edited by Dave_Sinkula; 05-25-2005 at 10:51 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM