www.pudn.com > CipherSystem.rar > GlobalFunctions.cpp


#include "stdafx.h" 
#include "GlobalFunctions.h" 
 
void CharToBinary(const string& charStr, string& binaryStr) 
{ 
	binaryStr.erase(); 
	int i; 
	for (i=0; i<(int)charStr.length(); i++) 
	{ 
		char temp = charStr[i]; 
 
		int j; 
		for (j=0; j<8; j++) 
		{ 
			if (temp & (0x80 >> j)) 
			{ 
				int test = 1; 
				binaryStr += test; 
			} 
			else  
			{ 
				int test = 0; 
				binaryStr += test; 
			} 
		} 
	} 
	return; 
} 
 
void BinaryToChar(const string& binaryStr, string& charStr) 
{ 
	charStr.erase(); 
	int mulArray[] = {128, 64, 32, 16, 8 ,4, 2, 1}; 
	int mulArrayDisplace = 0; 
	int temp = 0; 
	int i; 
	for (i=0; i<(int)binaryStr.length(); i++) 
	{ 
		temp += mulArray[mulArrayDisplace++] * (int)binaryStr[i]; 
		if (mulArrayDisplace == 8) 
		{ 
			charStr += temp; 
			temp = 0; 
			mulArrayDisplace = 0; 
		} 
	} 
} 
 
void CharToBinary(const string& str, binary_stream& binaryStream) 
{ 
	binaryStream.clear(); 
	int i; 
	for (i=0; i<(int)str.length(); i++) 
	{ 
		int j; 
		for (j=0; j<8; j++) 
		{ 
			if (str[i] & (0x80 >> j)) binaryStream.push_back(true); 
			else binaryStream.push_back(false); 
		} 
	} 
	return; 
} 
 
void BinaryToChar(const binary_stream& binaryStream, string& str) 
{ 
	str.erase(); 
	int mulArray[] = {128, 64, 32, 16, 8 ,4, 2, 1}; 
	int mulArrayDisplace = 0; 
	int temp = 0; 
	int i; 
	for (i=0; i<(int)binaryStream.size(); i++) 
	{ 
		temp += mulArray[mulArrayDisplace++] * (int)binaryStream[i]; 
		if (mulArrayDisplace == 8) 
		{ 
			str += char(temp); 
			temp = 0; 
			mulArrayDisplace = 0; 
		} 
	}	 
} 
void StringToCString(const string& str, CString& cstr) 
{ 
	cstr.Empty(); 
	int i; 
	for (i=0; i<(int)str.length(); i++) cstr += str[i]; 
} 
 
void CStringToString(const CString& cstr, string& str) 
{ 
	str.erase(); 
	int i; 
	for (i=0; i<(int)cstr.GetLength(); i++) str += cstr[i]; 
}