工作中遇到的数制转换问题

D0DEB8C4BBB7BEB3B1E4C1BFCEC4BCFE2E70726F66696C65A1A3D4DACEC4BCFEBFAACABCB5D8B7BDBCD3C8EBC8E7CFC2B2BFB7D6A1A30D0A2323232323232323232323232323BDE2BEF668707578D6D5B6CBC9CFB2BBC4DCCAE4C8EB40D7D6B7FB232323232323626567696E2323232323230D0A232044656661756C7420286578616D706C65206F66292073757065722D757365722773202E70726F66696C652066696C65200D0A2320446F206E6F742070757420222E2220696E20504154483B206974206973206120706F74656E7469616C207365637572697479206272656163682E0D0A2320446F206E6F742070757420222F7573722F6C6F63616C2F62696E2220696E20504154483B206974206973206120706F74656E7469616C207365637572697479206272656163682E0D0A23204578616D706C6520617373756D6573202F686F6D652F726F6F74206578697374732E0D0A2020202020202020736574202B750D0A23204265207375726520746861742056554520646F6573206E6F7420696E766F6B652074747920636F6D6D616E64730D0A2020206966205B202120222456554522205D3B207468656E0D0A202020232053657420757020746865207465726D696E616C3A0D0A20202020202020206966205B2022245445524D22203D202222205D0D0A20202020202020207468656E0D0A202020202020202020202020202020206576616C20602074736574202D73202D51202D6D20273A3F68702720600D0A2020202020202020656C73650D0A202020202020202020202020202020206576616C20602074736574202D73202D5120600D0A202020202020202066690D0A20202020202020207374747920657261736520225E4822206B696C6C20225E552220696E747220225E432220656F6620225E44220D0A20202020202020207374747920687570636C2069786F6E2069786F66660D0A2020202020202020746162730D0A20202020202020206563686F0D0A20202020202020206563686F202256616C7565206F66205445524D20686173206265656E2073657420746F205C22245445524D5C222E20220D0A20202020202020206578706F7274205445524D200D0A2020202020202020454449544F523D76690D0A20202020202020206578706F727420454449544F520D0A2020206669202020202020202020202320696620215655452020200D0A2320536574207570207368656C6C20656E7669726F6E6D656E743A0D0A2020202020202020736574202D752020202020202020202020202020202020202020202020202020202020202020202023206572726F7220696620756E646566696E6564207661726961626C652E0D0A20202020202020207472617020226563686F20276C6F676F757420726F6F74272220302020202020202020202020202023207768617420746F20646F206F6E20657869742E0D0A0D0A

你看懂上面的一堆字符串了吗?

今天要求从数据库里把发送过的业务激活指令提出来。最后发现写在表里的指令记录是如上面那些的字符串!明显这是数据的十六进制的表示法。可是怎么写成了字符串呢!直接存二进制不好吗?

唉!不管了搞个程序转换一下吧!

////代码开始

#include <cstdlib>
#include <iostream>
#include "math.h"
#include "stdio.h"

using namespace std;
long int hexTodec(const char *str , int length )
{
    long int liRestul = 0 ;
    int iTrans = 0;
    for(int i = length-1 ,j = 0 ; i >=0 ; i--,j++)
    {
        //cout<<str[i]<<"  ";
        if(96<str[i]&&str[i]<103){iTrans = (int)(str[i]-87);}//a~f
        else{
            if(64<str[i]&&str[i]<71){iTrans = (int)(str[i]-55);}//A~F
            else{
                if(47<str[i]&&str[i]<58){iTrans = (int)(str[i]-48);}//0~9
                else {return 0;}
                }
            }

        //cout<<iTrans<<"  ";
        liRestul=liRestul + iTrans * (long)pow(16,j);//hex to dec
        }
        //cout<<liRestul<<"  ";
        return liRestul;
    }

string* decode(const string *str,string *res)
{
    int iLength = str->length();
    int iAsiic = 0;
    char p;
    string restul;
    for (int i = 0 ; i< iLength ; i=i+2)
    {
        string sHex  = str->substr(i,2);
        //cout<<sHex<<"   ";
        p = hexTodec(sHex.c_str(),sHex.length());
        //std::cout<<p;
        res->append(1,p);      
        }
        //cout<<*res<<endl;
    return res;
    }
int main(int argc, char *argv[])
{
    if(argc < 2)
    {
        cout<<"please input 2byte HEX string! "<<endl;
        return 0;
        }
    //cout<<argv[1]<<" "<<argv[2]<<endl;
    //string str = "D0DEB8C4BBB7BEB3B1E4C1BFCEC4BCFE2E70726F66696C65A1" ;
    string str(argv[1]);
    string destr = "";
    decode(&str,&destr);
    //cout<<hexTodec(argv[1],str.length())<<endl;
    cout<<destr<<endl;
    return 0;
}

////代码结束

在linux下gcc编译:

#gcc -lstdc++ -o hexdecode hexdecode.cpp

运行结果:

#hexdecode D0DEB8C4BBB7BEB3B1E4C1BFCEC4BCFE2E70726F66696C65A1
修改环境变量文件.profile

自己试试吧!

上面的一堆字符串是我前面的一篇日志。

现在想想用这种法子存在数据库中可能是为了防止好多特殊字符在组装sql时要转义的问题吧!

你可能感兴趣的:(工作中遇到的数制转换问题)