⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utf_util.cpp

📁 utf8转换工具
💻 CPP
字号:
/*
* Written by Boby Thomas Pazheparampil. (March 2007.)
* Platform independent code.  (I hope so)
* Tested with Windows 2000, XP, Cygwin and Linux Debian.
*/


#include "utf_util.h"


void Hex2UTF()
{

	string sInput;

	cout<<"Hex2UTF convertor.\nEnter Hex values.\nType exit to quit\n";
	while(1)
	{
		clrscr();
		char acInput[75] = {0};
		scanf("%s",acInput);

		sInput = acInput;


		if(sInput.compare("exit") == 0)
			break;

		string sResult = convertHex2UTF(sInput);

		cout<<sResult.c_str();

	} 
}


void UTF2Hex()
{

	string sInput;


	cout<<"UTF2Hex convertor.\nEnter UTF values.\nType exit to quit\n";
	while(1)
	{
		clrscr();
		char acInput[75] = {0};
		scanf("%s",acInput);

		sInput = acInput;


		if(sInput.compare("exit") == 0)
			break;

		string sResult = convertUTF2Hex(sInput);
		if(sResult.compare("error") == 0)
			cout<<"Invalid UTF character\n";
		else if((convertHex2UTF(sResult).compare(sInput) != 0) && (convertHex2UTF(sResult).compare("0"+sInput) != 0) )
			cout<<"Invalid UTF character.\n";
		else
			cout<<sResult.c_str();

	} 
}



void displayMenu()
{
	cout<<"============================================\n";
	cout<<"Select the option\n";
	cout<<"============================================\n";
	cout<<"1. Self-Test\n";
	cout<<"2. Hex2UTF\n";
	cout<<"3. UTF2Hex\n";
	cout<<"4. ValidateUTFFile\n";
	cout<<"x. exit\n";
}

void ValidateUTFFile()
{

	cout<<"Enter File Name:";

	char acInput[MAX_PATH] = {0};
	scanf("%s",acInput);

	string sInput = acInput;

	if(sInput.length() ==0)
	{
		cout<<"Failed to open file "<<sInput.c_str()<<"\n";
		return;
	}



	if(generateUTFFileDetails(sInput))
		cout<<"Completed the validation. No invalid UTF character found.See utfdetails_"<<sInput.c_str()<<" for details\n";
	else
		cout<<"Completed the validation with errors.See utfdetails_"<<sInput.c_str()<<" for details\n";

	fflush(stdin);
	getchar();
	fflush(stdin);
}

void testAll()
{

	TESThex2binary();

	TESTbinary2hex();

	TESTconvertHex2UTF();

	TESTconvertUTF2Hex();

	TESTfindLengthUTF();

	TESTgenerateUTFFileDetails();
}

int main()
{

	cout<<"============================================\n";
	cout<<"     written by Boby Thomas                 \n";	
	cout<<"============================================\n";

	cout<<"\n\n\n";
	cout<<"============================================\n";
	cout<<"        UTF UTILITY\n";
	cout<<"============================================\n";

	bool isExit = false;


	while(1)
	{
		displayMenu();
		fflush(stdin);
		
		char acInput[30] = {0};
		scanf("%s",acInput);

		switch(acInput[0])
		{
		case '1':
			testAll();
			break;
		case '2':
			Hex2UTF();
			break;
		case '3':
			UTF2Hex();
			break;
		case '4':
			ValidateUTFFile();
			break;
		case 'x':
			isExit = true;
			break;
		}

		if(isExit)
			break;
	}

	return 1;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -