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

📄 convert.cpp

📁 十进制、八进制、二进制、十六进制的相互转换
💻 CPP
字号:

#include "stdafx.h"
#include "convert.h"

#include <comutil.h>
#include <stdio.h>
#include "objbase.h"
#include "olectl.h"
#include "assert.h"

#include "MyDataFactory.h"

ULONG    g_LockNumber = 0;
ULONG    g_MyDataNumber = 0;
HANDLE	 g_hModule;

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    g_hModule = hModule;
	return TRUE;
}

extern "C" const GUID CLSID_MyData = 
	{ 0x8556d3c8, 0x460f, 0x47cf, 
	{ 0x8f, 0x61, 0x77, 0xb8, 0x61, 0xb7, 0x64, 0x2d } };

// {E357F0E0-AE09-4a88-8F0A-1B3B419A790B}
extern "C" const GUID IID_IData = 
	{ 0xe357f0e0, 0xae09, 0x4a88, 
	{ 0x8f, 0xa, 0x1b, 0x3b, 0x41, 0x9a, 0x79, 0xb } };


extern "C" HRESULT __stdcall DllGetClassObject(const CLSID& clsid, const IID& iid, void **ppv)
{
	if (clsid == CLSID_MyData ) {
		
		MyDataFactory *pFactory = new MyDataFactory;
		
		if (pFactory == NULL) {
			return E_OUTOFMEMORY ;
		}
		
		HRESULT result = pFactory->QueryInterface(iid, ppv);

		return result;
	}
	else 
	{
		return CLASS_E_CLASSNOTAVAILABLE;
	}
}

//对象计数+锁计数,返回S_OK,同意卸载
extern "C" HRESULT __stdcall DllCanUnloadNow(void)
{
	if ((g_MyDataNumber == 0) && (g_LockNumber == 0))
		return S_OK;
	else
		return S_FALSE;
}

//
// Server registration
//
extern "C" HRESULT __stdcall DllRegisterServer()
{
	char szModule[1024];
	DWORD dwResult = ::GetModuleFileName((HMODULE)g_hModule, szModule, 1024);
	if (dwResult == 0)
		return SELFREG_E_CLASS;
	return RegisterServer(CLSID_MyData,
	                      szModule, 
						  "MyData.Object",
						  "MyData Component",
						  NULL);
}

//
// Server unregistration
//
extern "C" HRESULT __stdcall DllUnregisterServer()
{
	return UnregisterServer(CLSID_MyData,
	                        "MyData.Object",NULL);
}

BOOL SetKeyAndValue(const char* pszPath,
                    const char* szSubkey,
                    const char* szValue) ;

// Convert a CLSID into a char string.
void CLSIDtoString(const CLSID& clsid, 
                 char* szCLSID,
                 int length) ;

// Delete szKeyChild and all of its descendents.
LONG DeleteKey(HKEY hKeyParent, const char* szKeyString) ;

const int CLSID_STRING_SIZE = 39 ;
//
// Register the component in the registry.
//
HRESULT RegisterServer(const CLSID& clsid,         // Class ID
                       const char *szFileName,     // DLL module handle
                       const char* szProgID,       //   IDs
                       const char* szDescription,  // Description String
					   const char* szVerIndProgID) // optional

{
	// Convert the CLSID into a char.
	char szCLSID[CLSID_STRING_SIZE] ;
	CLSIDtoString(clsid, szCLSID, sizeof(szCLSID)) ;

	// Build the key CLSID\\{...}
	char szKey[64] ;
	strcpy(szKey, "CLSID\\") ;
	strcat(szKey, szCLSID) ;
  
	// Add the CLSID to the registry.
	SetKeyAndValue(szKey, NULL, szDescription) ;

	// Add the server filename subkey under the CLSID key.
	SetKeyAndValue(szKey, "InprocServer32", szFileName) ;

	// Add the ProgID subkey under the CLSID key.
	if (szProgID != NULL) {
		SetKeyAndValue(szKey, "ProgID", szProgID) ;
		SetKeyAndValue(szProgID, "CLSID", szCLSID) ;
	}

	if (szVerIndProgID) {
		// Add the version-independent ProgID subkey under CLSID key.
		SetKeyAndValue(szKey, "VersionIndependentProgID",
					   szVerIndProgID) ;

		// Add the version-independent ProgID subkey under HKEY_CLASSES_ROOT.
		SetKeyAndValue(szVerIndProgID, NULL, szDescription) ; 
		SetKeyAndValue(szVerIndProgID, "CLSID", szCLSID) ;
		SetKeyAndValue(szVerIndProgID, "CurVer", szProgID) ;

		// Add the versioned ProgID subkey under HKEY_CLASSES_ROOT.
		SetKeyAndValue(szProgID, NULL, szDescription) ; 
		SetKeyAndValue(szProgID, "CLSID", szCLSID) ;
	}

	return S_OK ;
}

