BitsHex::BitsToHexStr

Description

convert a vector of 1's and 0's to a hex string

Syntax

bool BitsToHexStr( const vector<byte> & vn, string & strHex )

Parameters

vn
[input]the byte vector to be compressed
strHex
[output] the string to store hex string

Return

true if succeed

Examples

EX1

void BitsHex_BitsToHexStr_ex1()
{
        vector<byte> vb;
        int nTotal = 20; //the vector will be converted to 5 Hex chars.
        srand( GetTickCount() );
        for ( int ii = 0; ii < nTotal; ii++ )
        {
                byte tb = rand() % 2;
                vb.Add(tb);
        }
        string strHex;
        BitsHex bh;
        if ( !bh.BitsToHexStr(vb, strHex) )
        {
                printf("Fail to convert vector to Hex string.");
                return;
        }
        printf("vector {%d, ", vb[0]);
        for ( ii = 1; ii < nTotal - 1; ii++ )
                printf("%d, ", vb[ii]);
        printf("%d} is convert to Hex string as follows:\n", vb[ii]);
        
        printf("%s\n", strHex);
        
        return;
}

Remark

See Also

header to Include

origin.h