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. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    22

    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. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    It's all binary. In essence it is already hex, it's just a matter of interpretation.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Do you need to convert from a character 'A', which is 0X41, to the two characters '41'?

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    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){
    	
    	printf("Input File Loading....\n");
    	while (1){
    	next = fgetc(inputfile);
    	
    
    	if(next != EOF){
    		
    		input_block[pos]= next;
    		
    		printf("%c", input_block[pos]);
    		pos = pos +1;
    
    	}
    	else break;
    	}

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Surely the AES encryption takes a stream of bytes, not a stream of hex numbers?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    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. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Converting a number to string, in hex, could easily be done with sprintf.
    For the type, you use %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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    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. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    No. From what I understand, you are sending a buffer full of data to encrypt, not a string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    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){
    	
    	printf("Input File Loading....\n");
    	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. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    What is binstr? There is no definition for it here.

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    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.

    http://en.wikipedia.org/wiki/Advance...ption_Standard

    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. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    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.
    Last edited by Elysia; 01-25-2008 at 10:46 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    22
    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 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  2. Replies: 12
    Last Post: 08-12-2008, 12:02 AM
  3. lvalue error trying to copy between structures
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-16-2006, 06:53 AM
  4. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 10:40 PM

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