//
// Remove the component from the registry.
//
HRESULT UnregisterServer(const CLSID& clsid,      // Class ID
                      const char* szProgID,       //   IDs
                      const char* szVerIndProgID) // Programmatic
{
	// Convert the CLSID into a char.
	char szCLSID[CLSID_STRING_SIZE] ;
	CLSIDtoString(clsid, szCLSID, sizeof(szCLSID)) ;

	// Build the key CLSID\\{...}
	char szKey[64] ;
	strcpy(szKey, "CLSID\\") ;
	strcat(szKey, szCLSID) ;

	// Delete the CLSID Key - CLSID\{...}
	LONG lResult = DeleteKey(HKEY_CLASSES_ROOT, szKey) ;

	// Delete the version-independent ProgID Key.
	if (szVerIndProgID != NULL)
		lResult = DeleteKey(HKEY_CLASSES_ROOT, szVerIndProgID) ;

	// Delete the ProgID key.
	if (szProgID != NULL)
		lResult = DeleteKey(HKEY_CLASSES_ROOT, szProgID) ;

	return S_OK ;
}

void CLSIDtoString(const CLSID& clsid,
                 char* szCLSID,
                 int length)
{
	assert(length >= CLSID_STRING_SIZE) ;
	// Get CLSID
	LPOLESTR wszCLSID = NULL ;
	HRESULT hr = StringFromCLSID(clsid, &wszCLSID) ;
	assert(SUCCEEDED(hr)) ;

	// Covert from wide characters to non-wide.
	wcstombs(szCLSID, wszCLSID, length) ;

	// Free memory.
	CoTaskMemFree(wszCLSID) ;
}

LONG DeleteKey(HKEY hKeyParent,           // Parent of key to delete
               const char* lpszKeyChild)  // Key to delete
{
	// Open the child.
	HKEY hKeyChild ;
	LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyChild, 0,
	                         KEY_ALL_ACCESS, &hKeyChild) ;
	if (lRes != ERROR_SUCCESS)
	{
		return lRes ;
	}

	// Enumerate all of the decendents of this child.
	FILETIME time ;
	char szBuffer[256] ;
	DWORD dwSize = 256 ;
	while (RegEnumKeyEx(hKeyChild, 0, szBuffer, &dwSize, NULL,
	                    NULL, NULL, &time) == S_OK)
	{
		// Delete the decendents of this child.
		lRes = DeleteKey(hKeyChild, szBuffer) ;
		if (lRes != ERROR_SUCCESS)
		{
			// Cleanup before exiting.
			RegCloseKey(hKeyChild) ;
			return lRes;
		}
		dwSize = 256 ;
	}

	// Close the child.
	RegCloseKey(hKeyChild) ;

	// Delete this child.
	return RegDeleteKey(hKeyParent, lpszKeyChild) ;
}

//
// Create a key and set its value.
//
BOOL SetKeyAndValue(const char* szKey,
                    const char* szSubkey,
                    const char* szValue)
{
	HKEY hKey;
	char szKeyBuf[1024] ;

	// Copy keyname into buffer.
	strcpy(szKeyBuf, szKey) ;

	// Add subkey name to buffer.
	if (szSubkey != NULL)
	{
		strcat(szKeyBuf, "\\") ;
		strcat(szKeyBuf, szSubkey ) ;
	}

	// Create and open key and subkey.
	long lResult = RegCreateKeyEx(HKEY_CLASSES_ROOT ,
	                              szKeyBuf, 
	                              0, NULL, REG_OPTION_NON_VOLATILE,
	                              KEY_ALL_ACCESS, NULL, 
	                              &hKey, NULL) ;
	if (lResult != ERROR_SUCCESS)
	{
		return FALSE ;
	}

	// Set the Value.
	if (szValue != NULL)
	{
		RegSetValueEx(hKey, NULL, 0, REG_SZ, 
		              (BYTE *)szValue, 
		              strlen(szValue)+1) ;
	}

	RegCloseKey(hKey) ;
	return TRUE ;
}


