您的位置:首页 > 编程语言 > C语言/C++

PHP入门(5) C++和PHP二进制传输

2016-07-06 18:33 423 查看
C++需要实现PHP端的:

bin2Hex

函数,PHP通过这种类型的字符串调用:

pack

转换成PHP能识别的2进制数据。

C++需要做的是实现一个bin2hex,其实只是把c++读取的2进制数据当成byte数组,把每一位转换成16进制字符串就可以了。Qt中使用sprintf无法限制2位长度,因此sprintf之后判断长度为8则截取最后3个字符串,包含了/0终止符

#ifndef PHPCLASS_H
#define PHPCLASS_H

#include <QObject>

class PhpClass : public QObject
{
Q_OBJECT
public:
explicit PhpClass(QObject *parent = 0);

//字节流转换为十六进制字符串的另一种实现方式
void Bin2Hex( const char *sSrc,QString& ret, int nSrcLen );

//十六进制字符串转换为字节流
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 <QObject>

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<QFile>
#include<QDebug>
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<phpclass.h>
#include<fileclass.h>
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进制流转16进制字符串方式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请求要自己设置请求头
x.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
x.send(d);
}


<?php
$filename="teststream.jpg";//要生成的图片名字
$data_buff=file_get_contents("php://input");
echo  $data_buff;
$jpg = pack("H*",$data_buff);//得到post过来的二进制原始数据
$file = fopen("pic/".$filename,"wb");//打开文件准备写入
fwrite($file,$jpg);//写入
fclose($file);//关闭
?>


使用post参数和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请求要自己设置请求头
x.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
x.send("OBJ="+Qt.btoa(d));
}


<?php
$filename = "teststream.jpg"; // 要生成的图片名字
$data_buff = base64_decode($_POST['OBJ']);
$jpg = pack("H*", $data_buff); // 得到post过来的二进制原始数据
$file = fopen("pic/" . $filename, "wb"); // 打开文件准备写入
fwrite($file, $jpg); // 写入
fclose($file); // 关闭
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: