hex converting
-
So is your question about converting base X numbers to base 16, or displaying text to the screen? If the former, try:
int number = 1609564721; char str[16]; sprintf(str, "%#x", number);
If the latter, try:printf("%s\n", str);
orcout << str << endl;
My friends dad gave me a program to write cause he said it will make me learn. I have to connect to a server and send it 4 byte messages that have the form of 5FF00239. The message changes though, because the window (dialog i think its called) has 2 check boxes. If only the first check box is checked then the message should be 5FF00239 and if the second check box is only check it should be 5FF0023A. But if both are checked then he wants the message to be 5FF0023B. How do I make these messages? I don't know what type they should be. I can do everything else he wants except i don't know how to do the message. I thought it would be a string, but he says if it is, then the message is 8 bytes instead of 4? Please help me...
-
Hi everyone, First off i would like to say this site is great, especially for a 15 year old that is trying to learn VC++ on his own. I want to send a hex number to the screen, but don't know how. What data type should it be, and how would i represent a hex number. For example I want to send something like 5FF00231 which would be an 4 byte number. I don't know how though, would it be an int, a string? Please help...thank you so much
Hex numbers are usually preceded by the characters 0x to indicate the following four byte code is a hex value and not an integer. So, the end message should read something like 0x5FF00231. I would use an integer (because hex numbers are generally used to display a 32-bit integer unsigned or signed in Base 16 format - remember that, especially if you want to get into the games industry :)). So my code in WinMain would look like :
int WinMain(blah...) { int myhex = 0x5FF00231; char message[32]; sprintf(&message[0], "0x%x", myhex); ::MessageBox(NULL, message, message, MB_OK); } }
Hope that helps you :) -
Hex numbers are usually preceded by the characters 0x to indicate the following four byte code is a hex value and not an integer. So, the end message should read something like 0x5FF00231. I would use an integer (because hex numbers are generally used to display a 32-bit integer unsigned or signed in Base 16 format - remember that, especially if you want to get into the games industry :)). So my code in WinMain would look like :
int WinMain(blah...) { int myhex = 0x5FF00231; char message[32]; sprintf(&message[0], "0x%x", myhex); ::MessageBox(NULL, message, message, MB_OK); } }
Hope that helps you :) -
Hex numbers are usually preceded by the characters 0x to indicate the following four byte code is a hex value and not an integer. So, the end message should read something like 0x5FF00231. I would use an integer (because hex numbers are generally used to display a 32-bit integer unsigned or signed in Base 16 format - remember that, especially if you want to get into the games industry :)). So my code in WinMain would look like :
int WinMain(blah...) { int myhex = 0x5FF00231; char message[32]; sprintf(&message[0], "0x%x", myhex); ::MessageBox(NULL, message, message, MB_OK); } }
Hope that helps you :) -
My friends dad gave me a program to write cause he said it will make me learn. I have to connect to a server and send it 4 byte messages that have the form of 5FF00239. The message changes though, because the window (dialog i think its called) has 2 check boxes. If only the first check box is checked then the message should be 5FF00239 and if the second check box is only check it should be 5FF0023A. But if both are checked then he wants the message to be 5FF0023B. How do I make these messages? I don't know what type they should be. I can do everything else he wants except i don't know how to do the message. I thought it would be a string, but he says if it is, then the message is 8 bytes instead of 4? Please help me...
Ok, numbers are treated differently when sending them versus displaying them. When you talk about a "hex number," that is for display purposes only. In other words, 1609564729 and 0x5FF00239 are treated identically by the computer. It stores them as binary regardless of how you use them. Now if you are sending "1609564729" and "0x5FF00239" (notice the quotes) to some other process/computer, then they are two totally different things. So, whether you send 1609564729 or 0x5FF00239 to the server, it makes no difference. What mechanism are you using to send data to the server?
-
Ok, numbers are treated differently when sending them versus displaying them. When you talk about a "hex number," that is for display purposes only. In other words, 1609564729 and 0x5FF00239 are treated identically by the computer. It stores them as binary regardless of how you use them. Now if you are sending "1609564729" and "0x5FF00239" (notice the quotes) to some other process/computer, then they are two totally different things. So, whether you send 1609564729 or 0x5FF00239 to the server, it makes no difference. What mechanism are you using to send data to the server?
-
That is what I want to do later on, I want to program video games. :-) Can you explain what the char message[32]does?
It reserves a spot (named "message") in memory for 32
char
acters. -
It reserves a spot (named "message") in memory for 32
char
acters. -
Hi, thanks for helping me. mechanism? I want to use the int send command. Is that what you are talking about. So you are saying that I can use a int variable, but just put 0x in front of the number?
Bingo!! These three code snippets are the same: int num = 123456789; // base-10 SendToServer(num); int num = 0x75BCD15; // base-16 SendToServer(num); int num = 0726746425; // base-8 SendToServer(num);
-
but I only need 4 bytes? should i change the 32 to 4 then? Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
You don't need this at all. Dangleberry's post was just showing you how to convert a number to a string (which was then displayed in a message box).
-
Bingo!! These three code snippets are the same: int num = 123456789; // base-10 SendToServer(num); int num = 0x75BCD15; // base-16 SendToServer(num); int num = 0726746425; // base-8 SendToServer(num);
For the send command, i have problems it needs the socket and a message and a length, and some flags. Its says I can't use int because the message has to be const char *. for the length is that in bytes? should it be 4 or 10 to represent all numbers/letters? Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
-
For the send command, i have problems it needs the socket and a message and a length, and some flags. Its says I can't use int because the message has to be const char *. for the length is that in bytes? should it be 4 or 10 to represent all numbers/letters? Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
How about a code snippet of what you are trying to do?
-
How about a code snippet of what you are trying to do?
hi, here is what i want to do, the program is connected to the server already. (I don't know how to connect, i just use my friends dad program to connect) int message; message = 0x5FF0023A; send(s, message, strlen(message), 0); It says send can't convert parameter 2 from int to const char * and it says the same thing about strlen. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
-
hi, here is what i want to do, the program is connected to the server already. (I don't know how to connect, i just use my friends dad program to connect) int message; message = 0x5FF0023A; send(s, message, strlen(message), 0); It says send can't convert parameter 2 from int to const char * and it says the same thing about strlen. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
NewHSKid wrote: the program is connected to the server already. (I don't know how to connect, i just use my friends dad program to connect) Does that mean he is supplying you with a valid socket descriptor? Given that the second parameter of
send()
is expecting achar*
, you'll need:int nMessage = 0x5FF0023A;
char szMessage[9]; // hold 8 characters plus a \0
sprintf(szMessage, "%d", nMessage); // or itoa(nMessage, szMessage, 16);
int nBytes = send(s, szMessage, lstrlen(szMessage), 0); -
NewHSKid wrote: the program is connected to the server already. (I don't know how to connect, i just use my friends dad program to connect) Does that mean he is supplying you with a valid socket descriptor? Given that the second parameter of
send()
is expecting achar*
, you'll need:int nMessage = 0x5FF0023A;
char szMessage[9]; // hold 8 characters plus a \0
sprintf(szMessage, "%d", nMessage); // or itoa(nMessage, szMessage, 16);
int nBytes = send(s, szMessage, lstrlen(szMessage), 0); -
hi, here is what i want to do, the program is connected to the server already. (I don't know how to connect, i just use my friends dad program to connect) int message; message = 0x5FF0023A; send(s, message, strlen(message), 0); It says send can't convert parameter 2 from int to const char * and it says the same thing about strlen. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
-
Hi, Thanks for helping. So when i use that code, the receiving program will get the hex number of 5FF0023A? Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
No, the
recv()
function on the other end will receive "5FF0023A" as achar*
. It can convert that to anint
eger (long
actually) withstrtol()
. long lMessage; char szMessage[9], *stop; int nBytes = recv(s, szMessage, sizeof(szMessage), 0); lMessage = strtol(szMessage, &stop, 16); -
My friends dad gave me a program to write cause he said it will make me learn. I have to connect to a server and send it 4 byte messages that have the form of 5FF00239. The message changes though, because the window (dialog i think its called) has 2 check boxes. If only the first check box is checked then the message should be 5FF00239 and if the second check box is only check it should be 5FF0023A. But if both are checked then he wants the message to be 5FF0023B. How do I make these messages? I don't know what type they should be. I can do everything else he wants except i don't know how to do the message. I thought it would be a string, but he says if it is, then the message is 8 bytes instead of 4? Please help me...
Windows Sockets can be highly complicated as well as windows programming for people somewhat new to programming. From your posts it seems as if your skipping quite a few steps in the learning process. I think if you ended up writing a program that does what you want now it will be mostly cut/paste code and you won't get much out of the experience. This program would be an excellent learning experience, but you should make creating it more of a long range goal.
-
but I only need 4 bytes? should i change the 32 to 4 then? Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
The only actual "numbers" in the computer are semi-conductor gates that are either on or off. That is fine for the computer, but most people need something visual to help us understand what is going on. To the computer, all numbering base sytems look the same - binary. It is only when outputting the graphical representation of those numbers that you need to worry about data types and base. So, the number 255 is represented in the coputer by 8 logic gates being on: 11111111 If you, the human, wants to see that number in hexadecimal, then you tell your program to format that number in base 16 and you see "FF". Now, the characters "FF" are represented by the ascii code for the letter 'F' - which is 70 or x40 - so if you looked at the raw bits for the characters "FF" you would see 70 70 or the bits 01000110, 01000110. So what happened??? You started with 11111111 and ended up with 01000110, 01000110! The confusion comes from figuring out what format the reciever of the data is expecting. That is what function headers are for. They define how to interperet the bits that are being sent.