• Здраво и добредојдовте на форумот на IT.mk.

    Доколку сеуште не сте дел од најголемата заедница на ИТ професионалци и ентузијасти во Македонија, можете бесплатно да се - процесот нема да ви одземе повеќе од 2-3 минути, а за полесна регистрација овозможивме и регистрирање со Facebook и Steam.

[C] CoolCon.c

  • Ја почнал/а темата
  • #1

LiquidWorm

Администратор
26 март 2007
2,667
183
www.zeroscience.mk
Едноставен command-line конвертор напишан во C, кој го конвертира внесувањето како текст или број.
(За Windows платформи)
- ASCII во бинарно, децимално, октално, хексадецимално.
- Бинарно во децимално, октално, хексадецимално.
- Децимално во бинарно, октално, хексадецимално.
- ROT13.
- Табели: ASCII и Base64.

Код:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

	
	void welcome(void);
	void what2what(void);
	void choose_string(void);
	void choose_number(void);
	void char2bin(void);
	void char2oct(void);
	void char2hex(void);
	void char2ascii(void);
	void rot13(void);
	void asciialpha(void);
	void base64alpha(void);
	void dec2bin(void);
	void dec2oct(void);
	void dec2hex(void);
	int binary2dec();
	int bin2dec(char *bin);
	int bin2oct();
	int bin2hex();



unsigned char text[256];
int broj;
char str[256];
int duzina, red, n;


void welcome()
{

	printf("nn################################################################################nn");
	printf("tt     _____            _    _____n");
	printf("tt    / ____|          | |  / ____|n");
	printf("tt   | |     ___   ___ | | | |     ___  _  __n");  
	printf("tt   | |    / _ \ / _ \| | | |    / _ \| '_  \ n");
	printf("tt   | |___| (_) | (_) | | | |___| (_) | | | |n");
	printf("tt    \_____\___/ \___/|_|  \_____\___/|_| |_| ");
	printf("nnn################################################################################n");
	printf("ttttCoolCon v0.01n");
	printf("tt   by LiquidWorm <liquidworm [at] gmail.com>nnn");
	printf("ttt   (1) Text conversionsn");
	printf("ttt   (2) Number conversionsn");
	printf("ttt   (3) EXITnnn");
}


void what2what()
{
	char what;
	
	printf("ttttPick a choice... ");
	what=getchar();
	if(what == '1')
	{
		choose_string();
		fflush(stdin);
	} else if(what == '2')
		{
			choose_number();
			fflush(stdin);
		} else if(what == '3')
			{
				exit(1);
			} else
				{
					printf("nttttaPlease choose 1 or 2.nn");
					fflush(stdin);
					return(what2what());
				}
}



