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

📄 pdu.cpp

📁 用一个开源码ASCII ENCODE,DECODE,来发送ASCII短信
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <iostream>#include <string>#include <sstream>#include <stdlib.h>#include "pdu.h"using namespace std;using std::string;/** * string semiOctetToString ( string ) * --------------------------------------------------------------- * Converts a string representing semi-octets to an octet * string. *  * Example: * string semiOctetString = "123456789A"; * string MyString = semiOctetToString(semiOctetString); * cout << MyString << endl; *  * ( will result in 21436587A9 , being printed ) *  * */string pdu::semiOctetToString (string semiOctet) {	unsigned int i;	string OctetString;		for (i = 0; i < ( semiOctet.length() / 2 ); i++) {		OctetString += semiOctet.substr(i*2 + 1, 1);		OctetString += semiOctet.substr(i*2, 1);	}	return OctetString;}/** * string stringToSemiOctet ( string ) * --------------------------------------------------------------- * Converts a string to a semi-octet string, and if necessary * pad the original string with 'F' to make it compatible. *  * Example: * string OctetString = "123456789"; * string MyString = stringToSemiOctet(OctetString); * cout << MyString << endl; *  * ( will result in 21436587F9 , being printed ) *  * */string pdu::stringToSemiOctet (string StringData) {	if ( StringData.length() % 2 != 0 ) {		StringData += "F";	}		return semiOctetToString(StringData);}/** * int hexStringToInt ( string ) * --------------------------------------------------------------- * Converts a string representing a hexadecimal value to a an * integer *  * Example: * string HexString = "0A"; * int MyInt = hexStringToInt(HexString); * cout << MyInt << endl; *  * ( will result in 10 , being printed ) *  * */int pdu::hexStringToInt (string hexString) {	unsigned int i;	int charvalue;	int returnvalue = 0;		for (i = 0; i < hexString.length(); i++) {		if ( hexString[i] >= 'a' && hexString[i] <= 'f' ) {			charvalue = (int) hexString[i] - 87;		}		if ( hexString[i] >= 'A' && hexString[i] <= 'F' ) {			charvalue = (int) hexString[i] - 55;		}		if ( hexString[i] >= '0' && hexString[i] <= '9' ) {			charvalue = (int) hexString[i] - 48;		}		returnvalue += charvalue * ( pow(16, (int)(hexString.length() - (i + 1)) ));	}	return returnvalue;}/** * string intToHexString ( int ) * --------------------------------------------------------------- * Converts an integer to a string representing the hexadecimal value * of the integer. The result will be have a 0 prepended to make the * string length even. *  * Example: * int MyInt = 10; * string MyHex = intToHexString(MyInt); * cout << MyHex << endl; *  * ( will result in "0A" , being printed ) *  * */string pdu::intToHexString (int integer) {	string hexString = "";	/*	for (int i=2*sizeof(int) - 1; i>=0; i--) {		if ( ( (integer >> i*4) & 0xF ) != 0 )  {			hexString += "0123456789ABCDEF"[((integer >> i*4) & 0xF)];					}	}	if ( hexString.length() % 2 != 0 ){		hexString = "0" + hexString;	}	*/	//Changed Conversion from intToHex from above to below...	char buf[3];	sprintf(buf,"%X",integer);	hexString = (string)buf;	//Prepend a 0 when not even	if ( hexString.length() % 2 != 0 ){		hexString = "0" + hexString;	}		return hexString;}/** * int binStringToInt ( string ) * --------------------------------------------------------------- * Converts a string representing a binary value to a an * integer *  * Example: * string BinString = "01111"; * int MyInt = binStringToInt(BinString); * cout << MyInt << endl; *  * ( will result in 15 , being printed ) *  * */int pdu::binStringToInt (string binString) {		unsigned int pos;	int integerReturn = 0;	int base = 2;		for (pos = 0; pos < binString.length(); pos++) {		if ( binString.substr(pos, 1) == "1" ) {			integerReturn += pow(base, (binString.length() - (pos + 1)) );		}	}	return integerReturn;}/** * string intToBinString ( int ) * --------------------------------------------------------------- * Converts an integer to a string representing the binary value * of the integer. padded to make the string a multiple of 8 characters long *  * Example: * int MyInt = 16; * string MyBin = intToBinString(MyInt); * cout << MyBin << endl; *  * ( will result in "00010000" , being printed ) *  * */string pdu::intToBinString (int integer) {	string stringReturn;		if (integer <= 0) {		return "0";	}		while ( integer > 0 ) {		if ( integer % 2 == 1 ) {			stringReturn = "1" + stringReturn;			--integer;		} else {			stringReturn = "0" + stringReturn;		}		integer = integer / 2;	}	//Changed 8 to 7 because the characters are in septets	while ( stringReturn.length() % 7 != 0 ) {		stringReturn = "0" + stringReturn;	}	return stringReturn;}/** * int pow ( int, int ) * --------------------------------------------------------------- * Calculates int 1 to the power of int 2. *  * Example: * int MyInt1 = 10; * int MyInt2 = 2; * int MyResult = pow(MyInt1, MyInt2); * cout << MyResult << endl; *  * ( will result in 100 , being printed ) *  * */int pdu::pow(int n, int i){   if (i == 0)      return 1;   else if (i == 1)      return n;   else {      int partial = pow(n, i / 2);      if (i % 2 == 0)         return partial * partial;      else         return partial * partial * n;   }}/** * int parsePDU ( string ) * --------------------------------------------------------------- * Parses a string representing a SMS in PDU mode *  * Example: * pdu MyPDU; * int MyResult = MyPDU.parsePDU("ADGHJ....5F"); * cout << MyResult << endl; *  * ( will result in 1 , being printed if ok, -1 on failure ) *  * */void pdu::parsePDU(string pduString) {	int stringLocation = 0;				this->smsc_length = hexStringToInt(pduString.substr(stringLocation, 2));	stringLocation += 2;		if ( this->smsc_length != 0 ) {					this->smsc_type_of_address = hexStringToInt(pduString.substr(stringLocation, 2));		stringLocation += 2;				this->smsc_number = semiOctetToString( pduString.substr(stringLocation, ( this->smsc_length -1 ) * 2 ) );		if ( this->smsc_number[this->smsc_number.length() - 1 ] == 'F' ) {			this->smsc_number = this->smsc_number.substr(0, this->smsc_number.length() - 1);		}		stringLocation += ( this->smsc_length -1 ) * 2;	} else {				this->smsc_type_of_address = 0;		this->smsc_number = "0";	}		this->sms_deliver = hexStringToInt(pduString.substr(stringLocation, 2));	stringLocation += 2;		if (this->sms_deliver & 0x03) {				parsePDU_send(pduString.substr(stringLocation, (pduString.length() - stringLocation  )));	} else {		parsePDU_receive(pduString.substr(stringLocation, (pduString.length() - stringLocation )));	}}/** * void parsePDU_receive ( string ) * --------------------------------------------------------------- * Parses a string representing a SMS in PDU mode *  * Example: * pdu MyPDU; * int MyResult = MyPDU.parsePDU("ADGHJ....5F"); * cout << MyResult << endl; *  * ( will result in 1 , being printed if ok, -1 on failure ) *  * */void pdu::parsePDU_receive(string pduString) {	int stringLocation = 0;			// SENDER Lenght	this->sender_length = hexStringToInt(pduString.substr(stringLocation, 2));	stringLocation += 2;	if ( this->sender_length != 0 ) {		// SENDER Type of address		this->sender_type_of_address = hexStringToInt(pduString.substr(stringLocation, 2));		stringLocation += 2;				// SENDER Number		int evenSenderLength = this->sender_length + ( this->sender_length % 2 );		this->sender_number = semiOctetToString( pduString.substr(stringLocation, evenSenderLength ));		this->sender_number = this->sender_number.substr(0, this->sender_length);		stringLocation += evenSenderLength;	}	// PROTOCOL Identifier	this->tp_pid = hexStringToInt(pduString.substr(stringLocation, 2));	stringLocation += 2;	//DATA Coding scheme tp_dcs

⌨️ 快捷键说明

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