Re: Convert Hexadecimal To String

John Boia <[email protected]>
Newsgroups gmane.comp.db.tds.freetds
Message-ID <[email protected]>
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
> [email protected]
> http://lists.ibiblio.org/mailman/listinfo/freetds
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.