I have a vector full of decimal values and I want to convert each element of the vector to ASCII represented strings (or char arrays). How do I do this? Each element of the array is 8 hex bits long and has been converted to it's decimal representation. So...

HEX(41424344) = DEC(1094861636) = ASCII('A','B','C','D')

I have the HEX to DEC part down fine but I can't quite figure out how to do the second part. I've tried using reinterpret_cast with many issues. Any simple solutions to this problem?

I feel like I can't make a conversion from my DEC stage to ASCII correctly because its bits 0 and 1 of the HEX that give char(0) and so on. With the dec representation there isn't a straightforward conversion is there?

Edited 6 Years Ago by dansnyderECE: n/a

Well ASCII characters run from 0-255 so any number between 0 and 255 will convert right to an ASCII value

int number = 48;
char character = number; // character is now '0'

Numbers larger than 255 should wrap around but you can always use the % operator

int number = 1068
char character = number % 255; // character is now '0'

If this isn't what your looking for let me know.

That's a useful way of using the mod operator. I can definitely use this. So if I wanted to convert, say... 1234344356355 to a string, how would I do it? For simplicity we can just have like, char character1, char character2, etc. Would I mod number, get character1, subtract 225 from number, mod 225 again to get character2?

Just push the big number into a stringstream and then read it out as a string:

std::stringstream ss;
int number = 12345;
ss << number;
std::string yourString;
ss >> yourString;

When I try using a stringstream I get the following error:

error: aggregate ‘std::stringstream ss’ has incomplete type and cannot be defined

I'm not familiar with using this class. Is there something other than <string> i need to include ot use this?

Nevermind, i included <sstream>

Edited 6 Years Ago by dansnyderECE: n/a

Actually, this doesn't do the conversion for me as far as I can see. It converts 12345 into "12345". Im looking to go: 126 into "ABC"

Edited 6 Years Ago by dansnyderECE: n/a

Well, actually, what I want to do is take HEX( 12,ab,17,4a ) and turn it into string("abcd") (the conversion isn't correct, I'm just giving an example.)

Edited 6 Years Ago by dansnyderECE: n/a

if you want to convert a number to a letter that you take that number plus 'A' and that will give to the letter.

int number = 4
char ch = number + 'A'; // ch is now 'E'

doing this over an array with each element being a single digit is pretty straight forward but to go from 526 to FCG is a little more complex. you will need to % 10 to get the last digit of the the number than convert it. then you /= 10 to get rid of the last digit and then keep going until you have all of the digits. This does present the problem that you will only get A-J as you letters.

Although somewhat unwieldy I think that it may work out just fine to take my 8 bit hex word (keep in mind that this is actually a string), convert the first 2 characters to decimal, and then type cast it, store that in my string, repeat and iterate through vector elements until I find "00". That way I won't be limited to specific characters (keep in mind that I will more than likely have to open stdout and stdin using this mechanism.)

Instead of dividing by 10, you can also do the stringstream thing to get the number into an std::string. Then you can access individual elements (numbers) with simply yourString[i] . You can do the conversion with +'A', then put humpty dumpty back together again.

Okay, so I have it figured out sortof using a combination of methods suggested by many people. Here's what I have so far:

string filename;
    int conversion;
    int flag = 0;
    int k=999;//(top->a0)-1;
    while (flag==0)
    {
        k+=1;
        for (int i=0; i<=3; i++)
        {
            conversion = hexCharValue(tempInstructionMemory[k][i*2])<<(4*1);
            conversion += hexCharValue(tempInstructionMemory[k][(i*2)+1])<<(4*0);
            if (conversion==0)
            {
                flag = 1;
                break;
            }
            filename+=(char)conversion;
        }
    }

Is there a more concise way of doing this same thing? I have a tendency to write very wasteful code.

Edited 6 Years Ago by dansnyderECE: n/a

I have a vector full of decimal values and I want to convert each element of the vector to ASCII represented strings (or char arrays). How do I do this? Each element of the array is 8 hex bits long and has been converted to it's decimal representation. So...

HEX(41424344) = DEC(1094861636) = ASCII('A','B','C','D')

I have the HEX to DEC part down fine but I can't quite figure out how to do the second part. I've tried using reinterpret_cast with many issues. Any simple solutions to this problem?

I feel like I can't make a conversion from my DEC stage to ASCII correctly because its bits 0 and 1 of the HEX that give char(0) and so on. With the dec representation there isn't a straightforward conversion is there?

Hi
If I understand you correctly, there are hexadecimal numbers (HEX(41424344)) given where each of them consists of 4 double-digit hex numbers. As for your example, unsigned int N = 0x41424344. Obviously you want to separate this 32-bit number into its 4 double-digit hex numbers 0x41, 0x42, 0x43, 0x44. This can easily be done by masking out each double-digit number and shifting the result to its lower right position to get the ordinal number for ASCII coding, for example:

unsigned hex = 0x41424344, mas = 0xff000000; char cha[4];
int j=0; while(hex>0){cha[j]=(hex&mas)>>24;j++;hex<<=8;}
for (j=0;j<4;j++) cout << cha[j] << "  "; //output: A  B  C  D

I thing your question should have been better posted to C forum for getting faster result.

-- tesu

That's another interesting way of doing this. I like using logic operations as much as possible so I'll use this eventually I'm sure. Why is this kind of question better suited for the C forum? (just out of curiosity.)

I just think people here are typically more interested in STL ("new") c++ and that typically has nothing to do with hex and shift operators, etc. The C guys seem to like that stuff more :)

This article has been dead for over six months. Start a new discussion instead.