[freetds] Convert Hexadecimal To String

John Boia jboia at stsci.edu
Tue Sep 24 14:49:25 EDT 2013


Try this.  I found it elsewhere and augmented it.

std::string bin_to_hex( const unsigned char *input, size_t len )
{
    static const char* const lut = "0123456789ABCDEF";

    // Convert each byte to 2-char hex representation
    std::string output;
    output.reserve(2 * len);
    volatile unsigned char c;
    for (size_t i = 0; i < len; ++i)
    {
        c = input[i];
        output.push_back(lut[c >> 4]);
        output.push_back(lut[c & 15]);
    }

    // Trim excess nulls from end of binary string
    bool done = false;
    while( !done ) {
       size_t pos = output.rfind("0000000000000000");
       if ( pos != std::string::npos ) {
          output.erase(pos,16);
       } else { 
          done = true;
       }
    }

    return output;
}


On Sep 24, 2013, at 13:25 , Velichko Yuriy wrote:

> Hello!
> 
> I use method dbdata() to get data returned by dbsqlexec().
> To fetch binary data I use dbconvert() to convert data into  SYBCHAR.
> This method gives the hexadecimal string.
> 
> Can you suggest me how to convert this string to character string?
> 
> For example MySQL client has build-in method for this purpose:
> mysql_hex_string();
> 
> freetds (dblib) has something similar?
> 
> Thanks!
> 
> -- 
> Best Regards!
> _______________________________________________
> FreeTDS mailing list
> FreeTDS at lists.ibiblio.org
> http://lists.ibiblio.org/mailman/listinfo/freetds



More information about the FreeTDS mailing list