void choose_number()
{
	char choicenum;
	
	fflush(stdin);
	printf("nntttChoose an option number...nnn");
	printf("ttt  (1) Decimal  -->  Binaryn");
	printf("ttt  (2) Binary  -->  Decimaln");
	printf("ttt  (3) Decimal  -->  Octaln");
	printf("ttt  (4) Binary  -->  Octaln");
	printf("ttt  (5) Binary  -->  Hexadecimal n");
	printf("ttt  (6) Decimal  -->  Hexadecimaln");
	printf("ttt  (7) Show ASCII tablen");
	printf("ttt  (8) Show Base64 tablen");
	printf("ttt  (9) Main Menunn");
	
	choicenum=getchar();
	switch(choicenum)
	{
		case '1':
			printf("n1. [ Decimal to Binary ]nn");
			dec2bin();
			fflush(stdin);
			
			while(choicenum != 'n' || choicenum != 'N')
			{
				fflush(stdin);
				printf("nnAnother conversion from Decimal to Binary (y/n)? ");
				fflush(stdin);
				choicenum=getchar();
				fflush(stdin);
				if(choicenum == 'y' || choicenum == 'Y')
				{ 
					fflush(stdin);
					dec2bin();
				}
				else if(choicenum == 'n' || choicenum == 'N')
					{
						return(choose_number());
					}
					else
						{
							printf("nntaYes or No please.n");
						}
			}
		case '2':
			printf("n2. [ Binary to Decimal ]nn");
			binary2dec();
			fflush(stdin);
			
			while(choicenum != 'n' || choicenum != 'N')
			{
				fflush(stdin);
				printf("nnAnother conversion from Binary to Decimal (y/n)? ");
				fflush(stdin);
				choicenum=getchar();
				fflush(stdin);
				if(choicenum == 'y' || choicenum == 'Y')
				{ 
					fflush(stdin);
					binary2dec();
				}
				else if(choicenum == 'n' || choicenum == 'N')
					{
						return(choose_number());
					}
					else
						{
							printf("nntaYes or No please.n");
						}
			}
		case '3':
			printf("n3. [ Decimal to Octal ]nn");
			dec2oct();
			fflush(stdin);
			
			while(choicenum != 'n' || choicenum != 'N')
			{
				fflush(stdin);
				printf("nnAnother conversion from Decimal to Octal (y/n)? ");
				fflush(stdin);
				choicenum=getchar();
				fflush(stdin);
				if(choicenum == 'y' || choicenum == 'Y')
				{ 
					fflush(stdin);
					dec2oct();
				}
				else if(choicenum == 'n' || choicenum == 'N')
					{
						return(choose_number());
					}
					else
						{
							printf("nntaYes or No please.n");
						}
			}
		case '4':
			printf("n4. [ Binary to Octal ]nn");
			bin2oct();
			fflush(stdin);
			
			while(choicenum != 'n' || choicenum != 'N')
			{
				fflush(stdin);
				printf("nnAnother conversion from Binary to Octal (y/n)? ");
				fflush(stdin);
				choicenum=getchar();
				fflush(stdin);
				if(choicenum == 'y' || choicenum == 'Y')
				{ 
					fflush(stdin);
					bin2oct();
				}
				else if(choicenum == 'n' || choicenum == 'N')
					{
						return(choose_number());
					}
					else
						{
							printf("nntaYes or No please.n");
						}
			}
		case '5':
			printf("n5. [ Binary to Hexadecimal ]nn");
			bin2hex();
			fflush(stdin);
			
			while(choicenum != 'n' || choicenum != 'N')
			{
				fflush(stdin);
				printf("nnAnother conversion from Binary to Hexadecimal (y/n)? ");
				fflush(stdin);
				choicenum=getchar();
				fflush(stdin);
				if(choicenum == 'y' || choicenum == 'Y')
				{ 
					fflush(stdin);
					bin2hex();
				}
				else if(choicenum == 'n' || choicenum == 'N')
					{
						return(choose_number());
					}
					else
						{
							printf("nntaYes or No please.n");
						}
			}
		case '6':
			printf("n6. [ Decimal to Hexadecimal ]nn");
			dec2hex();
			fflush(stdin);
			
			while(choicenum != 'n' || choicenum != 'N')
			{
				fflush(stdin);
				printf("nnAnother conversion from Decimal to Hexadecimal (y/n)? ");
				fflush(stdin);
				choicenum=getchar();
				fflush(stdin);
				if(choicenum == 'y' || choicenum == 'Y')
				{ 
					fflush(stdin);
					dec2hex();
				}
				else if(choicenum == 'n' || choicenum == 'N')
					{
						return(choose_number());
					}
					else
						{
							printf("nntaYes or No please.n");
						}
			}
		case '7':
			asciialpha();
			fflush(stdin);
			printf("nnPress any key to continue...");
			getchar();
			return(choose_number());
		case '8':
			base64alpha();
			fflush(stdin);
			printf("nPress any key to continue...");
			getchar();
			return(choose_number());
		case '9':
			fflush(stdin);
			welcome();
			what2what();
		default:
			printf("naChoose between 1 - 8 please.n");
			fflush(stdin);
			return(choose_number());
	}
}
 
  • Ја почнал/а темата
  • #2

LiquidWorm

Администратор
26 март 2007
2,667
183
www.zeroscience.mk
Продолжува

