Re: Hex Conversion? (HELP PLEASE!)
Hello there
Since this is C++ forum, how about doing it the C++ way?
something similar to
Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <iterator>
struct DOHString
{
typedef std::ios_base::fmtflags FlagType;
explicit DOHString(){}
void convert(const std::string& str, std::ostream& os,
FlagType flagValue = std::ios_base::hex | std::ios_base::showbase)
{
// set the iostate format values
os.flags(flagValue);
// convert each char by the called function
// don't forget to use the delimiter to your needs!
std::transform(str.begin(), str.end(),
std::ostream_iterator<int>(os, " "),
basicShift);
}
private:
static int basicShift(char c)
{
// do the work here
// explicit cast
return static_cast<int>(c);
}
};
int main()
{
// test runs
// to file, in Hex
std::ofstream outFile("out.txt", std::ios_base::binary);
DOHString c;
c.convert("wow how pow bow", outFile);
// to console, in Oct
c.convert("cow", std::cout, std::ios_base::oct | std::ios_base::showbase);
}