Encrypting text file with ASCII hex

This is a discussion on Encrypting text file with ASCII hex within the C++ Programming forums, part of the General Programming Boards category; Hello all, I need your help. I need a very quick program on taking in a file filled with characters ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Encrypting text file with ASCII hex

    Hello all,
    I need your help. I need a very quick program on taking in a file filled with characters and writing into a file with each character's ASCII (hex representation). This is a very dirty code but it does work. The problem I am having is how to write into a new file. Please help. Thank you.
    Code:
    // This program takes a sample C++ file and encrypts
    // it with the ASCII hex representation
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string lookup_hex(char blurb);
    
    int main(){
    	// creates a file pointer
    	FILE * thisFile;	// file object to read from
    	FILE * thatFile;	// file object to write from
    
    	// opens file
    	thisFile = fopen("test.txt", "r");
    	thatFile = fopen("output.txt", "w");
    	// initializes the character
    	char c;
    
    	while (thisFile != NULL){
    		// takes in a character
    		c = fgetc(thisFile);
    		
    		// evaluates character
    		string hex_value = lookup_hex(c);
    		
    		// writes ANSI characters in new file
    		fwrite(, thatFile);
    	}
    
    	return 0;
    }
    
    string lookup_hex(char blurb){
    	char test[2];
    	switch(blurb){
    	case ' ':
    		test[0] = '2';
    		test[1] = '0';
    	break;
    	case '!':
    		test[0] = '2';
    		test[1] = '1';
    	break;
    	case '"':
    		test[0] = '2';
    		test[1] = '2';
    	break;
    	case '#':
    		test[0] = '2';
    		test[1] = '3';
    	break;
    
    	};
    
    	return test;
    }

  2. #2
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Use the fstream class defined in, you guessed it, <fstream>


    The hex lookup would be so much simpler if you did.
    It would just be

    fstream_object << std::hex << static_cast<int>(c);

    But you would probably want to format it somehow so you could read it back in easily. If you want to, prepend an 'x' or '0x' to the number for easy parsing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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