Код:
void choose_string()
{
	char choicestring;

	fflush(stdin);
	printf("nntttChoose an option number...nnn");
	printf("ttt  (1) Text  -->  ASCIIn");
	printf("ttt  (2) Text  -->  Binaryn");
	printf("ttt  (3) Text  -->  Octaln");
	printf("ttt  (4) Text  -->  Hexadecimaln");
	printf("ttt  (5) ROT13n");
	printf("ttt  (6) Show ASCII tablen");
	printf("ttt  (7) Show Base64 tablen");
	printf("ttt  (8) Main Menun");

	choicestring=getchar();
	switch(choicestring)
	{
		case '1':
			printf("n1. [ Text to ASCII ]nn");
			char2ascii();
			fflush(stdin);
			
			while(choicestring != 'n' || choicestring != 'N')
			{
				fflush(stdin);
				printf("nnAnother conversion from Text to ASCII (y/n)? ");
				fflush(stdin);
				choicestring=getchar();
				fflush(stdin);
				if(choicestring == 'y' || choicestring == 'Y')
				{ 
					fflush(stdin);
					char2ascii();
				}
				else if(choicestring == 'n' || choicestring == 'N')
					{
						return(choose_string());
					}
					else
						{
							printf("nntaYes or No please.n");
						}
			}
		case '2':
			printf("n2. [ Text to Binary ]nn");
			char2bin();
			fflush(stdin);
			
			while(choicestring != 'n' || choicestring != 'N')
			{
				fflush(stdin);
				printf("nnAnother conversion from Text to Binary (y/n)? ");
				fflush(stdin);
				choicestring=getchar();
				fflush(stdin);
				if(choicestring == 'y' || choicestring == 'Y')
				{ 
					fflush(stdin);
					char2bin();
				}
				else if(choicestring=='n' || choicestring=='N')
					{
						return(choose_string());
					}
					else
						{
							printf("nntaYes or No please.n");
						}
			}
		case '3':
			printf("n3. [ Text to Octal ]nn");
			char2oct();
			fflush(stdin);
			
			while(choicestring != 'n' || choicestring != 'N')
			{
				fflush(stdin);
				printf("nnAnother conversion from Text to Octal (y/n)? ");
				fflush(stdin);
				choicestring=getchar();
				fflush(stdin);
				if(choicestring == 'y' || choicestring == 'Y')
				{ 
					fflush(stdin);
					char2oct();
				}
				else if(choicestring == 'n' || choicestring == 'N')
					{
						return(choose_string());
					}
					else
						{
							printf("nntaYes or No please.n");
						}
			}
		case '4':
			printf("n4. [ Text to Hexadecimal ]nn");
			char2hex();
			fflush(stdin);
			
			while(choicestring != 'n' || choicestring != 'N')
			{
				fflush(stdin);
				printf("nnAnother conversion from Text to Hexadecimal (y/n)? ");
				fflush(stdin);
				choicestring=getchar();
				fflush(stdin);
				if(choicestring == 'y' || choicestring == 'Y')
				{ 
					fflush(stdin);
					char2hex();
				}
				else if(choicestring=='n' || choicestring=='N')
					{
						return(choose_string());
					}
					else
						{
							printf("nntaYes or No please.n");
						}
			}
		case '5':
			printf("n5. [ ROT13 ]nn");
			rot13();
			fflush(stdin);

			while(choicestring != 'n' || choicestring != 'N')
			{
				fflush(stdin);
				printf("nnAnother ROT13 conversion (y/n)? ");
				fflush(stdin);
				choicestring=getchar();
				fflush(stdin);
				if(choicestring == 'y' || choicestring == 'Y')
				{ 
					fflush(stdin);
					rot13();
				}
				else if(choicestring=='n' || choicestring=='N')
					{
						return(choose_string());
					}
					else
						{
							printf("nntaYes or No please.n");
						}
			}
		case '6':
			asciialpha();
			fflush(stdin);
			printf("nnPress any key to continue...");
			getchar();
			return(choose_string());
		case '7':
			base64alpha();
			fflush(stdin);
			printf("nPress any key to continue...");
			getchar();
			return(choose_string());
		case '8':
			fflush(stdin);
			welcome();
			what2what();
		default:
			printf("naPlease choose 1-8 please.n");
			fflush(stdin);
			return(choose_string());
	}
}
 
  • Ја почнал/а темата
  • #3