MyData::MyData()
{
	m_Ref=0;
	m_realdata=NULL;
	m_tendata=NULL;
	g_MyDataNumber++;
//	two={0,1};
//	eight={0,1,2,3,4,5,6,7};
}

MyData::~MyData()
{

}

bool MyData::findchar(char ch,char* string)
{
	while (*string!='\0')
	{
		if (*string==ch)
			return true;
		else *string++;
	}
	
	return false;
}

void MyData::ParseDecimal(char* p)
{
	char temp[500];
	int i=0;
	while (*p!='\0')
	{
		if (*p=='.')
		{
			*p++;
			m_decimaldata=p;
			temp[i]='\0';
			break;
		}			
		else 
		{
			temp[i++]=*p;
			*p++;
		}		
	}
	m_intdata=new char[strlen(temp)+1];
	strcpy(m_intdata,temp);
}

int MyData::ParseExp(char* p)
{
	char string[20];//记录进制数的数组
	int i=0;
	int j=0;
	
	if (!findchar('x',p))
		return 0;
	if (isalnum(*p))
	{
		while (*p!='\0')
		{	
			string[i++]=*p;
			*p++;
			if (*p=='x')
			{
				*p++;
				break;
			}
				
		}
	}
	else return 0;
	
	string[i]='\0';
	m_realdata=p; //除去前缀,存放实际的数
	
	DecimalFlag=0;

	if (findchar('.',m_realdata))
	{
		DecimalFlag=1;
		ParseDecimal(m_realdata);
	}else m_intdata=p;

	char* exp=new char[strlen(string)+1];
	strcpy(exp,string);

	char* two="2";
	char* eight="8";
	char* sixteen="16";
	char* ten="10";
	if (strcmp(exp,sixteen)==0)
		inputexp=16;
	else if (strcmp(exp,eight)==0)
		inputexp=8;
	else if (strcmp(exp,two)==0)
		inputexp=2;
	else if (strcmp(exp,ten)==0)
		inputexp=10;
	else inputexp=0;

	char* q=m_realdata;
	while (*q!='\0')
	{
		if ((inputexp==2)&&((*q=='0')||(*q=='1')||(*q=='.')))
		{}			
		else if ((inputexp==8)&&(((*q>'0')&&(*q<'8'))||(*q=='.')))
		{}	
		else if ((inputexp==10)&&((isdigit(*q))||(*q=='.')))
		{}	
		else if ((inputexp==16)&&((isxdigit(*q))||(*q=='.')))
		{}
		else inputexp=0;
		*q++;
	}
	return inputexp;
}

void MyData::Toten(int exp)
{
	int n1=strlen(m_intdata);
	m_inttendata=0;
	m_decimaltendata=0;
	int temp;
	int i,j;

	for (i=0;i<n1;i++)
	{
		if ((exp==16)&&(isxdigit(m_intdata[i])))
		{
			switch (m_intdata[i])
			{
			case 'a':temp=10;break;
			case 'A':temp=10;break;
			case 'b':temp=11;break;
			case 'B':temp=11;break;
			case 'c':temp=12;break;
			case 'C':temp=12;break;
			case 'd':temp=13;break;
			case 'D':temp=13;break;
			case 'e':temp=14;break;
			case 'E':temp=14;break;
			case 'F':temp=15;break;
			case 'f':temp=15;break;
			default:temp=m_intdata[i]-'0';
			}
			m_inttendata=m_inttendata+pow(exp,(n1-1-i))*(temp);	
		}
		else m_inttendata=m_inttendata+pow(exp,(n1-1-i))*(m_intdata[i]-'0');	
	}//整数部分转换完毕

	if (DecimalFlag)
	{
		int n2=strlen(m_decimaldata);
		for (j=0;j<n2;j++)
		{
			if ((exp==16)&&(isxdigit(m_decimaldata[j])))
			{
				switch (m_decimaldata[j])
				{
				case 'a':temp=10;break;
				case 'A':temp=10;break;
				case 'b':temp=11;break;
				case 'B':temp=11;break;
				case 'c':temp=12;break;
				case 'C':temp=12;break;
				case 'd':temp=13;break;
				case 'D':temp=13;break;
				case 'e':temp=14;break;
				case 'E':temp=14;break;
				case 'F':temp=15;break;
				case 'f':temp=15;break;
				default:temp=m_decimaldata[j]-'0';
				}

				m_decimaltendata=m_decimaltendata+pow(exp,(-j-1))*(temp);	
			}
			else m_decimaltendata=m_decimaltendata+pow(exp,(-j-1))*(m_decimaldata[j]-'0');	
		}
	}	
}

