944,112 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3371
  • C++ RSS
Sep 25th, 2004
0

Data loss in Type Conversion.

Expand Post »
Hello Everyone,

I'm working on a couple of programs, client/server. I'm using winsock library to handle data transfer between the two programs. I'm having a problem when it comes to sending int values, they must be converted to (char *) since that's the data type of the buffers used in the socket Send() and Recv() functions. to elaborate, consider the following:

Client side:
-----------
unsigned char rnd[4];
int noncer =0;

// Generating a random number
RAND_pseudo_bytes(rnd,sizeof(rnd));

noncer = (int)rnd;

non1=(char*)noncer;

// These two lines produce the same output which is 1244556
cout <<"\n Noncer :" << noncer << "\n";
cout.flush();
cout <<"\n Noncer char :" << (int)non1 << "\n";
cout.flush();

SendData(Socket, non1);

Server Side:
-----------
int client_nonce;
char String[50];
String_initialization(String);

// Reading the nonce identifier
RecieveMessage(socket, String, number_of_bytes);
client_nonce = (int)String;

// this results in 1244588 !!
cout<<"\n the nonce is:"<<client_nonce <<endl;
cout.flush();

If anyone can tell me what's causing the change of data i'd really appreciate it.

Thank you all in advance.
Layla.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Layla_2401 is offline Offline
22 posts
since Sep 2004
Sep 25th, 2004
0

Re: Data loss in Type Conversion.

Don't play around with casting like that unless you know what you're doing. Type punning can be tricky business, but barring transmission issues with your sockets, this should work:
C++ Syntax (Toggle Plain Text)
  1. #include <cstring>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int i1 = 12345;
  9. char buffer[sizeof ( int )];
  10.  
  11. memcpy ( buffer, (unsigned char *)&i1, sizeof ( int ) ); // Simulate send/recv
  12.  
  13. int *pi = (int *)buffer;
  14. cout<< *pi <<endl;
  15. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 25th, 2004
0

Re: Data loss in Type Conversion.

Hi Narue,

Thank you SO much for your help, I now get the same value at both sides
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Layla_2401 is offline Offline
22 posts
since Sep 2004
Mar 22nd, 2006
0

Re: Data loss in Type Conversion.

Hi I am having the same problem, but I couldnt solve it.
I need to send a CString with Unicode (16bit) to the send and recv function. Unfortunately all i get is garbage
Any help is welcomed
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dimkovtrajce is offline Offline
1 posts
since Mar 2006
Mar 22nd, 2006
0

Re: Data loss in Type Conversion.

Don't resurrect a two year old thread to say "me too"! Start a new thread if you have a new question. Geez.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: <string>
Next Thread in C++ Forum Timeline: Validation Program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC