Data loss in Type Conversion.

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Sep 2004
Posts: 22
Reputation: Layla_2401 is an unknown quantity at this point 
Solved Threads: 0
Layla_2401 Layla_2401 is offline Offline
Newbie Poster

Data loss in Type Conversion.

 
0
  #1
Sep 25th, 2004
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.
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,571
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Data loss in Type Conversion.

 
0
  #2
Sep 25th, 2004
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:
  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. }
Quick reply to this message  
Join Date: Sep 2004
Posts: 22
Reputation: Layla_2401 is an unknown quantity at this point 
Solved Threads: 0
Layla_2401 Layla_2401 is offline Offline
Newbie Poster

Re: Data loss in Type Conversion.

 
0
  #3
Sep 25th, 2004
Hi Narue,

Thank you SO much for your help, I now get the same value at both sides
Quick reply to this message  
Join Date: Mar 2006
Posts: 1
Reputation: dimkovtrajce is an unknown quantity at this point 
Solved Threads: 0
dimkovtrajce dimkovtrajce is offline Offline
Newbie Poster

Re: Data loss in Type Conversion.

 
0
  #4
Mar 22nd, 2006
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
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,571
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Data loss in Type Conversion.

 
0
  #5
Mar 22nd, 2006
Don't resurrect a two year old thread to say "me too"! Start a new thread if you have a new question. Geez.
I'm here to prove you wrong.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC