Discussion:
[freetds] Convert Hexadecimal To String
Velichko Yuriy
2013-09-24 17:25:31 UTC
Permalink
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!
John Boia
2013-09-24 18:49:25 UTC
Permalink
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;
}
Post by Velichko Yuriy
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?
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
Velichko Yuriy
2013-09-24 19:06:17 UTC
Permalink
Thanks!
Post by John Boia
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;
}
Post by Velichko Yuriy
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?
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
_______________________________________________
FreeTDS mailing list
FreeTDS at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/freetds
--
Best Regards!
Continue reading on narkive:
Loading...