PHP入門(mén)(5) C++和PHP二進(jìn)制傳輸
C++需要實(shí)現(xiàn)PHP端的:
bin2Hex函數(shù),PHP通過(guò)這種類(lèi)型的字符串調(diào)用:pack轉(zhuǎn)換成PHP能識(shí)別的2進(jìn)制數(shù)據(jù)。 C++需要做的是實(shí)現(xiàn)一個(gè)bin2hex,其實(shí)只是把c++讀取的2進(jìn)制數(shù)據(jù)當(dāng)成byte數(shù)組,把每一位轉(zhuǎn)換成16進(jìn)制字符串就可以了。Qt中使用sprintf無(wú)法限制2位長(zhǎng)度,因此sprintf之后判斷長(zhǎng)度為8則截取最后3個(gè)字符串,包含了/0終止符#ifndef PHPCLASS_H
#define PHPCLASS_H
#include
class PhpClass : public QObject
{
Q_OBJECT
public:
explicit PhpClass(QObject *parent = 0);
//字節(jié)流轉(zhuǎn)換為十六進(jìn)制字符串的另一種實(shí)現(xiàn)方式
void Bin2Hex( const char *sSrc,QString& ret, int nSrcLen );
//十六進(jìn)制字符串轉(zhuǎn)換為字節(jié)流
void Hex2Bin( char* source, QByteArray& ret, int sourceLen);
signals:
public slots:
};
#endif // PHPCLASS_H
PhpClass::PhpClass(QObject *parent) : QObject(parent)
{
}
void PhpClass::Bin2Hex( const char *sSrc,QString& ret, int nSrcLen )
{
int i;
char szTmp[3];
for( i = 0; i < nSrcLen; i++ )
{
sprintf( szTmp, "%02X", (unsigned char) sSrc[i] );
ret.append(szTmp);
}
return ;
}
void PhpClass::Hex2Bin( char* source, QByteArray& ret, int sourceLen)
{
int i;
unsigned char highByte, lowByte;
for (i = 0; i < sourceLen; i += 2)
{
highByte = toupper(source[i]);
lowByte = toupper(source[i + 1]);
if (highByte > 0x39)
highByte -= 0x37;
else
highByte -= 0x30;
if (lowByte > 0x39)
lowByte -= 0x37;
else
lowByte -= 0x30;
ret.push_back((highByte << 4) | lowByte);
}
return ;
}
#ifndef FILECLASS_H
#define FILECLASS_H
#include
class FileClass : public QObject
{
Q_OBJECT
public:
explicit FileClass(QObject *parent = 0);
bool Read(char* file,QByteArray& ret);
bool Write(char* file,QByteArray& data);
signals:
public slots:
};
#endif // FILECLASS_H
#include "fileclass.h"
#include
#include
FileClass::FileClass(QObject *parent) : QObject(parent)
{
}
bool FileClass::Read(char*file,QByteArray& ret)
{
QFile mfile(file);
if(!mfile.open(QIODevice::ReadOnly) )
{
qDebug()<<"文件不存在";
return false;
}
qDebug()<<"文件存在";
ret = mfile.readAll();
mfile.close();
return true;
}
bool FileClass::Write(char* file,QByteArray& data)
{
QFile mfile(file);
if(!mfile.open(QIODevice::ReadWrite) )
{
qDebug()<<"文件不存在";
return false;
}
mfile.write(data);
mfile.close();
return true;
}
#include
#include
QVariant QmlClass::readimg(QString file)
{
FileClass mfile;
PhpClass php;
QByteArray ar;
QString m;
if(mfile.Read((char*)file.toStdString().c_str(),ar))
{
php.Bin2Hex(ar.data(),m,ar.size());
//2進(jìn)制流轉(zhuǎn)16進(jìn)制字符串方式1
QByteArray dates;
php.Hex2Bin((char*)m.toStdString().data(),dates,m.length());
mfile.Write("./test.jpg",dates);
}
return m;
}
function uploadimg()
{
var x = new XMLHttpRequest();
x.onreadystatechange =function()
{
if(x.readyState == 4) {
if(x.status == 200) {
console.log("The server replied with: " + x.responseText);
txt.text = x.responseText;
}
}
};
var xxx =new Object;
var d=myapp.readimg(":/1.jpg");
console.log(typeof d)
x.open("POST","http://localhost/mycode/Test/reg.php",true);
console.log(d)
//post請(qǐng)求要自己設(shè)置請(qǐng)求頭
x.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
x.send(d);
}
使用post參數(shù)和base64編碼:
function uploadimg()
{
var x = new XMLHttpRequest();
x.onreadystatechange =function()
{
if(x.readyState == 4) {
if(x.status == 200) {
console.log("The server replied with: " + x.responseText);
txt.text = x.responseText;
}
}
};
var xxx =new Object;
var d=myapp.readimg(":/main.qml");
console.log(typeof d)
x.open("POST","http://localhost/mycode/Test/reg.php",true);
console.log(Qt.btoa(d))
//post請(qǐng)求要自己設(shè)置請(qǐng)求頭
x.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
x.send("OBJ="+Qt.btoa(d));
}