LiquidWorm

Администратор
26 март 2007
2,667
183
www.zeroscience.mk
Продолжува

Код:
void base64alpha()
{
	printf("nShowing the Base64 alphabet ---------->n");
	sleep(1000);

	printf("nn 0 = 'A't");
	printf(" 1 = 'B't");
	printf(" 2 = 'C't");
	printf(" 3 = 'D't");
	printf(" 4 = 'E't");
	printf(" 5 = 'F't");
	printf(" 6 = 'G't");
	printf(" 7 = 'H't");
	printf(" 8 = 'I't");
	printf(" 9 = 'J't");
	printf("10 = 'K't");
	printf("11 = 'L't");
	printf("12 = 'M't");
	printf("13 = 'N't");
	printf("14 = 'O't");
	printf("15 = 'P't");
	printf("16 = 'Q't");
	printf("17 = 'R't");
	printf("18 = 'S't");
	printf("19 = 'T't");
	printf("20 = 'U't");
	printf("21 = 'V't");
	printf("22 = 'W't");
	printf("23 = 'X't");
	printf("24 = 'Y't");
	printf("25 = 'Z't");
	printf("26 = 'a't");
	printf("27 = 'b't");
	printf("28 = 'c't");
	printf("29 = 'd't");
	printf("30 = 'e't");
	printf("31 = 'f't");
	printf("32 = 'g't");
	printf("33 = 'h't");
	printf("34 = 'i't");
	printf("35 = 'j't");
	printf("36 = 'k't");
	printf("37 = 'l't");
	printf("38 = 'm't");
	printf("39 = 'n't");
	printf("40 = 'o't");
	printf("41 = 'p't");
	printf("42 = 'q't");
	printf("43 = 'r't");
	printf("44 = 's't");
	printf("45 = 't't");
	printf("46 = 'u't");
	printf("47 = 'v't");
	printf("48 = 'w't");
	printf("49 = 'x't");
	printf("50 = 'y't");
	printf("51 = 'z't");
	printf("52 = '0't");
	printf("53 = '1't");
	printf("54 = '2't");
	printf("55 = '3't");
	printf("56 = '4't");
	printf("57 = '5't");
	printf("58 = '6't");
	printf("59 = '7't");
	printf("60 = '8't");
	printf("61 = '9't");
	printf("62 = '+'t");
	printf("63 = '/'t");
	printf("nn");
	fflush(stdin);  //  Change into fpurge(stdin); if on Unix !
}



void asciialpha()
{
	unsigned char a;

	printf("nShowing the ASCII alphabet ---------->nn");
	sleep(1000);
	for(a=32;a<128;a=a+1)
	{
		printf("%3d = '%c't",a,a);
	}
}



void char2bin()
{
	fflush(stdin);
	printf("nEnter text: ");
	gets(text);
	fflush(stdin);
	duzina=strlen(text);
	printf(""%s" in binary is: nn", text);
	for(n=0;n<duzina;n++)
	{
		printf("%s ", itoa(text[n], str, 2));
	}
}



void char2oct()
{
	fflush(stdin);
	printf("nEnter text: ");
	gets(text);
	fflush(stdin);
	duzina=strlen(text);
	printf(""%s" in Octal is: nn",text);
	for(n=0;n<duzina;n++)
	{
		printf("%s ", itoa(text[n], str, 8));
	}
}



void char2hex()
{
	fflush(stdin);
	printf("nEnter text: ");
	gets(text);
	fflush(stdin);
	duzina=strlen(text);
	printf(""%s" in Hexadecimal is: nn",text);
	for(n=0;n<duzina;n++)
	{
		printf("%x ", text[n]);
	}
}



void char2ascii()
{
	fflush(stdin);
	printf("nEnter text: ");
	gets(text);
	fflush(stdin);
	duzina=strlen(text);
	printf("n"%s" in ASCII is: nn",text);
	for(n=0;n<duzina;n++)
	{
		printf("%d ",text[n]);
	}
	fflush(stdin);
}



