c + +: string to int to hex to string [ascii equivalent] [output string]

Q: Hi! I try a string like “EDF” and convert them to ASCII HEX equivalent, and then print it to take off to another string. For example, if the input string is
inString = “EOF”;
I want the to be:
outString = “454F46;

how can I do? Thank you!

-guy


Best Answer: There are a number of ways to do this, one I am suggesting may or may not be one of the most optimal but is easy:

#include <iostream>
#include <string>
using namespace std;
int main()
{
string hex = "100C";
int decimalValue = 0;

sscanf(hex.c_str(), "%x", &decimalValue);
cout<<decimalValue<<endl;
return 0;
}

Else you know the algo and that hex is base 16, write your own function.


Re:I work with custom file formats and HTTP based connections between desktops and different server types, so I do a fair amount of this kind of fun stuff. Conversions between ASCII, UTF-8 multibye, HTML & # xxx codes; MIME- and similar encoding/decoding, conversion to/from binary. Gah!

Re:Originally posted by: guy

Originally posted by: guy
OT, but why do programming books/classes spend so much time on stuff like this? IMO, it is about the most tedious & least useful tasks you can learn.

actually that's not what I'm learning. I'm making a SIC assembler using C++ and I have to input the intermediate file (which has BYTE parameters in plain text) and convert them into ASCII Hex, then print them to the object file.

yes it's tedious, but it's a good thing to know so you can just stick the code in there when you need it.

btw, the sprintf() worked. I'm sure there's a better, more elegant way to do this, but it's due tomorrow and I'll take whatever works in a pinch! thanks fellas!

-guy

Wow, there's actually a point to it? Cool :P
I've always seen it in books and such, but never needed it in practice. But then I've never worked at that kind of low level either.


Re:Originally posted by: guy
OT, but why do programming books/classes spend so much time on stuff like this? IMO, it is about the most tedious & least useful tasks you can learn.

actually that's not what I'm learning. I'm making a SIC assembler using C++ and I have to input the intermediate file (which has BYTE parameters in plain text) and convert them into ASCII Hex, then print them to the object file.

yes it's tedious, but it's a good thing to know so you can just stick the code in there when you need it.

btw, the sprintf() worked. I'm sure there's a better, more elegant way to do this, but it's due tomorrow and I'll take whatever works in a pinch! thanks fellas!

-guy


Re:OT, but why do programming books/classes spend so much time on stuff like this? IMO, it is about the most tedious & least useful tasks you can learn.

Re:casting each char to unsigned int and using sprintf %02X should also work though you might need to fix characters > 127 by casting to unsigned char first.

Re:take inString[1] = temp; ( second character of the string )
where temp is a 8 bit character ( char temp = 0 )

temp = 'O' in this case,
'O' = 0×4F

test the high and low nibbles with bit shifts for < 0×0A
if the nibble is greater than or equal to 0×0A, add 0×40 to get A thru F
if the nibble is less than 0×0A, add 0×30 to get 0 thru 9

so 'O' gets broken down into 0×34 ('4'), 0×46('F')


Related posts

Leave a comment

0 Comments.

Leave a Reply


click to changeSecurity Code

[ Ctrl + Enter ]