Hi Everybody.

Was hoping someone could clear this up for me. My understanding is not good in this area.

The below code simply reads a string from a text file two characters at a time. It then outputs them to console then casts them to an int and then outputs them to the console again.

Where I have the cast to int each number comes out different as expected. But.. for the below line, 1 - does the below only output the int value for the first character (remembering that two values drawn into the char string)?

and

**2 - ** when I change the cast to the below. Why do all the numbers output to the console come out the same?

int mint = (int)mychar;

int *mint = (int*)mychar;
cout << *mint << endl;

Is this the correct way to display the int value for two characters in the char string?

int main()
{
    ifstream myfile;
    stringstream ss;

    myfile.open("test.txt", ios::out |ios::binary);

    char *mychar = new char[2];

    while ((myfile.read(mychar,2)) )
    {

       cout << mychar << endl;
       int *mint = (int*)mychar;
       cout << *mint << endl;

    }
    myfile.close();



}

Recommended Answers

All 6 Replies

cout << mychar << endl;

This line is passing a char pointer to cout. When cout is given a char pointer to operate on, the function executed looks like this:

Output the char being pointed to, and the next char, and the next, and so on, until you find a char with value zero. Do not output that final char, but just stop.

cout << *mint << endl;

This line is passing an int to cout. When cout is given an int to operate on like this, the function executed looks like this:

Output the int.

They are two different functions, doing two different things.

So here's something for you to ponder. How big is an int? Typically, four bytes, or eight bytes. So if you take something two bytes long (like an array of two char) and you then give the compiler the address of that first char and you tell the compiler that it's actually the address of a single int, like this int *mint = (int*)mychar;, how much of that array of two char is going to get treated as being part of the same, single int? Remember, the compiler believes you, and thinks an int is four (or eight) bytes long.

Is this the correct way to display the int value for two characters in the char string?

No. If you want to take a char, and see what number it actually is, get the char, and then convert it to an int.

For example, using an explicit cast:

char x = 'a';
int y = (int)x;

y will now have the value 97, because that's the number that is used typically to represent 'a' (http://www.asciitable.com/)

Thank you for that explanation. That does make it clearer.
The problem I have is that I am creating a binary string two bytes wide and want to convert it to an int. I'm not sure how I do that?

What do you mean by "convert it to an int"?

Do you mean you want to take two chars (e.g. the char '9' and the char '6') and using that create an int with the "obvious" value (e.g. the integer value 96)?

I'm working on this with my colleage. Basically what we're trying to do is this. We have a stream of data. Lets say that that stream represents rows of 6 characters which represents three columns.

So we are dividing the 6 characters into 2 character chunks and each two consecutive characters is then converted to an integer.

What I'm saying is that we want to convert each binary string (of two bytes in width) into a integar. We could be understanding this incorrectly. Maybe you just add them together, we're not sure.

What I'm saying is that we want to convert each binary string (of two bytes in width) into a integar.

That doesn't tell us how you want it converted.

Do you want "65" to become the integer 65, or do you want it to become 55 for the ascii character "6" and 54 for the ascii character "5"?

If the six characters are:1 2 3 4 5 6
what three integers are you expecting to get from that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.