views:

335

answers:

5

I need to convert an ASCII string like... "hello2" into it's decimal and or hexadecimal representation (a numeric form, the specific kind is irrelevant). So, "hello" would be : 68 65 6c 6c 6f 32 in HEX. How do I do this in C++ without just using a giant if statement?

EDIT: Okay so this is the solution I went with:

int main()
{
    string c = "B";
    char *cs = new char[c.size() + 1];
    std::strcpy ( cs, c.c_str() );
    cout << cs << endl;

    char a = *cs;
    int as = a;
    cout << as << endl;
    return 0;
}
+3  A: 

A string is just an array of chars, so all you need to do is loop from 0 to strlen(str)-1, and use printf() or something similar to format each character as decimal/hexadecimal.

Oli Charlesworth
That's C, buddy.
DeadMG
And so it deserves a downvote??
Oli Charlesworth
@DeadMG: C is still valid C++
0A0D
Even in C++, and every language out there, when you boil it all down, a string is just an array of chars.
glowcoder
Technically, that code is valid C++. But then, so is *NULL, and I would downvote you for that, too. strlen won't take std::strings, and nor will printf. That makes your code invalid for any normal C++ program, and bad advice. C-strings should be avoided like the plague.
DeadMG
@DeadMG: Why would a function designed for C strings take a std::string? There is nothing wrong with C strings when they are used properly (such as strncpy vs strcpy)
0A0D
@DeadMG: "hello2" is a string literal; the OP said nothing about `std::string`. As others have pointed out, using `printf()` is considerably less verbose than C++ stream formatting.
Oli Charlesworth
+6  A: 

Just print it out in hex, something like:

for (int i=0; i<your_string.size(); i++)
    std::cout << std::hex << (unsigned int)your_string[i] << " ";

Chances are you'll want to set the precision and width to always give 2 digits and such, but the general idea remains the same. Personally, if I were doing it I'd probably use printf("%.2x");, as it does the right thing with considerably less hassle.

Jerry Coffin
Missing ASCII value example
0A0D
@0A0D:missing reasonable excuse for downvote. The question already gives an example of both the input and the output. I'm just telling him how to get it.
Jerry Coffin
@Jerry: "hello2" into it's decimal and or hexadecimal representation" - seems clear to me he wants decimal and hexadecimal.
0A0D
@0A0D: That's what docs are for. He could add a link to docs if he wanted to "complete" the answer. The point is that he gave him a pointer to the string formatting options in iostreams.
Merlyn Morgan-Graham
A: 
for(int i = 0; i < string.size(); i++) {
    std::cout << std::hex << (unsigned int)string[i];
}
DeadMG
doesn't work on my compiler (I think the cast is needed, though I'm wondering why).
Samuel_xL
What's the error? Builds fine on my compiler- although the cast is needed to get hex.
DeadMG
Because `operator<<` is overloaded, and you need the overload that takes an `int` (or `short`, `long`, etc.) for the `hex` flag to have an effect (i.e., the overload for `char` ignores the `hex` flag).
Jerry Coffin
A: 

You can use printf() to write the result to stdout or you could use sprintf / snprintf to write the result to a string. The key here is the %X in the format string.

#include <cstdio>
#include <cstring>
int main(int argc, char **argv)
{
    char *string = "hello2";
    int i;

    for (i = 0; i < strlen(string); i++)
        printf("%X", string[i]);

    return 0;
}

If dealing with a C++ std::string, you could use the string's c_str() method to yield a C character array.

brennie
Why the downvote? It works correctly, does it not?
brennie
Missing string to ASCII value example
0A0D
String to ASCII value example? The poster states that he starts with an ASCII value. "I need to convert an ASCII string like... "hello2""
brennie
@brennie: "hello2" into it's decimal and or hexadecimal representation" - seems clear to me he wants decimal and hexadecimal.
0A0D
@0A0D the key phrase is "and or" which I assume means "and/or." The key word here is "or."
brennie
A: 
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>

int main() {
  std::string hello = "Hello, world!";

  std::cout << std::hex << std::setw(2) << std::setfill('0');
  std::copy(hello.begin(),
            hello.end  (),
            std::ostream_iterator<unsigned>(std::cout, " "));
  std::cout << std::endl;
}
Anonymous Coward