当前位置:首页 > 芯闻号 > 充电吧
[导读]C++需要实现PHP端的:bin2Hex函数,PHP通过这种类型的字符串调用:pack转换成PHP能识别的2进制数据。 C++需要做的是实现一个bin2hex,其实只是把c++读取的2进制数据当成b

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 

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 

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进制流转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);
    }
使用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));
    }
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