how pass an __int64 to unsigned char [16]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kenzogio
    New Member
    • Apr 2009
    • 16

    how pass an __int64 to unsigned char [16]

    Hi,

    I have a struct "allmsg" and him member :
    Code:
     unsigned char card_number[MAX_CARD_NUMBER]; //16
    allmsg.card_num ber


    And I must store contain of an __int64 into this member

    Code:
    	__int64 temp_badge_id;
    	unsigned char *ptemp_badge_id; //

    So I pass value to this last :

    Code:
    DLL_API(long) vgt_sendto_S3100(__int64 badgeid, char* remote_host, int remote_port)
    {
    						//allmsg.card_number=(unsigned char*)badgeid; //ERROR
    					
    	unsigned int crc32=0;
    						temp_badge_id = badgeid; //here passing value OK

    And the code above here: seems ok but the contains of allmsg.cardnumb er
    is not the exactly the same than the _int64
    Code:
    		
    allmsg.card_number[15]=ptemp_badge_id[15];  //not good way
    allmsg.card_number[14]=ptemp_badge_id[14];
    ....
    ...
    allmsg.card_number[0]=ptemp_badge_id[0];
    It seems ok till 8 first bytes and then contains is not good.
    So maybe _int64 must be casted differently to pass in unsigned char ?

    And in fact _int64 is 8 bytes and not 16 bytes where it must store?



    Here above codes to check for details:

    I have a struct define in S3100.h :

    Code:
    #define MAX_CARD_NUMBER 16
    #define MAX_PADDING 16
    
    #define CRC_LENGTH 36
    #define CRC_LENGTH_RETURN 16
    
    struct frame_message { 
    unsigned int sender_id;
                              ..
    unsigned char card_number[MAX_CARD_NUMBER];//crcin
                            ..
    unsigned char padding_bytes[MAX_PADDING];
    }allmsg; 
    
    ..
    
    unsigned char *ps_card_number;
    
    ..
    in the other file S3100.cpp (dll) , I have this :
    Code:
    #include "S3100.h"
    
    ..
    #define PACKET_LENGTH 68 //with padding bytes reserved
    
    #define CRC_LENGTH 36
    						
    .........
    .........
    ////IMPLEMENTATION CRC CLASS/////
     =======================================================================================
    						// Fonction: Crc32::Crc32()
    						// =======================================================================================
    						
    						Crc32::Crc32()
    						{
    							memset( &this->crc32_table, 0, sizeof(unsigned int)*256 );
    							this->polynomial = 0;
    						}		
    						// =======================================================================================
    						
    						........
    						........
    						
    						//VARIABLES GENERALES ////
    						long error_code=0;		
    						bool DEBUG=true;
    						bool initcrcok=false;
    						unsigned int rescrc32=0;
    						char msg_crc[CRC_LENGTH];		// message to send
    	
    						char message[MAX_PATH];			// message to send
    						//char response[MAX_PATH];		// response to receive  //090406
    						char* response = (char*)malloc(1024*sizeof(char));  
    						int message_length=68;			// length of the message to send
    						int response_length=0;			
    						int engine_id=0;				
    						int analyse_id=0;	
    						//int remote_port=0;	
    						int timeout_ms=1000;			
    					
    						char binary[80];
    					
    					
    						char *glremote_ip=NULL;
    						int glremote_port;
    					
    						bool init_struct=false;
    					
    						frame_message packet;
    						Crc32 *pCrc32 = new Crc32();
    					
    						__int64 temp_badge_id;
    						char *ptemp_badge_id; //090408 enlever le unsigned
    				
    						.......
    						.......
    						
    			
    						void init_structptr()
    						{
    						
    							memset(allmsg.card_number,0,MAX_CARD_NUMBER);
    							memset(allmsg.padding_bytes,0,MAX_PADDING);
    						
    							memset(message,0,MAX_PATH);
    							message_length=0;
    							memset(msg_crc,0,CRC_LENGTH);//crc
    						
    							memset(response,0,MAX_PATH); //090406
    						
    							psender_id = (unsigned char*)&(allmsg.sender_id);
    							pencrypt_key_id = (unsigned char*)&allmsg.encrypt_key_id;
    							pencrypt_size = (unsigned char*)&allmsg.encrypt_size;
    							pcrc = (unsigned char*)&allmsg.crc;
    							pdata_length = (unsigned char*)&allmsg.data_length;//crcin
    							psequence_number = (unsigned char*)&allmsg.sequence_number;//crcin
    							ppacket_type = (unsigned char*)&allmsg.packet_type;//crcin
    							ps_read_number = (unsigned char*)&allmsg.read_number;//crcin
    							ps_card_number = (unsigned char*)&allmsg.card_number; //crcin
    							ps_pin_code = (unsigned char*)&allmsg.pin_code; //crcin
    							ppadding_bytes = (unsigned char*)&allmsg.padding_bytes;
    						
    							//allmsg.sender_id=0x0a0b0c0d;  //hexadecimal sur 4 bytes rmq: nous sommes en little endian
    							//allmsg.sender_id=3;
    							allmsg.sender_id=0;
    							allmsg.encrypt_key_id=0;
    							//allmsg.encrypt_size=0;
    							allmsg.encrypt_size=40;
    							allmsg.crc=0; //090406
    							//allmsg.data_length=24; //crcin 1
    							allmsg.data_length=52; //crcin 1
    							//allmsg.sequence_number=12345; //crcin 2
    							//allmsg.sequence_number=1; //090407 géré en mode saisie clavier
    							allmsg.packet_type=1; //crcin 3
    							//allmsg.read_number=2; //crcin 4 //090401
    							//allmsg.read_number=1; //090407 géré en mode saisie clavier
    							allmsg.pin_code=0; //crcin 6 //090404
    								
    							alreadyinit =true;
    							//allmsg.card_number=0;//0904007
    						
    						ptemp_badge_id = (char*)&temp_badge_id; //090407
    						
    						}
    
    						......
    						......
    						void write_struct(bool a)
    						{
    						
    							if (a)
    							{	
    						#ifdef _LITTLE_ENDIAN
    								message[0]= psender_id[0];
    								message[1]= psender_id[1]; 
    								message[2]= psender_id[2]; 
    								message[3]= psender_id[3];	
    							
    								message[4]= pencrypt_key_id[0];
    								message[5]= pencrypt_key_id[1]; 
    								message[6]= pencrypt_key_id[2]; 
    								message[7]= pencrypt_key_id[3];
    							
    								message[8]= pencrypt_size[0];
    								message[9]= pencrypt_size[1]; 
    								message[10]= pencrypt_size[2]; 
    								message[11]= pencrypt_size[3];
    								///		
    								message[12]= pcrc[0];
    								message[13]= pcrc[1]; 
    								message[14]= pcrc[2]; 
    								message[15]= pcrc[3];
    								///
    								message[16]= pdata_length[0];
    								message[17]= pdata_length[1]; 
    								message[18]= pdata_length[2]; 
    								message[19]= pdata_length[3];
    								msg_crc[0]=pdata_length[0];///////crc 1
    								msg_crc[1]=pdata_length[1];
    								msg_crc[2]=pdata_length[2];
    								msg_crc[3]=pdata_length[3];
    							
    								message[20]= psequence_number[0];
    								message[21]= psequence_number[1]; 
    								message[22]= psequence_number[2]; 
    								message[23]= psequence_number[3];
    								msg_crc[4]=psequence_number[0];///////crc 2
    								msg_crc[5]=psequence_number[1];
    								msg_crc[6]=psequence_number[2];
    								msg_crc[7]=psequence_number[3];
    							
    								message[24]= ppacket_type[0];
    								message[25]= ppacket_type[1]; 
    								message[26]= ppacket_type[2]; 
    								message[27]= ppacket_type[3];
    								msg_crc[8]=ppacket_type[0];///////crc 3
    								msg_crc[9]=ppacket_type[1];
    								msg_crc[10]=ppacket_type[2];
    								msg_crc[11]=ppacket_type[3];
    							
    								message[28]= ps_read_number[0];
    								message[29]= ps_read_number[1]; 
    								message[30]= ps_read_number[2]; 
    								message[31]= ps_read_number[3];
    								msg_crc[12]=ps_read_number[0]; ///////crc 4
    								msg_crc[13]=ps_read_number[1];
    								msg_crc[14]=ps_read_number[2];
    								msg_crc[15]=ps_read_number[3];
    							
    								for (int i=0; i<16 ;++i )
    								{
    								msg_crc[16+i]=message[32+i]= ps_card_number[i]; //crc 5
    								}/*
    								msg_crc[16]=message[32]= ps_card_number[0]; //crc 5
    								msg_crc[17]=message[33]= ps_card_number[1]; 
    								msg_crc[18]=message[34]= ps_card_number[2]; 
    								msg_crc[19]=message[35]= ps_card_number[3];
    								msg_crc[20]=message[36]= ps_card_number[4];
    								msg_crc[21]=message[37]= ps_card_number[5]; 
    								msg_crc[22]=message[38]= ps_card_number[6]; 
    								msg_crc[23]=message[39]= ps_card_number[7];
    								msg_crc[24]=message[40]= ps_card_number[8];
    								msg_crc[25]=message[41]= ps_card_number[9]; 
    								msg_crc[26]=message[42]= ps_card_number[10]; 
    								msg_crc[27]=message[43]= ps_card_number[11];
    								msg_crc[28]=message[44]= ps_card_number[12];
    								msg_crc[29]=message[45]= ps_card_number[13]; 
    								msg_crc[30]=message[46]= ps_card_number[14]; 
    								msg_crc[31]=message[47]= ps_card_number[15];
    								*/
    						
    								msg_crc[32]=message[48]= ps_pin_code[0]; //crc 6
    								msg_crc[33]=message[49]= ps_pin_code[1];
    								msg_crc[34]=message[50]= ps_pin_code[2];
    								msg_crc[35]=message[51]= ps_pin_code[3]; 
    								
    								for (int i=0; i<16 ;++i )
    								{
    								message[52+i]= ppadding_bytes[i];
    								}/*
    								message[52]= ppadding_bytes[0];
    								message[53]= ppadding_bytes[1]; 
    								message[54]= ppadding_bytes[2]; 
    								message[55]= ppadding_bytes[3];
    								message[56]= ppadding_bytes[4];
    								message[57]= ppadding_bytes[5]; 
    								message[58]= ppadding_bytes[6]; 
    								message[59]= ppadding_bytes[7];
    								message[60]= ppadding_bytes[8];
    								message[61]= ppadding_bytes[9]; 
    								message[62]= ppadding_bytes[10]; 
    								message[63]= ppadding_bytes[11];
    								message[64]= ppadding_bytes[12];
    								message[65]= ppadding_bytes[13]; 
    								message[66]= ppadding_bytes[14]; 
    								message[67]= ppadding_bytes[15];
    								*/
    						
    						#else
    								message[0]= psender_id[3];
    								message[1]= psender_id[2]; 
    								message[2]= psender_id[1]; 
    								message[3]= psender_id[0];	
    							
    								message[4]= pencrypt_key_id[3];
    								message[5]= pencrypt_key_id[2]; 
    								message[6]= pencrypt_key_id[1]; 
    								message[7]= pencrypt_key_id[0];
    							
    								message[8]= pencrypt_size[3];
    								message[9]= pencrypt_size[2]; 
    								message[10]= pencrypt_size[1]; 
    								message[11]= pencrypt_size[0];
    								///		
    								message[12]= pcrc[3];
    								message[13]= pcrc[2]; 
    								message[14]= pcrc[1]; 
    								message[15]= pcrc[0];
    								///
    								message[16]= pdata_length[3];
    								message[17]= pdata_length[2]; 
    								message[18]= pdata_length[1]; 
    								message[19]= pdata_length[0];
    								msg_crc[0]=pdata_length[3];///////crc 1
    								msg_crc[1]=pdata_length[2];
    								msg_crc[2]=pdata_length[1];
    								msg_crc[3]=pdata_length[0];
    							
    								//printf("\nblabla Big_endian\n");
    							
    								message[20]= psequence_number[3];
    								message[21]= psequence_number[2]; 
    								message[22]= psequence_number[1]; 
    								message[23]= psequence_number[0];
    								msg_crc[4]=psequence_number[3];///////crc 2
    								msg_crc[5]=psequence_number[2];
    								msg_crc[6]=psequence_number[1];
    								msg_crc[7]=psequence_number[0];
    							
    								message[24]= ppacket_type[3];
    								message[25]= ppacket_type[2]; 
    								message[26]= ppacket_type[1]; 
    								message[27]= ppacket_type[0];
    								msg_crc[8]=ppacket_type[3];///////crc 3
    								msg_crc[9]=ppacket_type[2];
    								msg_crc[10]=ppacket_type[1];
    								msg_crc[11]=ppacket_type[0];
    							
    								message[28]= ps_read_number[3];
    								message[29]= ps_read_number[2]; 
    								message[30]= ps_read_number[1]; 
    								message[31]= ps_read_number[0];
    								msg_crc[12]=ps_read_number[3]; ///////crc 4
    								msg_crc[13]=ps_read_number[2];
    								msg_crc[14]=ps_read_number[1];
    								msg_crc[15]=ps_read_number[0];
    							
    									
    								message[32]= ps_card_number[15];
    								message[33]= ps_card_number[14]; 
    								message[34]= ps_card_number[13]; 
    								message[35]= ps_card_number[12];
    								message[36]= ps_card_number[11];
    								message[37]= ps_card_number[10]; 
    								message[38]= ps_card_number[9]; 
    								message[39]= ps_card_number[8];
    								message[40]= ps_card_number[7];
    								message[41]= ps_card_number[6]; 
    								message[42]= ps_card_number[5]; 
    								message[43]= ps_card_number[4];
    								message[44]= ps_card_number[3];
    								message[45]= ps_card_number[2]; 
    								message[46]= ps_card_number[1]; 
    								message[47]= ps_card_number[0];
    								msg_crc[16]= ps_card_number[15]; //crc 5
    								msg_crc[17]= ps_card_number[14]; 
    								msg_crc[18]= ps_card_number[13]; 
    								msg_crc[19]= ps_card_number[12];
    								msg_crc[20]= ps_card_number[11];
    								msg_crc[21]= ps_card_number[10]; 
    								msg_crc[22]= ps_card_number[9]; 
    								msg_crc[23]= ps_card_number[8];
    								msg_crc[24]= ps_card_number[7];
    								msg_crc[25]= ps_card_number[6]; 
    								msg_crc[26]= ps_card_number[5]; 
    								msg_crc[27]= ps_card_number[4];
    								msg_crc[28]= ps_card_number[3];
    								msg_crc[29]= ps_card_number[2]; 
    								msg_crc[30]= ps_card_number[1]; 
    								msg_crc[31]= ps_card_number[0];
    							
    								message[48]= ps_pin_code[3]; 
    								message[49]= ps_pin_code[2];
    								message[50]= ps_pin_code[1];
    								message[51]= ps_pin_code[0]; 				
    								msg_crc[32]=ps_pin_code[3]; //crc 6
    								msg_crc[33]=ps_pin_code[2];
    								msg_crc[34]=ps_pin_code[1];
    								msg_crc[35]=ps_pin_code[0]; 
    								
    								message[52]= ppadding_bytes[15];
    								message[53]= ppadding_bytes[14]; 
    								message[54]= ppadding_bytes[13]; 
    								message[55]= ppadding_bytes[12];
    								message[56]= ppadding_bytes[11];
    								message[57]= ppadding_bytes[10]; 
    								message[58]= ppadding_bytes[9]; 
    								message[59]= ppadding_bytes[8];
    								message[60]= ppadding_bytes[7];
    								message[61]= ppadding_bytes[6]; 
    								message[62]= ppadding_bytes[5]; 
    								message[63]= ppadding_bytes[4];
    								message[64]= ppadding_bytes[3];
    								message[65]= ppadding_bytes[2]; 
    								message[66]= ppadding_bytes[1]; 
    								message[67]= ppadding_bytes[0];
    						#endif
    							} // if(a)
    							else
    							{
    						#ifdef _LITTLE_ENDIAN
    						
    								message[12]= pcrc[0];
    								message[13]= pcrc[1]; 
    								message[14]= pcrc[2]; 
    								message[15]= pcrc[3];
    						
    								message[32]= ps_card_number[0];
    								message[33]= ps_card_number[1]; 
    								message[34]= ps_card_number[2]; 
    								message[35]= ps_card_number[3];
    								message[36]= ps_card_number[4];
    								message[37]= ps_card_number[5]; 
    								message[38]= ps_card_number[6]; 
    								message[39]= ps_card_number[7];
    								message[40]= ps_card_number[8];
    								message[41]= ps_card_number[9]; 
    								message[42]= ps_card_number[10]; 
    								message[43]= ps_card_number[11];
    								message[44]= ps_card_number[12];
    								message[45]= ps_card_number[13]; 
    								message[46]= ps_card_number[14]; 
    								message[47]= ps_card_number[15];
    						
    							#else
    						
    								message[32]= ps_card_number[15];
    								message[33]= ps_card_number[14]; 
    								message[34]= ps_card_number[13]; 
    								message[35]= ps_card_number[12];
    								message[36]= ps_card_number[11];
    								message[37]= ps_card_number[10]; 
    								message[38]= ps_card_number[9]; 
    								message[39]= ps_card_number[8];
    								message[40]= ps_card_number[7];
    								message[41]= ps_card_number[6]; 
    								message[42]= ps_card_number[5]; 
    								message[43]= ps_card_number[4];
    								message[44]= ps_card_number[3];
    								message[45]= ps_card_number[2]; 
    								message[46]= ps_card_number[1]; 
    								message[47]= ps_card_number[0];
    						
    								message[12]= pcrc[3]; //GOOD
    								message[13]= pcrc[2]; 
    								message[14]= pcrc[1]; 
    								message[15]= pcrc[0];
    						
    							#endif
    						
    							}
    						}
    
    						.....
    						.....
    						
    						DLL_API(long) vgt_init_S3100 ( char* remote_host, int remote_port, int active_door, int seq_number)
    						{
    						
    							if (!alreadyinit)  
    							init_structptr();
    							
    							glremote_ip=remote_host;  //090407 ATTENTION APRES INIT
    							glremote_port =remote_port;
    							allmsg.read_number = active_door;	
    							allmsg.sequence_number=seq_number;
    							write_struct(1);
    							init_struct =true;
    							
    							return 0 ;
    						}
    
    						
    						.....
    						.....
    						
    						
    						
    						DLL_API(long) vgt_sendto_S3100	(__int64 badgeid, char* remote_host, int remote_port)
    						{
    						//allmsg.card_number=(unsigned char*)&badgeid;
    						//ps_card_number=(unsigned char*)badge_id;
    						unsigned int crc32=0;
    						temp_badge_id = badgeid;
    						memset(message,0,MAX_PATH);
    						//message[32]= ps_card_number[15];
    						
    						
    						//allmsg.card_number=(unsigned char*)&temp_badge_id;
    						
    						
    								allmsg.card_number[15]=ptemp_badge_id[15];  //ICIBUG
    								allmsg.card_number[14]=ptemp_badge_id[14];
    								allmsg.card_number[13]=ptemp_badge_id[13]; 
    								allmsg.card_number[12]=ptemp_badge_id[12];
    								allmsg.card_number[11]=ptemp_badge_id[11];
    								allmsg.card_number[10]=ptemp_badge_id[10]; 
    								allmsg.card_number[9]=ptemp_badge_id[9]; 
    								allmsg.card_number[8]=ptemp_badge_id[8];
    								allmsg.card_number[7]=ptemp_badge_id[7];
    								allmsg.card_number[6]=ptemp_badge_id[6]; 
    								allmsg.card_number[5]=ptemp_badge_id[5]; 
    								allmsg.card_number[4]=ptemp_badge_id[4];
    								allmsg.card_number[3]=ptemp_badge_id[3];
    								allmsg.card_number[2]=ptemp_badge_id[2]; 
    								allmsg.card_number[1]=ptemp_badge_id[1]; 
    								allmsg.card_number[0]=ptemp_badge_id[0];
    						
    						
    						/*
    						 char digits[30];
    						 sprintf(digits,"%I64d\n",temp_badge_id);
    						//I64d
    						for(int i=0; i<20; i++)
    						
    						printf ("digits[%d] = %d ", i, digits[i]);
    						
    						//allmsg.card_number=(unsigned char*)&digits;
    								allmsg.card_number[15]=digits[15];  //ICIBUG
    								allmsg.card_number[14]=digits[14];
    								allmsg.card_number[13]=digits[13]; 
    								allmsg.card_number[12]=digits[12];
    								allmsg.card_number[11]=digits[11];
    								allmsg.card_number[10]=digits[10]; 
    								allmsg.card_number[9]=digits[9]; 
    								allmsg.card_number[8]=digits[8];
    								allmsg.card_number[7]=digits[7];
    								allmsg.card_number[6]=digits[6]; 
    								allmsg.card_number[5]=digits[5]; 
    								allmsg.card_number[4]=digits[4];
    								allmsg.card_number[3]=digits[3];
    								allmsg.card_number[2]=digits[2]; 
    								allmsg.card_number[1]=digits[1]; 
    								allmsg.card_number[0]=digits[0];
    						*/
    						
    						
    						printf("\nvaleur de temp_badge_id (Lu)= %Lu\n", temp_badge_id);
    						
    						printf("\nvaleur de *ptemp_badge_id apres  (s)= %s\n", (unsigned char*)ptemp_badge_id);
    						
    						printf("\nvaleur de *ptemp_badge_id apres (c) = %c\n");
    						for (int i=0;i<16;++i)
    						{
    						printf("%c, ", (unsigned char*)ptemp_badge_id[i]);
    						}
    						printf("\n");
    						//gbadge_id = badgeid;
    						//allmsg.card_number= badgeid;
    						
    						//allmsg.card_number=(unsigned char*)&temp_badge_id;
    						/*
    										allmsg.card_number[0]=(((unsigned char)&badgeid) & 0x00000000000000FF); //GOOD TOO
    										allmsg.card_number[1]= (unsigned char)(&badgeid & 0x000000000000FF); //GOOD TOO
    										allmsg.card_number[2]= (unsigned char)(&badgeid & 0x0000000000FF); //GOOD TOO
    										allmsg.card_number[3]= (unsigned char)(&badgeid & 0x00000000FF); //GOOD TOO
    										allmsg.card_number[4]= (unsigned char)(&badgeid & 0x000000FF); //GOOD TOO
    										allmsg.card_number[5]= (unsigned char)(&badgeid & 0x0000FF); //GOOD TOO
    										allmsg.card_number[6]= (unsigned char)(&badgeid & 0x00FF); //GOOD TOO
    										allmsg.card_number[7]= (unsigned char)(&badgeid & 0xFF); //GOOD TOO
    						*/
    						
    						//temp_badge_id  =(__int64)&badgeid;
    						
    						//ptemp_badge_id = __ui64toa( badgeid, temparray, 10 );
    						
    						//ptemp_badge_id=(unsigned char*)&temp_badge_id;
    						
    						//allmsg.card_number[0]=&temp_badge_id
    						
    						
    						//if (!alreadyinit)
    						//init_structptr();
    						
    						////////////////////////////:
    						
    						/*
    							message[32]= ps_card_number[15];
    							message[33]= ps_card_number[14]; 
    							message[34]= ps_card_number[13]; 
    							message[35]= ps_card_number[12];
    							message[36]= ps_card_number[11];
    							message[37]= ps_card_number[10]; 
    							message[38]= ps_card_number[9]; 
    							message[39]= ps_card_number[8];
    							message[40]= ps_card_number[7];
    							message[41]= ps_card_number[6]; 
    							message[42]= ps_card_number[5]; 
    							message[43]= ps_card_number[4];
    							message[44]= ps_card_number[3];
    							message[45]= ps_card_number[2]; 
    							message[46]= ps_card_number[1]; 
    							message[47]= ps_card_number[0];
    						*/
    						
    						//allmsg.card_number= (unsigned char*)&badgeid;
    						
    						/*
    								allmsg.card_number[15]=ptemp_badge_id[15];  //ICIBUG
    								allmsg.card_number[14]=ptemp_badge_id[14];
    								allmsg.card_number[13]=ptemp_badge_id[13]; 
    								allmsg.card_number[12]=ptemp_badge_id[12];
    								allmsg.card_number[11]=ptemp_badge_id[11];
    								allmsg.card_number[10]=ptemp_badge_id[10]; 
    								allmsg.card_number[9]=ptemp_badge_id[9]; 
    								allmsg.card_number[8]=ptemp_badge_id[8];
    								allmsg.card_number[7]=ptemp_badge_id[7];
    								allmsg.card_number[6]=ptemp_badge_id[6]; 
    								allmsg.card_number[5]=ptemp_badge_id[5]; 
    								allmsg.card_number[4]=ptemp_badge_id[4];
    								allmsg.card_number[3]=ptemp_badge_id[3];
    								allmsg.card_number[2]=ptemp_badge_id[2]; 
    								allmsg.card_number[1]=ptemp_badge_id[1]; 
    								allmsg.card_number[0]=ptemp_badge_id[0];
    						*/
    						
    						
    						memset(message,0,MAX_PATH);
    						write_struct(1);   //090406 (1) -> (0) -> 1 ok // plus bas
    						
    						initcrcok =0 ;
    						initcrcok = objcrc32.Init(CRC32_POLY_WINZIP);
    						
    						rescrc32=0;
    						
    						rescrc32= objcrc32.GetCrc32( &msg_crc,CRC_LENGTH ); //objcrc32. 
    						
    						//allmsg.crc=rescrc32; 
    						*pcrc=rescrc32; //090405
    						//write_struct(0);  090408
    						#ifdef _LITTLE_ENDIAN
    						
    										message[12]= pcrc[0];
    										message[13]= pcrc[1]; 
    										message[14]= pcrc[2]; 
    										message[15]= pcrc[3];
    						
    						/*				message[12]= (char)(rescrc32 & 0x000000FF); //GOOD TOO
    										message[13]= (char)((rescrc32>>8) & 0x0000FF);
    										message[14]= (char)((rescrc32>>16) & 0x00FF);
    										message[15]= (char)(rescrc32>>24);
    						*/
    						#else
    						
    										message[12]= pcrc[3]; //GOOD
    										message[13]= pcrc[2]; 
    										message[14]= pcrc[1]; 
    										message[15]= pcrc[0];
    						
    										message[15]= (char)(rescrc32 & 0x000000FF); //GOOD TOO
    										message[14]= (char)((rescrc32>>8) & 0x0000FF);
    										message[13]= (char)((rescrc32>>16) & 0x00FF);
    										message[12]= (char)(rescrc32>>24);
    						#endif
    						
    						//write_struct(0); //ICIBUG
    						
    						/*		
    								allmsg.card_number[14]=ptemp_badge_id[14];
    								allmsg.card_number[13]=ptemp_badge_id[13]; 
    								allmsg.card_number[12]=ptemp_badge_id[12];
    								allmsg.card_number[11]=ptemp_badge_id[11];
    								allmsg.card_number[10]=ptemp_badge_id[10]; 
    								allmsg.card_number[9]=ptemp_badge_id[9]; 
    								allmsg.card_number[8]=ptemp_badge_id[8];
    								allmsg.card_number[7]=ptemp_badge_id[7];
    								allmsg.card_number[6]=ptemp_badge_id[6]; 
    								allmsg.card_number[5]=ptemp_badge_id[5]; 
    								allmsg.card_number[4]=ptemp_badge_id[4];
    								allmsg.card_number[3]=ptemp_badge_id[3];
    								allmsg.card_number[2]=ptemp_badge_id[2]; 
    								allmsg.card_number[1]=ptemp_badge_id[1]; 
    								allmsg.card_number[0]=ptemp_badge_id[0];
    						*/
    						
    						/*
    						printf("containe card_number\n", allmsg.card_number);
    							for(int i=0; i < 15; ++i)
    							{
    							printf("%2d [0x%2.2X], [0x%2.2X]\n", i,allmsg.card_number[i],ptemp_badge_id[i]);
    							}
    						*/
    						printf("Sender ID = %d\n", allmsg.sender_id);
    							for(int i=0; i < 4; ++i)
    							{
    							printf("%2d[0x%2.2X], ", i, (unsigned char)message[i]);
    							}
    						printf("\nEncryption key ID = %d\n", allmsg.encrypt_key_id);
    							for(int i=0; i < 4; ++i)
    							{
    							printf("%2d[0x%2.2X], ", i, (unsigned char)message[4+i]); 
    							}
    						printf("\nEncrypted size = %d\n", allmsg.encrypt_size);
    							for(int i=0; i < 4; ++i)
    							{
    							printf("%2d[0x%2.2X], ", i, (unsigned char)message[8+i]);
    							}
    						
    						printf("\nCRC-32 = %u\n",rescrc32);
    							for(int i=0; i < 4; ++i)
    							{
    							printf("%2d[0x%2.2X], ", i, (unsigned char)message[12+i]);
    							}
    						printf("\nData length = %d\n", allmsg.data_length);
    							for(int i=0; i < 4; ++i)
    							{
    							printf("%2d[0x%2.2X], ", i, (unsigned char)message[16+i]);
    							}
    						
    						printf("\nSequence number = %d\n", allmsg.sequence_number);
    							for(int i=0; i < 4; ++i)
    							{
    							printf("%2d[0x%2.2X], ", i, (unsigned char)message[20+i]);
    							}
    						
    						printf("\nPacket type = %d\n", allmsg.packet_type);
    							for(int i=0; i < 4; ++i)
    							{
    							printf("%2d[0x%2.2X], ", i, (unsigned char)message[24+i]);
    							}
    						
    						printf("\ndata: READER NUMBER = %d\n", allmsg.read_number);
    							for(int i=0; i < 4; ++i)
    							{
    							printf("%2d[0x%2.2X], ", i, (unsigned char)message[28+i]);
    							}
    						//printf("\ndata: CARD NUMBER = %s\n", allmsg.card_number);   printf("%s\n", _i64toa(987654321234567890LL, bin, 2)); 
    						printf("\ndata: CARD NUMBER = %Ld\n", temp_badge_id ); //gbadge_id //094008 remplacer paer temp_badge_id pui 
    							for(int i=0; i < 16; ++i)
    							{
    							printf("%2d[0x%2.2X], ", i, (unsigned char)message[32+i]);
    							if (i==14)printf("\n");
    							}
    						printf("\ndata: PIN CODE = %d\n", allmsg.pin_code);
    							for(int i=0; i < 4; ++i)
    							{
    							printf("%2d[0x%2.2X], ", i, (unsigned char)message[48+i]);
    							}
    						
    						
    						printf("\n\nCOMPLETE MESSAGE:\n");
    						
    						for(int i=0; i < 52; ++i) //090407 68 -> 52 disable padding bytes
    						{
    							printf("%2d[0x%2.2X], ", i, (unsigned char)message[i]);
    							if (i==14||i==29||i==44||i==59) printf("\n");
    						}
    						
    						char *remote_ip=remote_host;
    						
    						if (!error_code) 
    						{	error_code =  pCVgtNotifyDll->vgt_client_open (&pCUdpClt, remote_ip,remote_port);
    							if (error_code) 
    							{	printf("dans if(error_code) \n");
    								if (DEBUG) fprintf(stderr," ERROR: udp client open returned error_code %ld \n",error_code);
    								sprintf(response,"error_code= %d",ERROR_NOTIFY_CLIENT_OPEN);
    							}
    						}
    						
    						
    						if (!error_code) 
    						{
    							error_code =  pCVgtNotifyDll->vgt_client_send_to(&pCUdpClt, message,68); //68 -> 66
    							if (error_code) 
    							{
    								if (DEBUG) fprintf(stderr," ERROR: udp client send to returned error_code %ld \n",error_code);
    								sprintf(response,"error_code= %d",ERROR_NOTIFY_CLIENT_SEND_TO);
    							}
    						}
    							
    						printf("\nWait for response... \n");
    						if (!error_code) 
    						{
    							error_code =  pCVgtNotifyDll->vgt_client_wait_response (&pCUdpClt, (char *)response, MAX_PATH, &response_length, timeout_ms); //090406
    							if (error_code) 
    							{
    								if (DEBUG) fprintf(stderr," ERROR: udp client wait response returned error_code %ld \n",error_code);
    								sprintf(response,"error_code= %d",ERROR_NOTIFY_CLIENT_WAIT_RESPONSE);
    							}
    						}
    						
    						if (!error_code) 
    						{
    							error_code =  pCVgtNotifyDll->vgt_client_close (&pCUdpClt);
    							if (error_code) 
    							{
    								if (DEBUG) fprintf(stderr," ERROR: udp client close returned error_code %ld \n",error_code);
    								sprintf(response,"error_code= %d",ERROR_NOTIFY_CLIENT_CLOSE);
    							}
    						}
    						
    						
    						for(int i=0; i < response_length; ++i)
    						{
    						printf("%2d[0x%2.2X], ",i, (unsigned char)response[i]);
    						if (i==14||i==29||i==44||i==59) printf("\n");
    						
    						}
    						
    						
    						init_struct=true;
    						return error_code;
    						}
    						
    						
    						DLL_API(long) vgt_dest_S3100()
    						{
    						error_code=0;
    						
    						free(response);
    						printf("\n\nExiting...\n");
    						delete pCrc32;
    						
    						printf("\n");
    						
    						return error_code;
    						}
    And in the call file call_S3100.cpp (main)
    Code:
    
    
    					int main(int argc, char* argv[])
    					{	
    					
    						unsigned int rescrc32=0;
    	
    						char remote_ip[16];
    						int remote_port=0; 
    						unsigned int act_doors=1;
    											
    						__int64 badge_id=0;
    						int seq_num=1;
    						
    						....
    						....
    						
    						
    					error_code = pCVgtS3100_interface->vgt_init_S3100( remote_ip, remote_port, act_doors, seq_num);//IMPL_1
    					if (error_code) 
    					{
    						if (DEBUG) fprintf(stderr," ERROR: S3100 init returned error_code %ld \n",error_code);
    						sprintf(response,"error_code= %d",ERROR_S3100_INTERFACE_INIT);
    					}
    					printf("\n");
    					
    						
    						
    						.....
    						.....
    
    					error_code =pCVgtS3100_interface->vgt_sendto_S3100(badge_id,remote_ip,remote_port);
    					if (error_code) 
    					{
    						if (DEBUG) fprintf(stderr," ERROR: S3100 send to returned error_code %ld \n",error_code);
    						sprintf(response,"error_code= %d",ERROR_S3100_INTERFACE_SEND_TO);
    					}	
    					
    						.....
    In resume the problem is to use good way to pass an __int64 to an unsigned char of [16] . Perhaps us mask :

    Code:
    allmsg.card_number[0]=(((unsigned char)&badgeid) & 0x00000000000000FF); 
    allmsg.card_number[1]= (unsigned char)(&badgeid & 0x000000000000FF); 
    allmsg.card_number[2]= (unsigned char)(&badgeid & 0x0000000000FF);
    allmsg.card_number[3]= (unsigned char)(&badgeid & 0x00000000FF); allmsg.card_number[4]= (unsigned char)(&badgeid & 0x000000FF); 
    allmsg.card_number[5]= (unsigned char)(&badgeid & 0x0000FF); allmsg.card_number[6]= (unsigned char)(&badgeid & 0x00FF); allmsg.card_number[7]= (unsigned char)(&badgeid & 0xFF);
    is this correct and how aboute the next [8] to [16] ?
    how fix these problems?

    Could you help me please.

    Thank you

    Kenzo
  • Kenzogio
    New Member
    • Apr 2009
    • 16

    #2
    Ok,

    So for resuming all I wrote before.

    I have this struct :

    struct frame_message {
    //typedef struct {
    unsigned int sender_id;
    unsigned int encrypt_key_id;
    unsigned int encrypt_size;
    unsigned int crc;
    unsigned int data_length;
    unsigned int sequence_number ;
    unsigned int packet_type;
    unsigned int read_number;
    unsigned char card_number[16];/////
    unsigned int pin_code;
    unsigned char padding_bytes[MAX_PADDING];
    }allmsg;

    And I pass to allmsg.card_num ber an _int64 but it is not good way
    fonction pass(_int64 badgeid)
    {

    allmsg.card_num ber=(unsigned char*)&badgeid; //erreur incompatible signes assignement of (unsigned char* to unsigned char [16]

    ...


    }
    Could you fix me please ?

    Thank you

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      __int64 is 8 bytes long,so I don't see why are you trying to fill a 16byte array with 8 byte number?Unless you want to do something like this:

      Code:
      __int64 number=9223372036854775807 //2^63-1--> it's just an example
      char Array[20]="";//but then you need 20 byte array as you can see
      and you would like your array to become:"9223372 036854775807"

      Code:
      	//Then you can use _i64toa
      	_i64toa(number,Array,10);
      Is this what you want?

      Comment

      • Kenzogio
        New Member
        • Apr 2009
        • 16

        #4
        Thank you Savage,

        Exactly, I want : the contain in _int64 must be the same (but in bytes) format in card_number[16] .

        For example when I display containt of my variables:

        //(printf of temp_badge_id )
        data: CARD NUMBER = 123456789

        //(printf for of allmsg.card_num ber[] )
        0[0x00], 1[0x00], 2[0x00], 3[0x00], 4[0xB7], 5[0xFA], 6[0x09], 7[0xC8], 8[0x00], 9[0x00], 10[0x00], 11[0x00], 12[0x07], 13[0x5B], 14[0xCD],
        15[0x15],
        Like you see long long int: 123456789 in hex is 75BCD15 (bytes 12 -> 15 )
        but what is contain of byes 4 -> 7 ?? strange

        I think that problems come before from here:
        allmsg.card_num ber[15]=ptemp_badge_id[15];
        allmsg.card_num ber[14]=ptemp_badge_id[14];
        ...
        ..

        allmsg.card_num ber[0]=ptemp_badge_id[0];
        But I'm not sure ?
        So maybe iproblem is to pass correctly __int64 to unsigned char [] of 16 bytes

        Thank you

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Have you tried a simple memcopy?

          Code:
          __int64 number=0x75BCD15;
          char Array[16]="";
          memcopy(Array,&number,8);
          It should do what you want..

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            SO the card_number is a char (8 bits) and you want to put an _int64 (64 bits) in the char? I assume the value in the _int64 will fit in the char. Next since char is an integer an _int64 is an integer, can you not just assign the _Int64 to the char?

            There should be no need for a memcpy(). In fact, that could screw things up when your _int64 has a 2's complement value in it.

            Comment

            • Kenzogio
              New Member
              • Apr 2009
              • 16

              #7
              Hi,
              Thanks in fact the goal is that with some type of long long integer I must fit the entered value in exactly 16 bytes of char (or 128 bits) ... So I choice to use an _int64 but perhaps is there another type of long integer which is in 16 bytes just like may char array, and so with this one it will be more easy I think, could you advise me another solution ?

              But for the moment, I use an _int64 and your solution with memcopy() I try this but compiler says some error message( memcopy() is not in the scope) so I must include an header file but which one ?

              Thank you

              Kenzo

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #8
                Originally posted by Kenzogio
                Hi,
                Thanks in fact the goal is that with some type of long long integer I must fit the entered value in exactly 16 bytes of char (or 128 bits) ... So I choice to use an _int64 but perhaps is there another type of long integer which is in 16 bytes just like may char array, and so with this one it will be more easy I think, could you advise me another solution ?

                But for the moment, I use an _int64 and your solution with memcopy() I try this but compiler says some error message( memcopy() is not in the scope) so I must include an header file but which one ?

                Thank you

                Kenzo
                That's because it's memcpy() and not memcopy(), I've made a typo :P

                Weakness, could you please explain how is it that it will cause problems when there are two complements?

                Comment

                • Kenzogio
                  New Member
                  • Apr 2009
                  • 16

                  #9
                  Hi,

                  i think that I tried too with memcpy() but anyway I will try again.

                  So what you mean when saying "..when there are two complements" ?
                  If I well understand I explained the fact that if it's possible to use a more big int type than _int64 so exactly a 16 bytes types for example I don't know "long double int" and to get contain of it into my 16 bytes char 'allmsg.number_ card[x]`' and how implement this conversion type (e.g. with masks, modulo, .. )


                  Thank you

                  Comment

                  • Kenzogio
                    New Member
                    • Apr 2009
                    • 16

                    #10
                    Hi,

                    Sorry yes I try with memcpy() and it's ok cool.

                    But now my problem is to use a much bigger (size * 2) variable than the __int64 to fit correctly data into my 16 bytes array.

                    Thank you

                    Comment

                    • Savage
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1759

                      #11
                      Originally posted by Kenzogio
                      Hi,

                      Sorry yes I try with memcpy() and it's ok cool.

                      But now my problem is to use a much bigger (size * 2) variable than the __int64 to fit correctly data into my 16 bytes array.

                      Thank you
                      What's wrong with current alignment in 16 byte array?
                      Have you tried rotating the array or shifting the i__nt64 to make it fit as you want it to fit?

                      Comment

                      • Kenzogio
                        New Member
                        • Apr 2009
                        • 16

                        #12
                        the problem is the size. I have a 16 byte array. and I must find a variable with 16 bytes too. Because the __int64 is only a 8 byte int and what must a store in the rest of my 8 bytes array?? I must use an integer which is more big than a simple _int64 for use efficiently the size.

                        Thank you

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          There is no integer that I know of that has 128 bit, but additionally to that reading this thread it sounds like you do not really know properly how you are actually using your 16 byte array.

                          Do you really want to hold a value in the range -170141183460469 231731687303715 884105728 to 170141183460469 231731687303715 884105727 or are you in fact trying to hold a 16 digit number or do you have something else entirely in mind?

                          In the 2nd case then either store each digit separately in ASCII form in your array of 16 char or just use a single __int64 which is more than capable of holding any 16 digit decimal number and forget about your array. If you want to convert from an __int64 to an array of 16 char then use an intermediate array of 17 char (because you need space for the null terminator) and sprintf (or _i64toa) the __int64 into the array of 17 char and then memcpy the first 16 char from that array into you array of 16 char.

                          However I suspect that we are going to have trouble helping you find a solution until you can describe exactly what it is this data is supposed to contain.

                          Comment

                          • Kenzogio
                            New Member
                            • Apr 2009
                            • 16

                            #14
                            Hi,

                            For more details, it's about a data packet to send. this packet containt severals "FIELD NAME" with her "FIELD SIZE" and "REMARKS"

                            e.g:
                            Code:
                            [U]"FIELD NAME"             |        "FIELD SIZE"        |    REMARKS  [/U]
                            Sender ID                |    4 bytes                 | identifies the source system to the destination system. 
                            ...
                            
                            Card Number             |  16 byte                   | The card number is an 128bit number  in  binary format.
                            
                            ....
                            One of them it's the 'Card Number' and like I see in remarks I understand that it is the first case you wrote so holding a value in the range isn't it ?
                            .

                            Thank you

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #15
                              Yes that's right, now we are getting somewhere. What you have posted does not say is if that 128bit integer is signed or unsigned. It makes a difference because unsigned large numbers are marginally easier to deal with rather than a range of -170141183460469 231731687303715 884105728 to 170141183460469 231731687303715 884105727 you would be looking at a range of 0 - 340282366920938 463463374607431 768211455.

                              If we work all this in unsigned binary using 64bit integers than you can think of your 128bit number actually as a 2 digit base 184467440737095 51616 (2^64) number. That is you use 2 unsigned 64 bit integers to hold your value. You can easily output the value in hexadecimal this way because it is just the hexadecimal representation of the most significant digit (MSD) followed by the hexadecimal representation of the least significant digit (LSD).

                              This works as long as you are just transferring data between a source and a destination. If you need to perform calculations on the data it comes a little more complex because you need to be able to hold carries between digits however this can be relatively easily got round by holding you 128 bit number as a 4 digit base 4294967296 (2^32) number. You perform operations on the 32bit digits using 64bit arithmetic then any carries between digits appear in the top 32 bits of the results.

                              Because of the way that 2s complement arithmetic works if you are dealing with signed values you still used unsigned digits in your numbers and the maths should still work.

                              So you need either 2 unsigned __int64 or 4 unsigned long to hold your data. The question then becomes how to transfer that to the 16 unsigned character array.

                              memcpy is a very very bad way of doing this, the problem is that different computer systems and protocols use different endians, that is the order that the bytes are stored in memory. To transfer data properly and in a portable manner you have to know the endian of both the source (your computer which looks like it's a Windows PC and would be little endian in that case) and destination (the protocol packet you are creating).

                              The only truly safe and portable way to then copy the data is to extract each byte individually from the source (using the bitwise operators) and insert it into the correct location in the protocol packet. If this is a network protocol and you have pulled in the WinSock library then you may be able to use the functions htonl, htons, ntohl and ntohs if you store the data using unsigned longs rather than unsigned __int64.

                              Comment

                              Working...