Typecasting 4 digit integer into a Char.

Hey I'm making an antitheft facial recognition software that records everything when an authroized user accesses the computer. I plan to attempt to sell this software online and on disc so bear that in mind when you submit contributions. But im trying to typecast the integer values that are the mouse coordinates into chars so i can input them into a file then typecast them back into integers later to replay the movement.

But i dont think you can typecast such large integers into chars can you? The numeric values for each char or key only go up to like 200 something. Since it is larger how would the computer handle typecasting the larger intiger into a char...or would it even allow it. I guess my best bet is just to type up a short code and try to typecast the numbers into chars then display the typecasted results in a messagebox. But if that doesn't work how would i store the 4 digit intiger value into a file. Right now that is just my best bet.

I've never typecasted before but when i tried to do the following code it gave me the error "invalid conversion from char to Const CHAR." This seems like a pretty simple problem. But i have never typecasted before and i thought it would work and dont see how its not.

1
2
3
4
5
6
7
8
char Letter;
int x=974;

Letter=(char)x;


MessageBox(NULL, Letter, "Typecasted result", MB_OK);


after i typecast the char into letter i planned to display Letter in a messagebox just to show it was working as planned then implement it into the code to record the mouse coordinates into a file. Going back to the larger integer i guess i'll have to just divide the intiger by a certain amount to get it lower then typecast those two numbers (the dividing number and the other) into two chars then input them both into the file. But i gotta fix this simple error first of why im getting the typecasting error

My future code after i get it working would resemble below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char Letter;
int x, y;

switch (msg)
{
      case WM_MOUSEMOVE:
       CURSOR cursorpos;
       cusrorpos.x=x;
       cursorpos.y=y;
       RecordMovement(x, y);
}

void RecordMovement(int x, int y)
{
Letter=(char)x;
file=fopen("C://Movements.txt", r);
fprintf(file, Letter);
Letter=(char)y;
fprintf(file, Letter);
fclose(file);

}

Last edited on
As you suspected, you can't fit 974 into a char; unsigned chars can only handle 0 - 255 (0x00 - 0xFF).

You could use ofstream to write the integers to file as strings.

1
2
3
4
5
6
7
8
9
10
int x = 0;
int y = 0;

ofstream ofs("mouse.log");

// get the mouse co-ords somehow...

ofs << x << "\t" << y << endl;

...


Then you can read the coord back into int variables later.

1
2
3
4
5
6
7
8
int x = 0;
int y = 0;

ifstream ifs("mouse.log");

ifs >> x >> y;

...


But there's nothing to stop you writing the integers to file in binary form.

Andy

P.S. MessageBox doesn't take a char as it's second param; it takes a char*

1
2
3
char buffer[16] = "";
sprinf(buffer, "char = %c", Letter);
MessageBox(NULL, buffer, "Type casted result", MB_OK);

Thanks andy i was just looking at the ifstream and ofstream. what are the different exit sequences in them for new lines ect ect in that library and how can i begin reading from a certain line?

for example \n stands for newline and you can specify what numerical position to start reading from in the file. But what are some of the other exit sequences? I just seperate each variable with >> or << when reading or writing? Also can i input multiple types of data in one sequence by seperating them. I just got my winsock working correctly and now i planned on using it to transfer binary files in this way and audio ect.


I've always passed char arrays or chars into the parameter and my code has always fucntioned as i expected to. But in any event that it doesn't i'll look to that first.

I am not sure what you mean by "exit sequence"?

If you want to start replay from a particular line, I could prob just skip forward a number of lines. The iostream library does support seeking to a char offset, but you'd need to know the value a line starts at for that to work. Unless you have a huge text file, the line skip approach is prob quick enough. And a lot easier to implement.

If you're using >> to read the data out of a file, you need to be careful how you format a line, as the extraction will usually stop at the first white space. So you can't store strings with spaces if you want to use this approach.

You can read and write a line at a time, and used string manipulation to write and read your data. But for mouse positions, it is probably all you need. And yes, you can mix types, with the caveat about strings with spaces (one work around is to replace spaces with e.g. unnderscores before saving the string. And then replacing the underscores after you read the string back (or another suitable char)).

