Converting to hex with strcat?

This is a discussion on Converting to hex with strcat? within the C Programming forums, part of the General Programming Boards category; Hi, Code: static char *tohex (const unsigned char *digest, int len) { int i; static char dest[64]; for(i = 0; ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    32

    Converting to hex with strcat?

    Hi,

    Code:
    static char *tohex (const unsigned char *digest, int len) {
      int i;
      static char dest[64];
      for(i = 0; i < len; i++){
        strcat (dest, &digest[i]);
        //printf("%02x", digest[i]); 
      }
      dest[0] = '\0';
      //printf("\n");
    return dest;
    }

    i am using md5. When i get md5 hash, i want it to be a hex value in order to use the digest in another function. But couldn't do it. Am i suppose to use sprintf instead? Why i am having a problem with strcat?

    Thank you.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    One issue here is that dest is an undefined local variable. You should set it to zero before you apply strcat to it, otherwise the end could be anywhere:
    Code:
    static char dest[64] = {0};
    Beyond that, it is hard to tell what you are trying to do.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    I just want to convert a md5 digest (such as غ%B]) to hex value (such as cbe2d8ba9c25fa42b65d9d133e8c4c32)

    it's OK with

    Code:
      for(i = 0; i < len; i++){
        printf("%02x", digest[i]); 
      }
    But i want it to pass this hex value to another function. This why i am doing this. But i am stuck...

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Ah. Strcat will definitely not work then, since you are converting those byte values to a string using "%x".

    If printf works, then sprintf() will work. Unfortunately, it doesn't concatenate. BUT, you can always use a second pointer and move it forward 2 bytes at a time, since the hex representation will always be 2 bytes. IMPORTANT NOTE: hopefully you have taken that into consideration when sizing dest. It should be TWICE the size of digest, +1 for the null terminator. An unsigned char is one byte, but to represent that value in a string as hex you need two characters, get it? (16^2=256)

    So something like:
    Code:
       static char dest[64];
       char *ptr = dest;
      for(i = 0; i < len; i++){
        sprintf(ptr, "%02x", digest[i]);
         ptr+=2;
      }
    BTW, why are you using a static char here?
    Last edited by MK27; 01-16-2010 at 10:09 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    Thank you very much. i appreciate your help. I owe you one
    Yes, i'm aware of that. I was gonna do that, though Thanks again for pointing that out.
    Btw, it's my bad.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    32
    Quote Originally Posted by MK27 View Post
    Ah. Strcat will definitely not work then, since you are converting those byte values to a string using "%x".

    If printf works, then sprintf() will work. Unfortunately, it doesn't concatenate. BUT, you can always use a second pointer and move it forward 2 bytes at a time, since the hex representation will always be 2 bytes. IMPORTANT NOTE: hopefully you have taken that into consideration when sizing dest. It should be TWICE the size of digest, +1 for the null terminator. An unsigned char is one byte, but to represent that value in a string as hex you need two characters, get it? (16^2=256)

    So something like:
    Code:
       static char dest[64];
       char *ptr = dest;
      for(i = 0; i < len; i++){
        sprintf(ptr, "%02x", digest[i]);
         ptr+=2;
      }
    BTW, why are you using a static char here?
    Long time no see. I am stuck with sth. How can i return a variable (char) from this function?

    Code:
    char c2h(const unsigned char *digest, int len)
    {
       static char dest[64];
       char *ptr = dest;
      for(i = 0; i < len; i++){
        sprintf(ptr, "%02x", digest[i]);
         ptr+=2;
    return dest;
      }
    this is not working. Thank you.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    dest is not a char. If you want to return a pointer to char, then you need to change the return type of the function.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by |HBO| View Post
    Long time no see. I am stuck with sth. How can i return a variable (char) from this function?
    You need to return a pointer to a string. The string cannot be local to the function, since it will cease to exist when the function returns. So you use malloc() to put it on the global heap:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *eg() {
    	char *p = malloc(12);
    	strcpy(p, "hello world");
    	return p;
    }
    
    int main() {
    	char *test = eg();
    
    	puts(test);
    	free(test);
    
    	return 0;
    }
    It's important to free() malloc'd memory when you are done with it.

    Alternately, you can submit a pointer and fill it in:
    Code:
    #include <string.h>
    #include <stdio.h>
    
    void eg(char *p) {
    	strcpy(p, "hello world");
    }
    
    int main() {
    	char test[12];
    
    	eg(test);
    	puts(test);
    
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    MK27:
    Another solution is to make the array static. So his first post had it right when it came to that.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by EVOEx View Post
    MK27:
    Another solution is to make the array static. So his first post had it right when it came to that.
    Ah. That's how standard lib static pointers work then! Didn't know you could do that, but I guess it makes sense.

    If you need to keep the data from one such function call and make another one, however, you will have to copy that returned data.
    Last edited by MK27; 04-09-2010 at 01:19 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting a binary string to hex
    By maxhavoc in forum C++ Programming
    Replies: 6
    Last Post: 07-25-2006, 12:46 PM
  2. converting an int into hex and memcpy
    By puppy in forum C Programming
    Replies: 10
    Last Post: 07-03-2006, 11:39 AM
  3. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  4. converting hex to dec
    By jibbles in forum C Programming
    Replies: 20
    Last Post: 08-07-2004, 11:40 PM
  5. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 10:37 AM

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