-6

The hexadecimal value that I want to store in a variable is of length 32. "DC4938C31B9E8B30F32FC0F5EC894E16". I also want to then print this value but I don't know the format specifier in printf for unsigned char.

Manas
  • 84
  • 2
  • 7
  • Are you trying to store it as a string? Otherwise you will need an array of `unsigned char`. – wkl May 14 '12 at 15:41
  • the variable that I am using has this declaration. unsigned char out[512]; – Manas May 14 '12 at 15:42
  • 3
    Please make your question a bit clearer! It's really hard to say what you want to achieve. – guitarflow May 14 '12 at 15:43
  • OK. I am having a program which is accepting a string of unsigned char which will be an encrypted message using 3DES mode. So I think I will have to pass the message in a hexadecimal format. I have given an example of the message in hex above. This function will then decrypt the message which I have provided in this variable and will be the plaintext. – Manas May 14 '12 at 15:46
  • @Manas Do you mean it takes an *array* of uchar (e.g. `uchar[] k = {0xDC, 0x49, ...}`) or ...? –  May 14 '12 at 15:47
  • 1
    @Manas: You're happily tumbling through vocabulary, there. A string containing hexadecimal digits is one thing, a hexadecimal integer literal is another, and `printf()` can print a hexadecimal *format*, but it's still not quite clear what you want. Precision of language is a required skill in programming. – DevSolar May 14 '12 at 15:48
  • That's wat the problem is.. I have the value which I want to pass to the function I also know that it should be a string of unsigned char, but I am not able to assign it the value. I tried it in following 2 ways: 1) 1)unsigned out[BUFSIZE]={0xA0,0x69,0x57,0x3B,0x70,0x26,0x1C,0xE8,0xEF,0xF2,0x9F,0x60,0x80,0‌​x60,0xB2,0xE5}; 2)strcpy(out,"539619fbce6af887d341dfed442e8cc5"); but when I displayed this variable through printf("\nCiphertext 1 : %s\n",out); printf("\nCiphertext 2 : %x\n",out); , it was blank – Manas May 14 '12 at 15:51
  • If you people are not actually getting what I am trying to ask then please check out this link where I have discussed my question more specifically. http://stackoverflow.com/questions/10583023/in-a-c-program-how-can-i-store-a-hexadecimal-value-in-a-string-variable/10583186#comment13706390_10583186 – Manas May 14 '12 at 15:55
  • 1
    @Manas: You mean the question where you weren't any clearer in your communication? I'd recommend returning to a couple of advanced programming tutorials, instead of fiddling with some crypto API... learn to walk before you run. – DevSolar May 14 '12 at 16:03
  • Thanks for so cool suggestions guys. I know I am not even fit to stand leave about running. But what I know is that If I am able to send the desired string to this parameter of the function, rest will be taken care of by the library function. – Manas May 14 '12 at 16:06
  • ...which you didn't tell us about. This question is an exact duplicate. Close. – DevSolar May 14 '12 at 16:21

2 Answers2

2

That's a string of 32 characters, being hexadecimal digits. That makes 16 bytes, or 128 bits.

m0skit0 told you how to store it in a string. If you actually want to store that in an integer, you'd need something like unsigned long long intvar = strtoull( stringvar, NULL, 16 ) - provided "long long" on your machine can stomach 128 bits.

But what you want is something completely different, which became clear only after you linked to that other question. (It's really bad to take something out of context like that, especially if you are confused about what you're actually doing.)

If you take a look at the API documentation, you will see that the parameter you are looking at is a pointer to DES_cblock. That's not a string, and not an integer. Have a look at seed and ivsetup in that other question, how they are initialized, and think for a minute.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • 1
    Why in the world are 32 characters 16 bytes? ;-) – guitarflow May 14 '12 at 15:46
  • @guitarflow 32 *hexadecimal digits* can represent 16 octets of information. (I am sure that was a rile ;-) –  May 14 '12 at 15:48
  • Ahhhh ... of course, I thought you were talking about the C string. Damnit... ;-) – guitarflow May 14 '12 at 15:49
  • But the function that I am using is expecting a parameter of type unsigned char and not unsigned long long. Please guys check this link too. http://stackoverflow.com/questions/10583023/in-a-c-program-how-can-i-store-a-hexadecimal-value-in-a-string-variable/10583186#comment13706390_10583186 – Manas May 14 '12 at 16:01
1

That's not a char. It's a string:

unsigned char* value = "DC4938C31B9E8B30F32FC0F5EC894E16";
printf("%s\n", value);

Also, if this is a number I strongly do not suggest you transforming it into a string or char, since strings are slower and prone to more coding errors than numbers.

Numbers represented as hexadecimal (and not hexadecimal numbers, all numbers are hexadecimal, it's just a representation of the value) do not have any characters. Again, it's a number. You can of course convert it to a string (as with any other number) but to do so you should have a strong reason (e.g. the algorithm you want to use is faster with strings than numbers).

I suggest you reading about representation of numbers. A lot of new programmers have problems with this topic.

m0skit0
  • 25,268
  • 11
  • 79
  • 127