old-c-code/hexconv/hexconv.c

177 lines
3.4 KiB
C

/* hexconv - A Full Ascii to Hexidecimal & Hexidecimal to Ascii Converter.
Copyright (C) 2003 Shaun Marquardt
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define TRUE 1
#define FALSE !TRUE
void atoh( char *input );
void htoa( char *input );
void main( void )
{
char input[5000];
clrscr();
printf("1: Convert a string to Hexidecimal\n");
printf("2: Convert hexidecimal to a String\n");
printf("3: Exit\n");
printf("\nEnter your choice: ");
switch(getche())
{
case '1':
printf("\nEnter the string to be converted\n: ");
gets(input);
atoh(input);
break;
case '2':
printf("\nEnter the hexidecimal - read the README for more info\n: ");
gets(input);
htoa(input);
break;
case '3': break;
default:
printf("\nThat is an invalid choice!");
getch();
main();
} //end switch
exit(0);
}
void atoh( char *input )
{
unsigned char a;
int x = 0, col = 0;
int done;
printf("String - \"%s\"\n",input);
printf("--== Output ==--\n\n");
printf("\"");
done = FALSE;
while(!done)
{ //Go through list of characters
for(a=32;a<128;a++)
{
if(input[x] == '\0') //End of input, done.
{
printf("\"\n");
done = TRUE;
break;
}
if(a != input[x] && input[x] != '\0') //Not matching, continue
continue;
if(a == input[x] ) //Matching, print character
{
printf("%X",a);
col++;
if(col >= 79)
{
col = 0;
printf("\n\r");
}
break;
}
} //end for
x++;
} //end while
printf("\nDone. Press the enter key to exit the program.");
getch();
exit(0);
} //end atoh
void htoa( char *input )
{
unsigned char a;
char hex[5];
char buf[5];
int x = 0, col = 0;
int done;
hex[0] = '\0';
if( (strlen(input) % 2) != 0 )
{
printf("Hex characters must be entered in pairs.\n");
getch();
exit(1);
}
printf("Hex - \"%s\"\n",input);
printf("--== Output ==--\n\n");
printf("\"");
done = FALSE;
while(!done)
{
if(input[x] == '\0') //End of input, done.
{
printf("\"\n");
done = TRUE;
break;
}
if(input[x] == ' ') //Space character? Print out a space.
{
printf("%c", ' ');
x++;
}
sprintf(hex, "%c%c", toupper(input[x]), toupper(input[x+1])); //Get Hex Char
x+=2;
for(a=32;a<128;a++)
{ //Go through list of characters
sprintf(buf, "%X",a);
if(!strcmp(buf, hex)) //Matching, print character
printf("%c",a);
else //Not Matching, continue
continue;
col++;
if(col >= 79)
{
col = 0;
printf("\n\r");
}
break;
} //end "a" for loop
} //end while
printf("\nDone. Press the enter key to exit the program.");
getch();
exit(0);
} //end htoa