Discussion:
binascii in C++
(too old to reply)
Jeremy
2006-02-01 02:40:12 UTC
Permalink
I'm working on a project to create a keyfinder program that finds the
Windows CD Key in the registry and decodes it. I prototyped it in
Python and it worked great but for several reasons I've decided to
rewrite it in C++. I use the module binascii extensively in the Python
version but I can't find an equivalent module in C++. I'm not a
professional developer so maybe I'm overlooking something simple.

In particular I'm trying to find an equivalent to the binascii.b2a_hex()
and binascii.unhexlify() functions.

Thanks,

Jeremy
Jim Langston
2006-02-01 04:51:25 UTC
Permalink
Post by Jeremy
I'm working on a project to create a keyfinder program that finds the
Windows CD Key in the registry and decodes it. I prototyped it in Python
and it worked great but for several reasons I've decided to rewrite it in
C++. I use the module binascii extensively in the Python version but I
can't find an equivalent module in C++. I'm not a professional developer
so maybe I'm overlooking something simple.
In particular I'm trying to find an equivalent to the binascii.b2a_hex()
and binascii.unhexlify() functions.
Thanks,
Jeremy
Was a little bored, this is trivial so I thought I'd write it. Here it is
for b2a_hex(). I'll post the part ofr unhexlify when I finish it in a few
minutes.

#include <iostream>
#include <string>

class binascii
{
public:
static std::string b2a_hex( const std::string& input );
static std::string hexlify( const std::string& input );
static std::string a2b_hex( const std::string& input );
static std::string unhexlify( const std::string& input );
};

std::string binascii::b2a_hex( const std::string& input )
{
const static std::string HexCodes = "0123456789ABCDEF";
std::string HexString;
for ( int i = 0; i < input.length(); ++i )
{
unsigned char BinValue = input[i];
HexString += HexCodes[( BinValue >> 4 ) & 0x0F];
HexString += HexCodes[BinValue & 0x0F];
}
return HexString;
}

std::string binascii::hexlify( const std::string& input )
{
return b2a_hex( input );
}

int main()
{
std::string Input = "\01ABC\xFF";
std::cout << binascii::b2a_hex( Input );
std::string wait;
std::getline(std::cin, wait );
}
Jim Langston
2006-02-01 05:04:40 UTC
Permalink
Post by Jeremy
I'm working on a project to create a keyfinder program that finds the
Windows CD Key in the registry and decodes it. I prototyped it in Python
and it worked great but for several reasons I've decided to rewrite it in
C++. I use the module binascii extensively in the Python version but I
can't find an equivalent module in C++. I'm not a professional developer
so maybe I'm overlooking something simple.
In particular I'm trying to find an equivalent to the binascii.b2a_hex()
and binascii.unhexlify() functions.
Thanks,
Jeremy
Here is what I came up with in it's entiretly.

#include <iostream>
#include <string>
class binascii
{
public:
static std::string b2a_hex( const std::string& input );
static std::string hexlify( const std::string& input );
static std::string a2b_hex( const std::string& input );
static std::string unhexlify( const std::string& input );
};

std::string binascii::b2a_hex( const std::string& input )
{
const static std::string HexCodes = "0123456789ABCDEF";
std::string HexString;
for ( int i = 0; i < input.length(); ++i )
{
unsigned char BinValue = input[i];
// High half
HexString += HexCodes[( BinValue >> 4 ) & 0x0F];
HexString += HexCodes[BinValue & 0x0F];
}
return HexString;
}

std::string binascii::hexlify( const std::string& input )
{
return b2a_hex( input );
}

std::string binascii::a2b_hex( const std::string& input )
{
const static std::string HexCodes = "0123456789ABCDEF";
std::string BinString;
for ( int i = 0; i < input.length() - 1; i += 2 )
{
BinString += ( input[i] >= 'A' ? input[i] - 'A' + 10 : input[i] -
'0' ) * 16 +
( input[i + 1] >= 'A' ? input[i + 1] - 'A' + 10 : input[i + 1] -
'0' );
}
return BinString;
}

std::string binascii::unhexlify( const std::string& input )
{
return a2b_hex( input );
}

int main()
{
std::string Input = "\01ABC\xFF";
std::cout << binascii::b2a_hex( Input ) << std::endl;
Input = "01414243FF";
std::cout << binascii::a2b_hex( Input ) << std::endl;
// Check for error, hex string is missing a char. It should ignore last
half char.
Input = "4142434";
std::cout << binascii::a2b_hex( Input ) << std::endl;

std::string wait;
std::getline(std::cin, wait );
}

Continue reading on narkive:
Loading...