# Char to Hex

This is a discussion on Char to Hex within the C Programming forums, part of the General Programming Boards category; Hi all, I am trying to read chars in from a text file and then convert them into hex so ...

1. ## Char to Hex

Hi all,

I am trying to read chars in from a text file and then convert them into hex so that i can use the text with my S-box for the AES encryption algorithm.

But i have no idea how to convert from char to hex. I have done a fair amount of googling, but i havnt really found anything that seems to work. So i thought id ask you guys

Any ideas guys?

Thanks

Baffa

2. It's all binary. In essence it is already hex, it's just a matter of interpretation.

3. Do you need to convert from a character 'A', which is 0X41, to the two characters '41'?

4. Yea thats what im after, im currently doing this to just test the input of my text which works:

Code:
FILE* inputfile;
inputfile=fopen(INPUT_FILE_NAME, "r");

if (inputfile != NULL){

while (1){
next = fgetc(inputfile);

if(next != EOF){

input_block[pos]= next;

printf("&#37;c", input_block[pos]);
pos = pos +1;

}
else break;
}

5. Surely the AES encryption takes a stream of bytes, not a stream of hex numbers?

--
Mats

6. Probably, im just starting out with this AES stuff so you probably know better than me looking at your info on your user account ;P

now that you have said that tho, i have been looking at examples of the algorithm in the various stages and the data being encrypted is always represented as hex. However, im guessing that this is just done for representation purposes and the real processing is done on the binary data.

As you can see this is a bit of a foggy grey area of my knowledge...

7. Converting a number to string, in hex, could easily be done with sprintf.
For the type, you use &#37;X.
Now the question is whether you need it or not. An encryption algorithm is usually made to encrypt whatever contents is passed onto it if I'm not mistaken. So you don't necessarily need to convert to hex before passing to the encryption algorithm.

8. ok so this is the example that ive been working from. it seems to be pretty comprehensive so i guessed it was a good source.

http://www.cs.bc.edu/~straubin/cs381...ingles2004.swf

what do we think? I think that i still need to convert to hex if I'm going to follow the example that is given here? or am i talking rubbish?

9. No. From what I understand, you are sending a buffer full of data to encrypt, not a string.

10. ok so after a bit more reading i have found that its probably a good idea to convert to binary and work at that level.

However i cannot use itoa as im intending to run the code as part of a CUDA application. As far as im aware itoa is not an ANSI C standard function and so is not supported in the CUDA extensions of C.

Annoyingly i am getting this error when i try to use sprintf or sscanf:

"AES.cu", line 48: error: argument of type "char" is incompatible with
parameter of type "const char *"
sscanf(next, "%x", &binstr);
^

"AES.cu", line 50: error: a value of type "char *" cannot be assigned to an
entity of type "char"
input_block[pos]= binstr;
^

2 errors detected in the compilation of "/tmp/tmpxft_000020c7_00000000-2.ii".

i dont know if thats my poor coding skills or what but its damn annoying. heres my code:

Code:
FILE* inputfile;
inputfile=fopen(INPUT_FILE_NAME, "r");

if (inputfile != NULL){

while (1){
next = fgetc(inputfile);

if(next != EOF){
sscanf(next, "%x", &binstr);
//		sprintf(binstr, "%x", next);
input_block[pos]= binstr;

printf("%x", input_block[pos]);
pos = pos +1;

}
else break;

11. You might want to read
http://cpwiki.sf.net/Indentation
to help out your indentation skills.

But as some has mentioned, you really don't need to convert anything. The encryption works on raw data (ie just raw numbers) and encrypts them so the original sequence of bytes can't be read until decrypted. There's no need to convert anything.

12. What is binstr? There is no definition for it here.

13. Cool thanks for the indentation stuff

not going to lie my coding skills are in need of a bit of improvement!

But, i still cant see how else im going to encrypt the characters im reading in.

Surely I would have to work with binary data for that to work?

if i were to encrypt the sentence "the cat sat on the mat" how would you do that without getting the binary values of the letters first?

oh, and the definition of binstr is as follows
Code:
char binstr[4];

14. I'm supposing you're trying to implement the algorithm yourself?
You only need to work with raw data on a byte-basis. So for that you would typically use BYTE, unsigned char. You ignore what type it is - to the encryption algorithm, it's just raw bytes. You really don't need to convert the data at all because the encryption is just supposed to make something indifferentiable from the original data and is non-readable.

15. there is no function. i am writing the function. ive gtta make my own implementation of AES. thats my point. I dont know if im coming across right or not but im writing everything from scratch basically.

Page 1 of 2 12 Last
Popular pages Recent additions