char* MyData::convertdata(int exp)
{
	char temp1[500];
	char temp2[500];
	double temp;
	int i=0;
	int val=m_inttendata;
		
//	val=val/exp; //商
//	temp=val%exp;//余数
	while (val>=exp) 
	{
		temp=val%exp;
		if (exp==16)
		{
			switch (int(temp))
			{
			case 10:temp1[i]='A';break;
			case 11:temp1[i]='B';break;
			case 12:temp1[i]='C';break;
			case 13:temp1[i]='D';break;
			case 14:temp1[i]='E';break;
			case 15:temp1[i]='F';break;
			default:temp1[i]=temp;
			}
		}else temp1[i]=temp;
		val=val/exp;
		i++;
	}	

	if (exp==16)
	{
		switch (val)
		{
		case 10:temp1[i]='A';break;
		case 11:temp1[i]='B';break;
		case 12:temp1[i]='C';break;
		case 13:temp1[i]='D';break;
		case 14:temp1[i]='E';break;
		case 15:temp1[i]='F';break;
		default:temp1[i]=val;
		}
		i++;
	}else
		temp1[i++]=val;
	temp1[i]='\0';

	for(int j=0;j<i;j++)
	{
		if ((temp1[i-j-1]>='A')&&(temp1[i-1-j]<='F')&&exp==16)
			temp2[j]=temp1[i-1-j];
		else temp2[j]=temp1[i-1-j]+48;
				
	}
	temp2[j]='\0';	
	char* result=new char[strlen(temp2)+1];
	strcpy(result,temp2);
	
	double val2=m_decimaltendata;
	if (DecimalFlag) //将小数部分转换为相应进制的小数
	{
		double* temp3=new double[500];
		double temp4;
		char temp5[500];
		int j=0;

		temp=val2*exp;
		temp4=modf(temp,temp3);
		do 
		{
			switch ((int)(*temp3))
			{
			case 10:temp5[j]='A';break;
			case 11:temp5[j]='B';break;
			case 12:temp5[j]='C';break;
			case 13:temp5[j]='D';break;
			case 14:temp5[j]='E';break;
			case 15:temp5[j]='F';break;
			default:temp5[j]=*temp3+48;
			}
			temp=temp4*exp;
			temp4=modf(temp,temp3);//temp4保存temp的小数部分,整数部分在temp3指向的单元处
			j++;
		}while (temp4!=0);
		
		temp5[j]=*temp3+48;
		temp5[++j]='\0';
		char* result_deci=new char[strlen(temp5)+1];
		strcpy(result_deci,temp5);
		strcat(result,".");
		strcat(result,result_deci);
	}

	return result;
}





HRESULT  MyData::QueryInterface(const IID& iid, void **ppv)
{
	if ( iid == IID_IUnknown )
	{
		*ppv = (IData *) this ;
				((IData *)(*ppv))->AddRef() ;
	} else if ( iid == IID_IData ) 
	{
		*ppv = (IData *) this ;
				((IData *)(*ppv))->AddRef() ;
	} 
	else
	{
		*ppv = NULL;
		return E_NOINTERFACE ;
	}
	return S_OK;
}

ULONG  MyData::AddRef()
{
	m_Ref ++;
	return  (ULONG) m_Ref;
}

ULONG	MyData::Release()
{
	m_Ref --;
	if (m_Ref == 0 ) {
		g_MyDataNumber--;
		delete this;
		return 0;
	}
	return  (ULONG) m_Ref;
}


⌨️ 快捷键说明

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