c++ - Convert ASCII string into Decimal and Hexadecimal Representations -
I need to replace an ASCII string ... "hello2" in decimal and hexadecimal representation (a numerical form, Specific type is irrelevant). So, "hello" will be: 68 65 6C6C6F32 in hex. How can I do this in C ++ without using a huge statement?
Edit: OK, so this is the solution that went with me:
int main () {string c = "b"; Char * cs = new char [c.size () + 1]; Std :: strcpy (cs, c.c_str ()); Cout & lt; & Lt; CS & LT; & Lt; Endl; Char a = * cs; In the form of int = a; Cout & lt; & Lt; Like & lt; & Lt; Endl; Return 0; }
You can use printf () to write results to stdout or you can use Sprintf / snprintf is a string to write the results. % X Here is the key in the format string.
#include & lt; Cstdio & gt; # Include & lt; Cstring & gt; Int main (int argc, char ** argv) {char * string = "hello2"; Int i; For (i = 0; i & lt; strlen (string); i ++) printf ("% x", string [i]); Return 0; }
If you want to work with the C ++ std :: string, you can use the c_str () method of the string to generate a C character array.
Comments
Post a Comment