Thread: Why the result is different for the hex string

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    49

    Why the result is different for the hex string

    I have a charater pointer, and want to display it in Hex String.

    Display Method 1:
    Code:
    for (i=0; i<ret; i++)
    {
            printf(" %02X, ", *out);
            out++;
    }
    Result:
    9D ED 8B 7A 6E 39 7A D6 72 9D E8 26 7E 5F 16 32 BF 34 36 F1 52 F9 FB 4A 0F 5F 98 34 C6 6D 66 98 85 44 DD 5B 33 3D A8 98 0E 86 0E 9C C8 AA 55 06 A3 48 03 64 B9 D5 58 D0 88 8E 32 E2 CA 3B 59 EB 1A 79 22 78 9F 68 5A 3D B1 D9 85 AA 39 50 ED 15 96 6A 24 F6 CF 84 78 1E 9A 47 D9 3D 9E 2A D5 06 7B E9 8A DC 35 FD A7 1D 1B D8 89 43 61 B3 6E 7A 2A 0C A6 46 DB 30 DF 49 9C B7 92 6F 4F F5 C4 4D

    Display Method 2:
    Code:
    for (i=0; i<ret; i++)
    {
            printf("%02X, ", *out);
            out++;
    }
    Result:
    20F2DD76589DEA2CD966C29D88DABB731A5F4593934E329F62 A2ECABE683E6BEE4972595A034C14A6F1F9D1221A874D4D191 259B0F59B8B5F7E3CF4BA0F0B9C9140D6B947967B1BD1C6793 745D9F2DD8D51B6EE88E4255C76BCCF9CE479F7C0F7528F27B 78EC3213393E65745961CBBD7F26D1589E151CDCAD602089F6 AD1D8F

    Display Method 3:
    Code:
    unsigned char* outarr[128];
    unsigned char* outstr[256];
    memcpy(outarr, out,strlen(out));
        
    int k=0;
    for(k=0;k<128;k++)
    	{
            sprintf(outstr+2*k, "%02X", outarr[k]);
    	}
     printf("%s\n",outstr );
    Result:
    76DDF2202CEA9D589DC266D973BBDA8893455F1A9F324E93AB ECA262BEE683E6952597E44AC134A0129D1F6FD474A8219B25 91D1B5B8590F4BCFE3F7C9B9F0A0946B0D14BDB16779749367 1CD82D9F5DE86E1BD5C755428ECEF9CC6BF7C9F47

    Which is the right Hex String? Many thanks!!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Result 1 and result 2 don't match the format string ( no commas in the output )
    Kurt

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by homoon View Post
    Which is the right Hex String? Many thanks!!
    To answer that we would need to know what string *out points to wouldn't we?

    My suspicion is none.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The third method is definitely wrong, since it is attempting to write 257 characters to a 256 character buffer. (sprintf() adds a terminating zero byte). It is also implementation-defined whether or not char is unsigned, but sprintf() writes to a char buffer, not a buffer of unsigned char. However, if all you're doing is printing out outstr using %s, that should make no difference.

    The first example outputs whitespace, and no commas are shown in any of your output. Apart from that, the first two code fragments are equivalent (assuming that ret has the same value, that out points at the same address). However, your first two examples also change the value of out, so it points to a different address after each loop than it did before. If you expect those two examples to give the same output, then you need to ensure that ret and out have the same values at the start of both loops.

    Without a small but complete sample of code that provides context in which you execute your sample code, all anyone can say is that one of them is wrong in the context in which it is run, but nobody can tell you which one.
    Last edited by grumpy; 04-29-2012 at 04:53 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-04-2011, 06:16 PM
  2. Function result as a string
    By nowber in forum C Programming
    Replies: 6
    Last Post: 10-28-2009, 05:40 AM
  3. unexpected result, no display of input string
    By xephyr in forum C Programming
    Replies: 11
    Last Post: 08-04-2004, 07:22 PM
  4. Replies: 10
    Last Post: 03-19-2003, 02:15 PM
  5. Not getting good result searching an array of string.
    By cazil in forum C++ Programming
    Replies: 5
    Last Post: 02-12-2002, 11:24 AM