参考:http://www.cppblog.com/Khan/archive/2009/05/08/82240.html
#include <QCoreApplication>
#include <qbytearray.h>
#include <vector>
#include <qdebug.h>
#include <math.h>
QByteArray UInt8ToBin(uint8_t num)
{
QByteArray byteArr;
while (num != 0)
{
byteArr.push_back(num % 2);
num /= 2;
}
QByteArray retArr;
for (int i = 7; i >= 0; i--)
{
if(i<byteArr.size())
{
retArr.append(byteArr.at(i));
}
else
{
retArr.append((char)0x00);
}
}
return retArr;
}
uint8_t Bin2Uint8(const QByteArray &data)
{
uint8_t num = 0;
int n = data.size();
for(size_t i=0; i<data.size(); i++)
{
/* num += data.at(i);
num*=2;*/
num += data.at(i)*pow(2,n-i-1);
}
return num;
}
QString PrintBin(const QByteArray &data)
{
QString str;
for(int i=0; i<data.size(); i++)
{
str += data.at(i) + '0';
}
return str;
}
int sevenbitdefault[] = {'@', '£', '$', '¥', 'è', 'é', 'ù', 'ì', 'ò', 'Ç', '\n', 'Ø', 'ø', '\r','Å', 'å','\u0394', '_', '\u03a6', '\u0393', '\u039b', '\u03a9', '\u03a0','\u03a8', '\u03a3', '\u0398', '\u039e','€', 'Æ', 'æ', 'ß', 'É', ' ', '!', '"', '#', '¤', '%', '&', '\'', '(', ')','*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7','8', '9', ':', ';', '<', '=', '>', '?', '¡', 'A', 'B', 'C', 'D', 'E','F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S','T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'Ä', 'Ö', 'Ñ', 'Ü', '§', '¿', 'a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o','p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'ä', 'ö', 'ñ','ü', 'à '};
/*static const uint16_t sevenbitdefault[128] =
{
0x0040, 0x00a3, 0x0024, 0x00a5, 0x00e8, 0x00e9, 0x00f9, 0x00ec, 0x00f2,0x00c7, 0x000a, 0x00d8, 0x00f8, 0x000d, 0x00c5, 0x00e5,
0x0394, 0x005f, 0x03a6, 0x0393, 0x039b, 0x03a9, 0x03a0, 0x03a8, 0x03a3,0x0398, 0x039e, 0x0000, 0x00c6, 0x00e6, 0x00df, 0x00c9,
L' ', L'!', L'"', L'#', 0x00a4, L'%', L'&', 0x0027, L'(', L')', L'*', L'+', L',', L'-', L'.', L'/',
L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7', L'8', L'9', L':', L';', L'<', L'=', L'>', L'?',
0x00A1, L'A', L'B', L'C', L'D', L'E', L'F', L'G', L'H', L'I', L'J', L'K', L'L', L'M', L'N', L'O',
L'P', L'Q', L'R', L'S', L'T', L'U', L'V', L'W', L'X', L'Y', L'Z', 0x00C4, 0x00d6, 0x00d1, 0x00dc, 0x00a7,
0x00bf, L'a', L'b', L'c', L'd', L'e', L'f', L'g', L'h', L'i', L'j', L'k', L'l', L'm', L'n', L'o',
L'p', L'q', L'r', L's', L't', L'u', L'v', L'w', L'x', L'y', L'z', 0x00e4, 0x00f6, 0x00f1, 0x00fc, 0x00e0
};*/
static uint16_t ext7bit[102];
uint16_t GetExt7bit(int num)
{
/*
* 见:http://www.cppblog.com/Khan/archive/2009/05/08/82240.html
GSM 7Bit Dec Hex CharacterISO-8859-1
27 0x1B
27 10 0x1B0A
27 20 0x1B14
27 40 0x1B28
27 41 0x1B29
*/
memset(ext7bit,0,sizeof(ext7bit));
ext7bit[10] = 0x000C;
ext7bit[20] = L'^'; //0x005E; // ^
ext7bit[40] = L'{'; //0x007B; // {
ext7bit[41] = L'}'; // 0x007D; // }
ext7bit[47] = L'\\'; //0x005C; // '\'
ext7bit[60] = L'['; //0x005B; // [
ext7bit[61] = L'~'; // 0x007E; // ~
ext7bit[62] = L']'; //0x005D; // ]
ext7bit[64] = L'|'; // 0x007C; // |
ext7bit[101] = L'€';
if(num < 102)
{
return ext7bit[num];
}
return 0;
}
QByteArray Decode7Bit(const QByteArray &str,int trueLength)
{
std::vector<QByteArray> octetArrayVec;
std::vector<QByteArray> restArrayVec;
std::vector<QByteArray> septetsArrayVec;
int count = 0;
bool bok = false;
QByteArray byteStr;
#if 0
for(int i=0; i<str.size(); i+=2)
{
int num = str.mid(i,2).toInt(&bok,16); // 16进制数转成int
byteStr += QString("%1").arg(num,8,2,QLatin1Char('0')).toLocal8Bit(); // 转成2进制字节
}
#else
for(int i=0; i<str.size(); i++)
{
uint8_t num = str[i];
byteStr += UInt8ToBin(num);
//printf("num:%d ",num);
}
#endif
int leftByte=1;
for(int i=0; i<byteStr.size(); i+=8)
{
QByteArray arr = byteStr.mid(i,8);
octetArrayVec.push_back(arr);
restArrayVec.push_back(arr.mid(0,leftByte%8));
septetsArrayVec.push_back(arr.mid(leftByte%8));
leftByte++;
if(leftByte == 8)
{
leftByte = 1;
}
}
for(int i=0; i<octetArrayVec.size();i++)
{
uint8_t num = str[i];
QString str = QString("%1: %2 %3 %4 ").arg(num,0,16,QLatin1Char('0'))
.arg(PrintBin(octetArrayVec[i])).arg(PrintBin(septetsArrayVec[i])).arg(PrintBin(restArrayVec[i]));
qDebug("%s",qPrintable(str));
}
int matchcount = 0;
QByteArray smsStr;
size_t i=0;
// put the right parts of the array's together to make the sectets
for(; i<restArrayVec.size(); i++)
{
if(i%7 == 0)
{
if(i!=0)
{
// int num = restArrayVec[i-1].toInt(&bok,2);
smsStr += sevenbitdefault[Bin2Uint8(restArrayVec[i-1])];
}
int num = Bin2Uint8(septetsArrayVec[i]);
if(num == 27) // 处理扩展字符
{
i++;
num = Bin2Uint8(septetsArrayVec[i]+restArrayVec[i-1]);
smsStr += GetExt7bit(num);
matchcount ++;
}
else
{
smsStr += sevenbitdefault[num];
}
matchcount ++;
}
else
{
//int num = (septetsArrayVec[i]+restArrayVec[i-1]).toInt(&bok,2);
int num = Bin2Uint8(septetsArrayVec[i]+restArrayVec[i-1]);
if(num == 27)
{
i++;
num = Bin2Uint8(septetsArrayVec[i]+restArrayVec[i-1]);
smsStr += GetExt7bit(num);
matchcount ++;
}
else
{
smsStr += sevenbitdefault[num];
}
matchcount ++;
}
}
if(matchcount != trueLength)
{
//int num = restArrayVec[i-1].toInt(&bok,2);
smsStr += sevenbitdefault[Bin2Uint8(restArrayVec[i-1])];
}
return smsStr;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
#if 0
// B118201603
QByteArray data("B118201603");
QByteArray data1("B11800108B01");
QByteArray data2("329940209301");
/*data.push_back(0xB1);
data.push_back(0x18);
data.push_back(0x20);
data.push_back(0x16);
data.push_back(0x03);*/
QByteArray str = Decode7Bit(data,5);
qDebug("%s %s %s",Decode7Bit(data,5).data(), Decode7Bit(data1,6).data(),Decode7Bit(data2,6).data());
#endif
int size = sizeof(sevenbitdefault)/sizeof(sevenbitdefault[0]);
qDebug("size:%d",size);
/*QByteArray data;
data.push_back(0xB1);
data.push_back(0x18);
data.push_back(0x20);
data.push_back(0x16);
data.push_back(0x03);
QByteArray str = Decode7Bit(data,5);
qDebug("%s",str.data());
// 34 DA A6 B7 E9 D1 68
uint8_t data1[] = {0x34, 0xDA, 0xA6,0xB7,0xE9,0xD1,0x68};
QByteArray str1 = Decode7Bit(QByteArray((char*)data1,sizeof(data1)),8);
qDebug("%s",str1.data());*/
// 9B 5E 6D D3 AB 01
uint8_t data2[] = {0x9B, 0x5E, 0x6D,0xD3,0xAB,0x01};
QByteArray str2 = Decode7Bit(QByteArray((char*)data2,sizeof(data2)),6);
qDebug("%s",str2.data());
return a.exec();
}