VB icon

Acsii Table

Email
Submitted on: 8/13/2016 9:05:11 PM
By: Ben128  
Level: Beginner
User Rating: By 1 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++
Views: 2958
 
     //Displays a list of Acsii chars in Decimal, Oct, Hex, Binary and the sysmbol and HTML codes //Chars below 31 print there names. Hope you like it.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Acsii Table
// Description://Displays a list of Acsii chars in Decimal, Oct, Hex, Binary and the sysmbol and HTML codes
//Chars below 31 print there names. Hope you like it.
// By: Ben128
//**************************************

//Displays a list of Acsii chars in Decimal, Oct, Hex, Binary and the sysmbol and HTML codes
//Chars below 31 print there names.
//Also thanks to Tom for adviseing me how to fix memory leak in Dec2Bin.
#include <iostream>
#include <iomanip>
using namespace std;
using std::cout;
using std::endl;
char *Dec2Bin(int dec){
	char TempBin[32];
	int j = 0;
	int i = 30;
	int t = dec;
	int bitcnt = 0;
	bitcnt = 7;
	while(i >=0)
	{
		//Build the binary number.
		if((dec & (1 << i)) != 0){
			TempBin[j] = '1';
		}else{
			TempBin[j] = '0';
		} 
		//INC Counters
		i--;
		j++;
}
	
	//Reserve array contents
	for(i=bitcnt;i>=0;i--){
		//Reserve array chars
		TempBin[bitcnt-i] = TempBin[30-i];
	}
	//Convert char array to Char*
	i = strlen(TempBin);
	//Resize array to hold the data
	char *pBin =(char*)malloc(i+1*sizeof(char));
	//Copy from TempBin to pStr
	strcpy(pBin,TempBin);
	//Set ending
	pBin[bitcnt+1] = '\0';
	//Clear up
	memset(TempBin,NULL,sizeof(TempBin));
	//Return
	return pBin;
}
std::string Hex2(int number)
{
 char buffer[5]; // assume 32 bit && sizeof(int) == 4
 sprintf(buffer, "%02X", number);
 return std::string(buffer, buffer+5);
}
std::string Int2(int number)
{
 char buffer[5]; // assume 32 bit && sizeof(int) == 4
 sprintf(buffer, "%03d", number);
 return std::string(buffer, buffer+5);
}
std::string Oct2(int number)
{
 char buffer[5]; // assume 32 bit && sizeof(int) == 4
 sprintf(buffer, "%03o", number);
 return std::string(buffer, buffer+5);
}
void main(){
	int i = 0;
	string s0 = "";
	char *sBin = "";
	unsigned c = '\0';
	system("title ASCII Table");
	//All values below 32 names
	char *NonePrint[] = {"NUL","SOH","STX","ETX","EOT","ENQ","ACK","BEL","BS",
	"HT","LF","VT","FF","CR","SO","SI","DLE","DC1","DC2","DC3","DC4","NAK","SYN",
	"ETB","CAN","EM","SUB","ESC","FS","GS","RS","US"};
	
	//Set headers
	cout << "DEC\tOCT\tHEX\tBIN\t\tSymbol\tHTML" << endl;
	do{
		//Convert decimal to bin
		sBin = Dec2Bin(i);
		if(i <=31){
			//Print special name
			s0 = NonePrint[i];
		}else{
			//Print chars
			s0 = (unsigned char)i;
		}
		cout << i << "\t" << Oct2(i).c_str() <<
			"\t" << Hex2(i).c_str() <<
			"\t" << sBin << 
			"\t" << s0.c_str() << 
			"\t" << "&#" << Int2(i).c_str() << ";" << endl;
		free(sBin);
		//INC counter
		i++;
	}while(i <=255);
	//Clear up
	s0.clear();
	//Pause console
	system("pause");
}


Other 51 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/14/2016 8:19:52 AMThomas1965

Nice and useful
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.