If you want to write and read binary data instead, look at ostream::write/istream::read (and also fwrite -- rather than fprint -- and fread?).

Last edited on
What i mean by exit sequence is just the different letters proceeded by the "/" sign that accompish different tasks at the end of the instruction. How would i would read binary data from a file (image, exe, or other file) into an array of sorts? It would be just 0's and 1's representing bits and bytes. would this below code work and is there room for improvement?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

class Binary
{
public:
int byte[5000];
int CurByte;
int Position;

void SendFile(char Name);

}


void Binary::SendFile(char Name)
{
char File[30];
file=name;
ostream FileToSend (file, ios::in, ios::binary);
while (NotRead)
{

ostream& seekp(Position);
FileToSend.read(byte[CurByte],  1);
Send(socket, byte[CurByte], 1, 0);
Position++
CurByte++;

if (BytesRead>=SizeOfFile)
{
Char FileSentMSG[4];
FileSentMSG="//.//"
send(socket, FileSentMSG, 5, 0);
NotRead=false;
}
}
}



This is just a rough code (i plan to add in encryption) but i am not exactly certain how the binary file gets read into a variable that i can send over winsock. If i have to save all the 0's and 1's into a binary array of sorts then i can do that but i would prefer a more established method.

Since each byte only goes to 256 maybe if i read each byte itself as an integer which i could typecast into a char then send the whole integer that would work. I'm worried about the file not being pieced together correctly but as long as it is in the same sequence it was recieved then it will be able to be used normally. How would i access single bits in the file? And if i write these chars which are essentially the same byte sequence that i read to the new file and save it as that file type....will it work like usual. Say it is an exe or a image file. I know when it comes down to it all datatypes are the same (just a bunch of 0's and 1's). But i'm still having a hard time grasping the fact that i may be able to write a whole bunch of char's to an exe file and save it as an exe then acutally have it execute.
Last edited on
There all kinds of problems with the code you posted. To the extent that there no way it even compiles, let alone runs. I suggest you work through a beginners C++ book and then try again. (Code fragments don't need to be complete, but should not be so riddled with errors)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Binary
{
public:
int byte[5000]; // mistake
int CurByte;
int Position;

void SendFile(char Name); // mistake

} // mistake


void Binary::SendFile(char Name) // mistake
{
char File[30]; // dangerously small buffer
file=name; // mistake
ostream FileToSend (file, ios::in, ios::binary);
while (NotRead) // undeclared var
{

ostream& seekp(Position); // mistake
FileToSend.read(byte[CurByte],  1); // mistake
Send(socket, byte[CurByte], 1, 0); // mistake
Position++
CurByte++;

if (BytesRead>=SizeOfFile) // undeclared vars
{
Char FileSentMSG[4]; // mistake
FileSentMSG="//.//" // mistake
send(socket, FileSentMSG, 5, 0);
NotRead=false; // undeclared var
}
}
}


Casting an int to a char will just not work. But what you can so is, where socket has been successfully connected.

1
2
3
4
5
6
7
int m = 123;

...

send(socket, (char*)&m, sizeof(int), 0);

...
Last edited on
I realize alot of variables were undeclared and others were not correctly utilized in the function. It was just part of a rough code. Or as you called it a code fragment. I would see them all rather easily had i actually attempted to compile it. But i realized them when i was typing it up. When it comes to the buffer I usually have files in the same directory and don't use long filenames. I'm guessing i initialized to large of a buffer for byte? Or maybe i used an int instead of a char? And on the FileSentMSG i used quotes instead of brackets or string copy to declare it to the array.

So when im reading the from the file into the byte value i would just do (char)byte[CurByte]; ....or to be more class correct (char)Binary.byte[CurByte]. Well if i declared byte as an integer i would just read it into byte without using the (char) typecast.


Last edited on
There is a difference between castings to char and char*.
I'm fairly sure you want the latter.
Topic archived. No new replies allowed.