-2

I have a cryptographic program in which I need to represent each character by its hex value and return all of these hex values as a char string. After multiple crypto functions I got a final decimal value of each char. Now at the end of a for cycle I need to put all of them in a string containing each character hex value separated by space. In short, I have decimal number, which I need to convert to hex and put them in a string for return. Thanks for responses. I tried sprintf() but I obviously can't put it into char string because it can only hold one char per index. I am a university student and its my first year working with C so please keep that in mind. I hope it is not such noob question.

I tried something.

at the top before loop starts I wrote this:

char*res=calloc(size,sizeof(unsigned char));

and before the end of loop i wrote this:

sprintf(&res[i],"%02x",dec);

dec - decimal value of a character. if i print it out i get the right output.

now my output should be "80 9c 95 95 96 11 bc 96 b9 95 9d 10"

but instead I got "899991b9b9910".

it means i got only 1st character of each hex value except the last one. im sure now you might get a solution for me. THANK YOU!

KYHSGeekCode
  • 1,068
  • 2
  • 12
  • 30
  • 2
    can you give some example what you want? – yajiv Mar 17 '18 at 15:16
  • I want to return a string like this "10 9d 9c 80 95" – Samuel Gregorovič Mar 17 '18 at 15:30
  • means for 'Hello' you want to return '48 65 6c 6c 6f' right? – yajiv Mar 17 '18 at 15:36
  • I think he wants to convert char(byte) array to string – KYHSGeekCode Mar 17 '18 at 15:41
  • @yajiv yes. I do multiple functions to the input string like reversing them, then using caesar cipher and then i have to return the hex value of each character after all of these. But you got the point. – Samuel Gregorovič Mar 17 '18 at 15:45
  • As the example, please give _both_ input _and_ output. – J...S Mar 17 '18 at 15:49
  • I make multiple changes to the input line. Changing the order of letters, or using caesar cipher. Everything is working. i do this to every charaxter of input using for(). At the end of every for() i get a decimal representation of ascii character. Now i need to convert this into hex and put it into a string. In 2nd cycle of for i need to to this to 2nd character etc. At the end i need to have all of these hex values in char string and return them. It is a char* function – Samuel Gregorovič Mar 17 '18 at 15:58
  • 1
    For example. My input string is "Hello world". After making all the changes to the string i get the decimal value of encrypted char. It is 128 which means i need to get " 80" into the string(hex value). This applies to all of the characters in input string – Samuel Gregorovič Mar 17 '18 at 16:04
  • Do you mean: input=hello, etc and you process them as bytes then get some decimals. Then you print the numbers as hex string to a char buffer isn't it? – KYHSGeekCode Mar 17 '18 at 16:06
  • Exactly. I have the final encrypted char. I just have it in decimal representation in ascii.and i need it in hex. And return all of them in a string separated by space – Samuel Gregorovič Mar 17 '18 at 16:11
  • Possible duplicate of [How do you convert a byte array to a hexadecimal string in C?](https://stackoverflow.com/questions/6357031/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-in-c) – Mark Benningfield Mar 17 '18 at 16:30
  • @Mark Benningfield possible, if it is not that different from the linked post. – KYHSGeekCode Mar 17 '18 at 16:33
  • please can oyu look at the edit? i think we are close to the solution. – Samuel Gregorovič Mar 17 '18 at 17:06
  • 1
    Stack Overflow is not a forum, we don't add `[SOLVED]` to the titles. Simply click the green checkmark next to the answer that solved your problem. See [the tour](https://stackoverflow.com/tour). – Jonathon Reinhart Mar 17 '18 at 22:21

2 Answers2

1

I figured it out:

char*res = calloc(size,sizeof(unsigned char)*3);

sprintf(&res[f*3],"%x ", dec);

When I return and print res, it prints out as I wanted. Please, if you can, modify this answer for others to understand. Thank you all for help!

sg7
  • 6,108
  • 2
  • 32
  • 40
0
char * getString(unsigned char *s,int len)
{
  char *res=calloc(sizeof(unsigned char),len*3+1);
  for(int i=0;i<len;++i)
  {
    sprintf(&res[i*3],"%x ",s[i]);
  }
  return res;
}

Will this help?

Usage:

   unsigned char [] data= {'H' ,'E' , 'L', 'l', 'o'};
   char* str=getString(data,sizeof(data)/sizeof(unsigned char));
   printf("%s",str);
   free(str);

Or:

int main()
{
    unsigned char input[20] ={0,};
    scanf("%19s",input);
    encrypt(input,strlen(input));
    char* str=getString(data,sizeof(data)/sizeof(unsigned char));
   printf("%s",str);
   free(str);
   return 0;
}

And the void encrypt(unsigned char *input, int len) modifies the input array.

I think converting the data at once is easier to implement.

Also you need to #include

<stdio.h>, <stdlib.h>.

this code may add additional space at end of the line of string generated...

And this post can answer you or your question is duplicate if I got your point.

And seeing that post, I'll have to practice C skills a lot.. I'm a student either so have more time to learn..

edit

Or To fit your need(?):

char * getString(unsigned char *s,int len)
{
  char *res=calloc(sizeof(unsigned char),len*3+1);
  for(int i=0;i<len;++i)
  {
    s[i]=your_process(s[i]);
    sprintf(&res[i*3],"%x ",s[i]);
  }
  return res;
}
KYHSGeekCode
  • 1,068
  • 2
  • 12
  • 30