-
2 chars, one int
ok, little bit of a problem here.
I am reading in chars, x and y. once read, i need to print them out as y, x.
I have an int Z which i need to contain y AND x without adding them. so if x actually was 35 and y actually was 37, i would need Z to store 3735.
once i figure this out i need to shift the int left 4 bits, but i figured i would get this chunk done first.
any help is appreciated.
thanks,
keith
-
Instead of the 2 chars and 1 int. make 3 chars and use strcat() to put those 2 chars into the 1 char. Ex:
Code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char tempchar1[3];
char tempchar2[3];
char totalchars[5] = {"\0"};
cin >> tempchar1;
cin >> tempchar2;
strcat(totalchars, tempchar1);
strcat(totalchars, tempchar2);
strcat(totalchars, "\0");
cout << "Together: " << totalchars << endl;
return 0;
}
-
hrm. maybe it would help if i posted code.
int a, b;
char xCha, xChb;
int combine;
fIn2.open(userfilename.c_str(), ios::in | ios::binary);
fIn2.seekg(ios::beg);
fIn2.seekp(1000);
while(!fIn2.eof()) {
fIn2.get(xCha);
fIn2.get(xChb);
a= (int)xCha;
b= (int)xChb;
combine = ???;
fout.put(combine);
}
the int "combine" needs to take ints b and a and combine them. for example if "a" reads C3 and "b" reads 23, i would need "combine" to read 23C3.
any help using the code i have?
thanks,
keith
-
an int is 4 bytes. so you should be able to store your bytes A and B in the low two bytes of your int.
Z = ((int)A<<8) | (int)B;
done.
-
hrm. i tried both these methods and they don't seem to give me the results i'm looking for. i'm probably just dumb and am missing something basic, but here is what i DO have:
i have two ints.
i need to put these two ints together, without adding them mathematically. like
int a = 12
int b = 78
and int c must = 1278, not 90.
any ideas? is it possible?
thanks again,
keith
-
our methods did the same thing.
((int)A<<8) | (int)B
gives the same results as
A*256 + B
0x12 * 256 + 0x78 is 0x1278
-
I wrote a simple program that does what your looking for, i hope it helps
Code:
#include <iostream>
using namespace std;
int main() {
int a = 12;
int b = 78;
int c = 0;
char buffer[5];
sprintf(buffer, "%d%d", a, b);
c = atoi(buffer);
cout << c << endl;
system("pause");
return 0;
}
-
hrm. when i use this method:
while(!fIn2.eof()) {
fIn2.get(A);
fIn2.get(B);
combine = ((int)A<<8) | (int)B;
fout.put(combine);
it seems to be printing out every other int, rather than combining both A and B. i'm probably missing something.....also, what does | do in this program?
keith
-
don't use put. Use write(). I think put just does one byte.
fout.write(&combine,2);
except you should probably understand that this writes out the low two bytes of combine and ignores the high two.
-
also, since intel is lil-endian, you're bytes will be reverse of what you expect.
-
"|" operator is the bitwise "OR" meaning if you have the following bytes
01011000
10000110
the result is
11011110
as each bit is or'd
-
oooh awesome, OR'ing was going to be a question later on!
but i tried using write(&combine,2) and got the following error when compiling:
'write' : cannot convert parameter 1 from 'int *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
keith
-