convert int to char

Should this work?

1
2
unsigned long long int msglength;
char message_length[8] = {'(char)msglength'}


They have the same amount of bits. It compiles, but I don't think I'm getting the results I expected. Here is what I'm trying to do.

I'm trying to teach myself bit manipulation. To do this I am trying to write my own SHA-256 algorithm using criteria in FIPS PUB 180-4.

I'm getting stuck at the beginning with the padding. The padding needs to be a multiple of 512 bits. The format is like this

< x input bits >1< y zero bits >< 64 bits denoting the length of x >

It seems that the most logical container for this is a character array.
The part that stumps me the most is the last 64 bits. I thought I came up with an easy way to do it, but I don't think it works. Since I basically just need a 64 bit number, my idea was this. why can't I just store the length in an unsigned long long int and copy it to the last 8 bytes of the padding.
The reason I don't think it works, well this is what I tried.

1
2
3
unsigned long long int msglength = 5;
char message_length[8] = {'(char)msglength'}
cout << message_length[7] <<endl;


since 5 only requires 8 bits I expected that container to be holding the value of 5, or "00000101", I would think the others would be all zero. Instead It prints nothing to the screen. Could someone tell me the error of my ways :)
Try using reinterpret_cast. http://www.cplusplus.com/doc/tutorial/typecasting/

Bye!
'(char)msglength' should cause an error; "" is used for strings, '' - for single characters.

If you use reinterpret_cast, you will have character for whatever your int in (probably)ASCII is.

If you want to convert number to string, try using stringstream.
Good one, MatthewRock, but I've thinked on this.
std::stringstream returns an char*, but he's using an char array. Here is the problem. None type-casting can solve it, but I was thinking and... I made it!! std::to_string will make this for you. You can use an std::string. It looks like an "infinite char array". Both need #include <string>

Good Bye!
thank you both for your help. I'l give that a try.
Topic archived. No new replies allowed.