void dec2bin()
{
	fflush(stdin);
	printf("nEnter number: ");
	gets(text);
	fflush(stdin);
	broj=atoi(text);
	printf("n%s in Binary is: ",text);
	printf("%sn", itoa(broj, str, 2));
}



void dec2oct()
{
	fflush(stdin);
	printf("nEnter number: ");
	gets(text);
	fflush(stdin);
	broj=atoi(text);
	printf("n%s in Octal is: ",text);
	printf("%on",broj);
}



void dec2hex()
{
	fflush(stdin);
	printf("nEnter number: ");
	gets(text);
	fflush(stdin);
	broj=atoi(text);
	printf("n%s in Hexadecimal is: ",text);
	printf("%xn",broj);
}
 
  • Ја почнал/а темата
  • #4

LiquidWorm

Администратор
26 март 2007
2,667
183
www.zeroscience.mk
Продолжува

Код:
void rot13()
{
	fflush(stdin);
	printf("nEnter text: ");
	gets(text);
	fflush(stdin);
	duzina=strlen(text);
	printf("n"%s" in ROT13 is: nn",text);
	for(n=0;n<duzina;n++)
	{
		fflush(stdin);
		
		if(text[n] ==' ') putchar(' '); /* <-- SPACE CHARACTER */
		if(text[n] =='a') putchar('n'); if(text[n] =='A') putchar('N');
		if(text[n] =='b') putchar('o'); if(text[n] =='B') putchar('O');
		if(text[n] =='c') putchar('p'); if(text[n] =='C') putchar('P');
		if(text[n] =='d') putchar('q'); if(text[n] =='D') putchar('Q');
		if(text[n] =='e') putchar('r'); if(text[n] =='E') putchar('R');
		if(text[n] =='f') putchar('s'); if(text[n] =='F') putchar('S');
		if(text[n] =='g') putchar('t'); if(text[n] =='G') putchar('T');
		if(text[n] =='h') putchar('u'); if(text[n] =='H') putchar('U');
		if(text[n] =='i') putchar('v'); if(text[n] =='I') putchar('V');
		if(text[n] =='j') putchar('w'); if(text[n] =='J') putchar('W');
		if(text[n] =='k') putchar('x'); if(text[n] =='K') putchar('X');
		if(text[n] =='l') putchar('y'); if(text[n] =='L') putchar('Y');
		if(text[n] =='m') putchar('z'); if(text[n] =='M') putchar('Z');
		if(text[n] =='n') putchar('a'); if(text[n] =='N') putchar('A');
		if(text[n] =='o') putchar('b'); if(text[n] =='O') putchar('B');
		if(text[n] =='p') putchar('c'); if(text[n] =='P') putchar('C');
		if(text[n] =='q') putchar('d'); if(text[n] =='Q') putchar('D');
		if(text[n] =='r') putchar('e'); if(text[n] =='R') putchar('E');
		if(text[n] =='s') putchar('f'); if(text[n] =='S') putchar('F');
		if(text[n] =='t') putchar('g'); if(text[n] =='T') putchar('G');
		if(text[n] =='u') putchar('h'); if(text[n] =='U') putchar('H');
		if(text[n] =='v') putchar('i'); if(text[n] =='V') putchar('I');
		if(text[n] =='w') putchar('j'); if(text[n] =='W') putchar('J');
		if(text[n] =='x') putchar('k'); if(text[n] =='X') putchar('K');
		if(text[n] =='y') putchar('l'); if(text[n] =='Y') putchar('L');
		if(text[n] =='z') putchar('m'); if(text[n] =='Z') putchar('M');
		if(text[n] =='0') putchar('0'); if(text[n] =='1') putchar('1');
		if(text[n] =='2') putchar('2'); if(text[n] =='3') putchar('3');
		if(text[n] =='4') putchar('4'); if(text[n] =='5') putchar('5');
		if(text[n] =='6') putchar('6'); if(text[n] =='7') putchar('7');
		if(text[n] =='8') putchar('8'); if(text[n] =='9') putchar('9');
		if(text[n] =='`') putchar('`'); if(text[n] =='~') putchar('~');
		if(text[n] =='!') putchar('!'); if(text[n] =='@') putchar('@');
		if(text[n] =='#') putchar('#'); if(text[n] =='$') putchar('$');
		if(text[n] =='%') putchar('%'); if(text[n] =='^') putchar('^');
		if(text[n] =='&') putchar('&'); if(text[n] =='*') putchar('*');
		if(text[n] =='(') putchar('('); if(text[n] ==')') putchar(')');
		if(text[n] =='-') putchar('-'); if(text[n] =='_') putchar('_');
		if(text[n] =='=') putchar('='); if(text[n] =='+') putchar('+');
		if(text[n] =='\') putchar('\'); if(text[n] =='|') putchar('|');
		if(text[n] =='[') putchar('['); if(text[n] =='{') putchar('{');
		if(text[n] ==']') putchar(']'); if(text[n] =='}') putchar('}');
		if(text[n] ==';') putchar(';'); if(text[n] ==':') putchar(':');
		if(text[n] =='\'') putchar('\''); if(text[n] =='"') putchar('"');
		if(text[n] ==',') putchar(','); if(text[n] =='<') putchar('<');
		if(text[n] =='.') putchar('.'); if(text[n] =='>') putchar('>');
		if(text[n] =='/') putchar('/'); if(text[n] =='?') putchar('?');
	}
	fflush(stdin);
}



int bin2hex()
{
	char bin[256] = "";
	char *p;
	int dec;

	printf("nEnter number: ");
	fflush(stdin);
	fgets(bin, sizeof(bin), stdin);
	if ((p = strchr(bin,'n')) != NULL)
	{
		*p = '�';
	}
	dec = bin2dec(bin);
	if(dec)
	{
		printf("n%s in Hexadecimal is: ",bin);
		printf("%xn",dec);				 
	}
}



int bin2oct()
{
	char bin[256] = "";
	char *p;
	int dec;

	printf("nEnter number: ");
	fflush(stdin);
	fgets(bin, sizeof(bin), stdin);
	if ((p = strchr(bin,'n')) != NULL)
	{
		*p = '�';
	}
	dec = bin2dec(bin);
	if(dec)
	{
		printf("n%s in Octal is: ",bin);
		printf("%on",dec);				 
	}
}



int binary2dec()
{
	char bin[256] = "";
	char *p;
	int dec;

	printf("nEnter number: ");
	fflush(stdin);
	fgets(bin, sizeof(bin), stdin);
	if ((p = strchr(bin,'n')) != NULL)
	{
		*p = '�';
	}
	dec = bin2dec(bin);
	if(dec)
	{
		printf("n%s in Decimal is: ",bin);
		printf("%dn",dec);				 
	}
}



int bin2dec(char *bin)
{
	int b, k, m, z;
	int len, sum = 0;
	
	len = strlen(bin) - 1;
	for(k = 0; k <= len; k++)
	{
		z = (bin[k] - '0');
		if ((z > 1) || (z < 0))
		{
			puts("nnaBinary number please.");
			fflush(stdin);
			return(0);
		}
		for(b=1,m=len;m>k;m--)
		{
		b *= 2;
		}
		sum = sum + z * b;
	}
	return(sum);
}



int main()
{
	welcome();
	what2what();
}
//it.com.mk
Извор(бинарно и изворен код): http://www.packetstormsecurity.org/filedesc/CoolCon0.01.rar.html

:))
 
  • Ја почнал/а темата
  • #5

LiquidWorm

Администратор
26 март 2007
2,667
183
www.zeroscience.mk
Има проблеми во \n, \t, \a, \r... синтаксата, треба да средиме форумот, не става \(коса црта) пред нив ...ќе треба самите да ги додадете или од изворот го има коректниот код.
А во GeSHi уште поголеми проблеми со вчитување на кодови...ќе се среди.
Благодарам. ;)

:rolleyes:
 

Нови мислења

Последни Теми

Статистика

Теми
43,498
Мислења
821,846
Членови
28,041
Најнов член
OgnenIvanovski
На врв Дно