By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

how pass an __int64 to unsigned char [16]

P: 16
Hi,

I have a struct "allmsg" and him member :
Expand|Select|Wrap|Line Numbers
  1.  unsigned char card_number[MAX_CARD_NUMBER]; //16
  2.  
allmsg.card_number


And I must store contain of an __int64 into this member

Expand|Select|Wrap|Line Numbers
  1.     __int64 temp_badge_id;
  2.     unsigned char *ptemp_badge_id; //
  3.  

So I pass value to this last :

Expand|Select|Wrap|Line Numbers
  1.  
  2. DLL_API(long) vgt_sendto_S3100(__int64 badgeid, char* remote_host, int remote_port)
  3. {
  4.                         //allmsg.card_number=(unsigned char*)badgeid; //ERROR
  5.  
  6.     unsigned int crc32=0;
  7.                         temp_badge_id = badgeid; //here passing value OK
  8.  
  9.  

And the code above here: seems ok but the contains of allmsg.cardnumber
is not the exactly the same than the _int64
Expand|Select|Wrap|Line Numbers
  1.  
  2. allmsg.card_number[15]=ptemp_badge_id[15];  //not good way
  3. allmsg.card_number[14]=ptemp_badge_id[14];
  4. ....
  5. ...
  6. allmsg.card_number[0]=ptemp_badge_id[0];
  7.  
  8.  
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 :

Expand|Select|Wrap|Line Numbers
  1.  
  2. #define MAX_CARD_NUMBER 16
  3. #define MAX_PADDING 16
  4.  
  5. #define CRC_LENGTH 36
  6. #define CRC_LENGTH_RETURN 16
  7.  
  8. struct frame_message { 
  9. unsigned int sender_id;
  10.                           ..
  11. unsigned char card_number[MAX_CARD_NUMBER];//crcin
  12.                         ..
  13. unsigned char padding_bytes[MAX_PADDING];
  14. }allmsg; 
  15.  
  16. ..
  17.  
  18. unsigned char *ps_card_number;
  19.  
  20. ..
  21.  
  22.  
in the other file S3100.cpp (dll) , I have this :
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include "S3100.h"
  3.  
  4. ..
  5. #define PACKET_LENGTH 68 //with padding bytes reserved
  6.  
  7. #define CRC_LENGTH 36
  8.  
  9. .........
  10. .........
  11. ////IMPLEMENTATION CRC CLASS/////
  12.  =======================================================================================
  13.                         // Fonction: Crc32::Crc32()
  14.                         // =======================================================================================
  15.  
  16.                         Crc32::Crc32()
  17.                         {
  18.                             memset( &this->crc32_table, 0, sizeof(unsigned int)*256 );
  19.                             this->polynomial = 0;
  20.                         }        
  21.                         // =======================================================================================
  22.  
  23.                         ........
  24.                         ........
  25.  
  26.                         //VARIABLES GENERALES ////
  27.                         long error_code=0;        
  28.                         bool DEBUG=true;
  29.                         bool initcrcok=false;
  30.                         unsigned int rescrc32=0;
  31.                         char msg_crc[CRC_LENGTH];        // message to send
  32.  
  33.                         char message[MAX_PATH];            // message to send
  34.                         //char response[MAX_PATH];        // response to receive  //090406
  35.                         char* response = (char*)malloc(1024*sizeof(char));  
  36.                         int message_length=68;            // length of the message to send
  37.                         int response_length=0;            
  38.                         int engine_id=0;                
  39.                         int analyse_id=0;    
  40.                         //int remote_port=0;    
  41.                         int timeout_ms=1000;            
  42.  
  43.                         char binary[80];
  44.  
  45.  
  46.                         char *glremote_ip=NULL;
  47.                         int glremote_port;
  48.  
  49.                         bool init_struct=false;
  50.  
  51.                         frame_message packet;
  52.                         Crc32 *pCrc32 = new Crc32();
  53.  
  54.                         __int64 temp_badge_id;
  55.                         char *ptemp_badge_id; //090408 enlever le unsigned
  56.  
  57.                         .......
  58.                         .......
  59.  
  60.  
  61.                         void init_structptr()
  62.                         {
  63.  
  64.                             memset(allmsg.card_number,0,MAX_CARD_NUMBER);
  65.                             memset(allmsg.padding_bytes,0,MAX_PADDING);
  66.  
  67.                             memset(message,0,MAX_PATH);
  68.                             message_length=0;
  69.                             memset(msg_crc,0,CRC_LENGTH);//crc
  70.  
  71.                             memset(response,0,MAX_PATH); //090406
  72.  
  73.                             psender_id = (unsigned char*)&(allmsg.sender_id);
  74.                             pencrypt_key_id = (unsigned char*)&allmsg.encrypt_key_id;
  75.                             pencrypt_size = (unsigned char*)&allmsg.encrypt_size;
  76.                             pcrc = (unsigned char*)&allmsg.crc;
  77.                             pdata_length = (unsigned char*)&allmsg.data_length;//crcin
  78.                             psequence_number = (unsigned char*)&allmsg.sequence_number;//crcin
  79.                             ppacket_type = (unsigned char*)&allmsg.packet_type;//crcin
  80.                             ps_read_number = (unsigned char*)&allmsg.read_number;//crcin
  81.                             ps_card_number = (unsigned char*)&allmsg.card_number; //crcin
  82.                             ps_pin_code = (unsigned char*)&allmsg.pin_code; //crcin
  83.                             ppadding_bytes = (unsigned char*)&allmsg.padding_bytes;
  84.  
  85.                             //allmsg.sender_id=0x0a0b0c0d;  //hexadecimal sur 4 bytes rmq: nous sommes en little endian
  86.                             //allmsg.sender_id=3;
  87.                             allmsg.sender_id=0;
  88.                             allmsg.encrypt_key_id=0;
  89.                             //allmsg.encrypt_size=0;
  90.                             allmsg.encrypt_size=40;
  91.                             allmsg.crc=0; //090406
  92.                             //allmsg.data_length=24; //crcin 1
  93.                             allmsg.data_length=52; //crcin 1
  94.                             //allmsg.sequence_number=12345; //crcin 2
  95.                             //allmsg.sequence_number=1; //090407 géré en mode saisie clavier
  96.                             allmsg.packet_type=1; //crcin 3
  97.                             //allmsg.read_number=2; //crcin 4 //090401
  98.                             //allmsg.read_number=1; //090407 géré en mode saisie clavier
  99.                             allmsg.pin_code=0; //crcin 6 //090404
  100.  
  101.                             alreadyinit =true;
  102.                             //allmsg.card_number=0;//0904007
  103.  
  104.                         ptemp_badge_id = (char*)&temp_badge_id; //090407
  105.  
  106.                         }
  107.  
  108.                         ......
  109.                         ......
  110.                         void write_struct(bool a)
  111.                         {
  112.  
  113.                             if (a)
  114.                             {    
  115.                         #ifdef _LITTLE_ENDIAN
  116.                                 message[0]= psender_id[0];
  117.                                 message[1]= psender_id[1]; 
  118.                                 message[2]= psender_id[2]; 
  119.                                 message[3]= psender_id[3];    
  120.  
  121.                                 message[4]= pencrypt_key_id[0];
  122.                                 message[5]= pencrypt_key_id[1]; 
  123.                                 message[6]= pencrypt_key_id[2]; 
  124.                                 message[7]= pencrypt_key_id[3];
  125.  
  126.                                 message[8]= pencrypt_size[0];
  127.                                 message[9]= pencrypt_size[1]; 
  128.                                 message[10]= pencrypt_size[2]; 
  129.                                 message[11]= pencrypt_size[3];
  130.                                 ///        
  131.                                 message[12]= pcrc[0];
  132.                                 message[13]= pcrc[1]; 
  133.                                 message[14]= pcrc[2]; 
  134.                                 message[15]= pcrc[3];
  135.                                 ///
  136.                                 message[16]= pdata_length[0];
  137.                                 message[17]= pdata_length[1]; 
  138.                                 message[18]= pdata_length[2]; 
  139.                                 message[19]= pdata_length[3];
  140.                                 msg_crc[0]=pdata_length[0];///////crc 1
  141.                                 msg_crc[1]=pdata_length[1];
  142.                                 msg_crc[2]=pdata_length[2];
  143.                                 msg_crc[3]=pdata_length[3];
  144.  
  145.                                 message[20]= psequence_number[0];
  146.                                 message[21]= psequence_number[1]; 
  147.                                 message[22]= psequence_number[2]; 
  148.                                 message[23]= psequence_number[3];
  149.                                 msg_crc[4]=psequence_number[0];///////crc 2
  150.                                 msg_crc[5]=psequence_number[1];
  151.                                 msg_crc[6]=psequence_number[2];
  152.                                 msg_crc[7]=psequence_number[3];
  153.  
  154.                                 message[24]= ppacket_type[0];
  155.                                 message[25]= ppacket_type[1]; 
  156.                                 message[26]= ppacket_type[2]; 
  157.                                 message[27]= ppacket_type[3];
  158.                                 msg_crc[8]=ppacket_type[0];///////crc 3
  159.                                 msg_crc[9]=ppacket_type[1];
  160.                                 msg_crc[10]=ppacket_type[2];
  161.                                 msg_crc[11]=ppacket_type[3];
  162.  
  163.                                 message[28]= ps_read_number[0];
  164.                                 message[29]= ps_read_number[1]; 
  165.                                 message[30]= ps_read_number[2]; 
  166.                                 message[31]= ps_read_number[3];
  167.                                 msg_crc[12]=ps_read_number[0]; ///////crc 4
  168.                                 msg_crc[13]=ps_read_number[1];
  169.                                 msg_crc[14]=ps_read_number[2];
  170.                                 msg_crc[15]=ps_read_number[3];
  171.  
  172.                                 for (int i=0; i<16 ;++i )
  173.                                 {
  174.                                 msg_crc[16+i]=message[32+i]= ps_card_number[i]; //crc 5
  175.                                 }/*
  176.                                 msg_crc[16]=message[32]= ps_card_number[0]; //crc 5
  177.                                 msg_crc[17]=message[33]= ps_card_number[1]; 
  178.                                 msg_crc[18]=message[34]= ps_card_number[2]; 
  179.                                 msg_crc[19]=message[35]= ps_card_number[3];
  180.                                 msg_crc[20]=message[36]= ps_card_number[4];
  181.                                 msg_crc[21]=message[37]= ps_card_number[5]; 
  182.                                 msg_crc[22]=message[38]= ps_card_number[6]; 
  183.                                 msg_crc[23]=message[39]= ps_card_number[7];
  184.                                 msg_crc[24]=message[40]= ps_card_number[8];
  185.                                 msg_crc[25]=message[41]= ps_card_number[9]; 
  186.                                 msg_crc[26]=message[42]= ps_card_number[10]; 
  187.                                 msg_crc[27]=message[43]= ps_card_number[11];
  188.                                 msg_crc[28]=message[44]= ps_card_number[12];
  189.                                 msg_crc[29]=message[45]= ps_card_number[13]; 
  190.                                 msg_crc[30]=message[46]= ps_card_number[14]; 
  191.                                 msg_crc[31]=message[47]= ps_card_number[15];
  192.                                 */
  193.  
  194.                                 msg_crc[32]=message[48]= ps_pin_code[0]; //crc 6
  195.                                 msg_crc[33]=message[49]= ps_pin_code[1];
  196.                                 msg_crc[34]=message[50]= ps_pin_code[2];
  197.                                 msg_crc[35]=message[51]= ps_pin_code[3]; 
  198.  
  199.                                 for (int i=0; i<16 ;++i )
  200.                                 {
  201.                                 message[52+i]= ppadding_bytes[i];
  202.                                 }/*
  203.                                 message[52]= ppadding_bytes[0];
  204.                                 message[53]= ppadding_bytes[1]; 
  205.                                 message[54]= ppadding_bytes[2]; 
  206.                                 message[55]= ppadding_bytes[3];
  207.                                 message[56]= ppadding_bytes[4];
  208.                                 message[57]= ppadding_bytes[5]; 
  209.                                 message[58]= ppadding_bytes[6]; 
  210.                                 message[59]= ppadding_bytes[7];
  211.                                 message[60]= ppadding_bytes[8];
  212.                                 message[61]= ppadding_bytes[9]; 
  213.                                 message[62]= ppadding_bytes[10]; 
  214.                                 message[63]= ppadding_bytes[11];
  215.                                 message[64]= ppadding_bytes[12];
  216.                                 message[65]= ppadding_bytes[13]; 
  217.                                 message[66]= ppadding_bytes[14]; 
  218.                                 message[67]= ppadding_bytes[15];
  219.                                 */
  220.  
  221.                         #else
  222.                                 message[0]= psender_id[3];
  223.                                 message[1]= psender_id[2]; 
  224.                                 message[2]= psender_id[1]; 
  225.                                 message[3]= psender_id[0];    
  226.  
  227.                                 message[4]= pencrypt_key_id[3];
  228.                                 message[5]= pencrypt_key_id[2]; 
  229.                                 message[6]= pencrypt_key_id[1]; 
  230.                                 message[7]= pencrypt_key_id[0];
  231.  
  232.                                 message[8]= pencrypt_size[3];
  233.                                 message[9]= pencrypt_size[2]; 
  234.                                 message[10]= pencrypt_size[1]; 
  235.                                 message[11]= pencrypt_size[0];
  236.                                 ///        
  237.                                 message[12]= pcrc[3];
  238.                                 message[13]= pcrc[2]; 
  239.                                 message[14]= pcrc[1]; 
  240.                                 message[15]= pcrc[0];
  241.                                 ///
  242.                                 message[16]= pdata_length[3];
  243.                                 message[17]= pdata_length[2]; 
  244.                                 message[18]= pdata_length[1]; 
  245.                                 message[19]= pdata_length[0];
  246.                                 msg_crc[0]=pdata_length[3];///////crc 1
  247.                                 msg_crc[1]=pdata_length[2];
  248.                                 msg_crc[2]=pdata_length[1];
  249.                                 msg_crc[3]=pdata_length[0];
  250.  
  251.                                 //printf("\nblabla Big_endian\n");
  252.  
  253.                                 message[20]= psequence_number[3];
  254.                                 message[21]= psequence_number[2]; 
  255.                                 message[22]= psequence_number[1]; 
  256.                                 message[23]= psequence_number[0];
  257.                                 msg_crc[4]=psequence_number[3];///////crc 2
  258.                                 msg_crc[5]=psequence_number[2];
  259.                                 msg_crc[6]=psequence_number[1];
  260.                                 msg_crc[7]=psequence_number[0];
  261.  
  262.                                 message[24]= ppacket_type[3];
  263.                                 message[25]= ppacket_type[2]; 
  264.                                 message[26]= ppacket_type[1]; 
  265.                                 message[27]= ppacket_type[0];
  266.                                 msg_crc[8]=ppacket_type[3];///////crc 3
  267.                                 msg_crc[9]=ppacket_type[2];
  268.                                 msg_crc[10]=ppacket_type[1];
  269.                                 msg_crc[11]=ppacket_type[0];
  270.  
  271.                                 message[28]= ps_read_number[3];
  272.                                 message[29]= ps_read_number[2]; 
  273.                                 message[30]= ps_read_number[1]; 
  274.                                 message[31]= ps_read_number[0];
  275.                                 msg_crc[12]=ps_read_number[3]; ///////crc 4
  276.                                 msg_crc[13]=ps_read_number[2];
  277.                                 msg_crc[14]=ps_read_number[1];
  278.                                 msg_crc[15]=ps_read_number[0];
  279.  
  280.  
  281.                                 message[32]= ps_card_number[15];
  282.                                 message[33]= ps_card_number[14]; 
  283.                                 message[34]= ps_card_number[13]; 
  284.                                 message[35]= ps_card_number[12];
  285.                                 message[36]= ps_card_number[11];
  286.                                 message[37]= ps_card_number[10]; 
  287.                                 message[38]= ps_card_number[9]; 
  288.                                 message[39]= ps_card_number[8];
  289.                                 message[40]= ps_card_number[7];
  290.                                 message[41]= ps_card_number[6]; 
  291.                                 message[42]= ps_card_number[5]; 
  292.                                 message[43]= ps_card_number[4];
  293.                                 message[44]= ps_card_number[3];
  294.                                 message[45]= ps_card_number[2]; 
  295.                                 message[46]= ps_card_number[1]; 
  296.                                 message[47]= ps_card_number[0];
  297.                                 msg_crc[16]= ps_card_number[15]; //crc 5
  298.                                 msg_crc[17]= ps_card_number[14]; 
  299.                                 msg_crc[18]= ps_card_number[13]; 
  300.                                 msg_crc[19]= ps_card_number[12];
  301.                                 msg_crc[20]= ps_card_number[11];
  302.                                 msg_crc[21]= ps_card_number[10]; 
  303.                                 msg_crc[22]= ps_card_number[9]; 
  304.                                 msg_crc[23]= ps_card_number[8];
  305.                                 msg_crc[24]= ps_card_number[7];
  306.                                 msg_crc[25]= ps_card_number[6]; 
  307.                                 msg_crc[26]= ps_card_number[5]; 
  308.                                 msg_crc[27]= ps_card_number[4];
  309.                                 msg_crc[28]= ps_card_number[3];
  310.                                 msg_crc[29]= ps_card_number[2]; 
  311.                                 msg_crc[30]= ps_card_number[1]; 
  312.                                 msg_crc[31]= ps_card_number[0];
  313.  
  314.                                 message[48]= ps_pin_code[3]; 
  315.                                 message[49]= ps_pin_code[2];
  316.                                 message[50]= ps_pin_code[1];
  317.                                 message[51]= ps_pin_code[0];                 
  318.                                 msg_crc[32]=ps_pin_code[3]; //crc 6
  319.                                 msg_crc[33]=ps_pin_code[2];
  320.                                 msg_crc[34]=ps_pin_code[1];
  321.                                 msg_crc[35]=ps_pin_code[0]; 
  322.  
  323.                                 message[52]= ppadding_bytes[15];
  324.                                 message[53]= ppadding_bytes[14]; 
  325.                                 message[54]= ppadding_bytes[13]; 
  326.                                 message[55]= ppadding_bytes[12];
  327.                                 message[56]= ppadding_bytes[11];
  328.                                 message[57]= ppadding_bytes[10]; 
  329.                                 message[58]= ppadding_bytes[9]; 
  330.                                 message[59]= ppadding_bytes[8];
  331.                                 message[60]= ppadding_bytes[7];
  332.                                 message[61]= ppadding_bytes[6]; 
  333.                                 message[62]= ppadding_bytes[5]; 
  334.                                 message[63]= ppadding_bytes[4];
  335.                                 message[64]= ppadding_bytes[3];
  336.                                 message[65]= ppadding_bytes[2]; 
  337.                                 message[66]= ppadding_bytes[1]; 
  338.                                 message[67]= ppadding_bytes[0];
  339.                         #endif
  340.                             } // if(a)
  341.                             else
  342.                             {
  343.                         #ifdef _LITTLE_ENDIAN
  344.  
  345.                                 message[12]= pcrc[0];
  346.                                 message[13]= pcrc[1]; 
  347.                                 message[14]= pcrc[2]; 
  348.                                 message[15]= pcrc[3];
  349.  
  350.                                 message[32]= ps_card_number[0];
  351.                                 message[33]= ps_card_number[1]; 
  352.                                 message[34]= ps_card_number[2]; 
  353.                                 message[35]= ps_card_number[3];
  354.                                 message[36]= ps_card_number[4];
  355.                                 message[37]= ps_card_number[5]; 
  356.                                 message[38]= ps_card_number[6]; 
  357.                                 message[39]= ps_card_number[7];
  358.                                 message[40]= ps_card_number[8];
  359.                                 message[41]= ps_card_number[9]; 
  360.                                 message[42]= ps_card_number[10]; 
  361.                                 message[43]= ps_card_number[11];
  362.                                 message[44]= ps_card_number[12];
  363.                                 message[45]= ps_card_number[13]; 
  364.                                 message[46]= ps_card_number[14]; 
  365.                                 message[47]= ps_card_number[15];
  366.  
  367.                             #else
  368.  
  369.                                 message[32]= ps_card_number[15];
  370.                                 message[33]= ps_card_number[14]; 
  371.                                 message[34]= ps_card_number[13]; 
  372.                                 message[35]= ps_card_number[12];
  373.                                 message[36]= ps_card_number[11];
  374.                                 message[37]= ps_card_number[10]; 
  375.                                 message[38]= ps_card_number[9]; 
  376.                                 message[39]= ps_card_number[8];
  377.                                 message[40]= ps_card_number[7];
  378.                                 message[41]= ps_card_number[6]; 
  379.                                 message[42]= ps_card_number[5]; 
  380.                                 message[43]= ps_card_number[4];
  381.                                 message[44]= ps_card_number[3];
  382.                                 message[45]= ps_card_number[2]; 
  383.                                 message[46]= ps_card_number[1]; 
  384.                                 message[47]= ps_card_number[0];
  385.  
  386.                                 message[12]= pcrc[3]; //GOOD
  387.                                 message[13]= pcrc[2]; 
  388.                                 message[14]= pcrc[1]; 
  389.                                 message[15]= pcrc[0];
  390.  
  391.                             #endif
  392.  
  393.                             }
  394.                         }
  395.  
  396.                         .....
  397.                         .....
  398.  
  399.                         DLL_API(long) vgt_init_S3100 ( char* remote_host, int remote_port, int active_door, int seq_number)
  400.                         {
  401.  
  402.                             if (!alreadyinit)  
  403.                             init_structptr();
  404.  
  405.                             glremote_ip=remote_host;  //090407 ATTENTION APRES INIT
  406.                             glremote_port =remote_port;
  407.                             allmsg.read_number = active_door;    
  408.                             allmsg.sequence_number=seq_number;
  409.                             write_struct(1);
  410.                             init_struct =true;
  411.  
  412.                             return 0 ;
  413.                         }
  414.  
  415.  
  416.                         .....
  417.                         .....
  418.  
  419.  
  420.  
  421.                         DLL_API(long) vgt_sendto_S3100    (__int64 badgeid, char* remote_host, int remote_port)
  422.                         {
  423.                         //allmsg.card_number=(unsigned char*)&badgeid;
  424.                         //ps_card_number=(unsigned char*)badge_id;
  425.                         unsigned int crc32=0;
  426.                         temp_badge_id = badgeid;
  427.                         memset(message,0,MAX_PATH);
  428.                         //message[32]= ps_card_number[15];
  429.  
  430.  
  431.                         //allmsg.card_number=(unsigned char*)&temp_badge_id;
  432.  
  433.  
  434.                                 allmsg.card_number[15]=ptemp_badge_id[15];  //ICIBUG
  435.                                 allmsg.card_number[14]=ptemp_badge_id[14];
  436.                                 allmsg.card_number[13]=ptemp_badge_id[13]; 
  437.                                 allmsg.card_number[12]=ptemp_badge_id[12];
  438.                                 allmsg.card_number[11]=ptemp_badge_id[11];
  439.                                 allmsg.card_number[10]=ptemp_badge_id[10]; 
  440.                                 allmsg.card_number[9]=ptemp_badge_id[9]; 
  441.                                 allmsg.card_number[8]=ptemp_badge_id[8];
  442.                                 allmsg.card_number[7]=ptemp_badge_id[7];
  443.                                 allmsg.card_number[6]=ptemp_badge_id[6]; 
  444.                                 allmsg.card_number[5]=ptemp_badge_id[5]; 
  445.                                 allmsg.card_number[4]=ptemp_badge_id[4];
  446.                                 allmsg.card_number[3]=ptemp_badge_id[3];
  447.                                 allmsg.card_number[2]=ptemp_badge_id[2]; 
  448.                                 allmsg.card_number[1]=ptemp_badge_id[1]; 
  449.                                 allmsg.card_number[0]=ptemp_badge_id[0];
  450.  
  451.  
  452.                         /*
  453.                          char digits[30];
  454.                          sprintf(digits,"%I64d\n",temp_badge_id);
  455.                         //I64d
  456.                         for(int i=0; i<20; i++)
  457.  
  458.                         printf ("digits[%d] = %d ", i, digits[i]);
  459.  
  460.                         //allmsg.card_number=(unsigned char*)&digits;
  461.                                 allmsg.card_number[15]=digits[15];  //ICIBUG
  462.                                 allmsg.card_number[14]=digits[14];
  463.                                 allmsg.card_number[13]=digits[13]; 
  464.                                 allmsg.card_number[12]=digits[12];
  465.                                 allmsg.card_number[11]=digits[11];
  466.                                 allmsg.card_number[10]=digits[10]; 
  467.                                 allmsg.card_number[9]=digits[9]; 
  468.                                 allmsg.card_number[8]=digits[8];
  469.                                 allmsg.card_number[7]=digits[7];
  470.                                 allmsg.card_number[6]=digits[6]; 
  471.                                 allmsg.card_number[5]=digits[5]; 
  472.                                 allmsg.card_number[4]=digits[4];
  473.                                 allmsg.card_number[3]=digits[3];
  474.                                 allmsg.card_number[2]=digits[2]; 
  475.                                 allmsg.card_number[1]=digits[1]; 
  476.                                 allmsg.card_number[0]=digits[0];
  477.                         */
  478.  
  479.  
  480.                         printf("\nvaleur de temp_badge_id (Lu)= %Lu\n", temp_badge_id);
  481.  
  482.                         printf("\nvaleur de *ptemp_badge_id apres  (s)= %s\n", (unsigned char*)ptemp_badge_id);
  483.  
  484.                         printf("\nvaleur de *ptemp_badge_id apres (c) = %c\n");
  485.                         for (int i=0;i<16;++i)
  486.                         {
  487.                         printf("%c, ", (unsigned char*)ptemp_badge_id[i]);
  488.                         }
  489.                         printf("\n");
  490.                         //gbadge_id = badgeid;
  491.                         //allmsg.card_number= badgeid;
  492.  
  493.                         //allmsg.card_number=(unsigned char*)&temp_badge_id;
  494.                         /*
  495.                                         allmsg.card_number[0]=(((unsigned char)&badgeid) & 0x00000000000000FF); //GOOD TOO
  496.                                         allmsg.card_number[1]= (unsigned char)(&badgeid & 0x000000000000FF); //GOOD TOO
  497.                                         allmsg.card_number[2]= (unsigned char)(&badgeid & 0x0000000000FF); //GOOD TOO
  498.                                         allmsg.card_number[3]= (unsigned char)(&badgeid & 0x00000000FF); //GOOD TOO
  499.                                         allmsg.card_number[4]= (unsigned char)(&badgeid & 0x000000FF); //GOOD TOO
  500.                                         allmsg.card_number[5]= (unsigned char)(&badgeid & 0x0000FF); //GOOD TOO
  501.                                         allmsg.card_number[6]= (unsigned char)(&badgeid & 0x00FF); //GOOD TOO
  502.                                         allmsg.card_number[7]= (unsigned char)(&badgeid & 0xFF); //GOOD TOO
  503.                         */
  504.  
  505.                         //temp_badge_id  =(__int64)&badgeid;
  506.  
  507.                         //ptemp_badge_id = __ui64toa( badgeid, temparray, 10 );
  508.  
  509.                         //ptemp_badge_id=(unsigned char*)&temp_badge_id;
  510.  
  511.                         //allmsg.card_number[0]=&temp_badge_id
  512.  
  513.  
  514.                         //if (!alreadyinit)
  515.                         //init_structptr();
  516.  
  517.                         ////////////////////////////:
  518.  
  519.                         /*
  520.                             message[32]= ps_card_number[15];
  521.                             message[33]= ps_card_number[14]; 
  522.                             message[34]= ps_card_number[13]; 
  523.                             message[35]= ps_card_number[12];
  524.                             message[36]= ps_card_number[11];
  525.                             message[37]= ps_card_number[10]; 
  526.                             message[38]= ps_card_number[9]; 
  527.                             message[39]= ps_card_number[8];
  528.                             message[40]= ps_card_number[7];
  529.                             message[41]= ps_card_number[6]; 
  530.                             message[42]= ps_card_number[5]; 
  531.                             message[43]= ps_card_number[4];
  532.                             message[44]= ps_card_number[3];
  533.                             message[45]= ps_card_number[2]; 
  534.                             message[46]= ps_card_number[1]; 
  535.                             message[47]= ps_card_number[0];
  536.                         */
  537.  
  538.                         //allmsg.card_number= (unsigned char*)&badgeid;
  539.  
  540.                         /*
  541.                                 allmsg.card_number[15]=ptemp_badge_id[15];  //ICIBUG
  542.                                 allmsg.card_number[14]=ptemp_badge_id[14];
  543.                                 allmsg.card_number[13]=ptemp_badge_id[13]; 
  544.                                 allmsg.card_number[12]=ptemp_badge_id[12];
  545.                                 allmsg.card_number[11]=ptemp_badge_id[11];
  546.                                 allmsg.card_number[10]=ptemp_badge_id[10]; 
  547.                                 allmsg.card_number[9]=ptemp_badge_id[9]; 
  548.                                 allmsg.card_number[8]=ptemp_badge_id[8];
  549.                                 allmsg.card_number[7]=ptemp_badge_id[7];
  550.                                 allmsg.card_number[6]=ptemp_badge_id[6]; 
  551.                                 allmsg.card_number[5]=ptemp_badge_id[5]; 
  552.                                 allmsg.card_number[4]=ptemp_badge_id[4];
  553.                                 allmsg.card_number[3]=ptemp_badge_id[3];
  554.                                 allmsg.card_number[2]=ptemp_badge_id[2]; 
  555.                                 allmsg.card_number[1]=ptemp_badge_id[1]; 
  556.                                 allmsg.card_number[0]=ptemp_badge_id[0];
  557.                         */
  558.  
  559.  
  560.                         memset(message,0,MAX_PATH);
  561.                         write_struct(1);   //090406 (1) -> (0) -> 1 ok // plus bas
  562.  
  563.                         initcrcok =0 ;
  564.                         initcrcok = objcrc32.Init(CRC32_POLY_WINZIP);
  565.  
  566.                         rescrc32=0;
  567.  
  568.                         rescrc32= objcrc32.GetCrc32( &msg_crc,CRC_LENGTH ); //objcrc32. 
  569.  
  570.                         //allmsg.crc=rescrc32; 
  571.                         *pcrc=rescrc32; //090405
  572.                         //write_struct(0);  090408
  573.                         #ifdef _LITTLE_ENDIAN
  574.  
  575.                                         message[12]= pcrc[0];
  576.                                         message[13]= pcrc[1]; 
  577.                                         message[14]= pcrc[2]; 
  578.                                         message[15]= pcrc[3];
  579.  
  580.                         /*                message[12]= (char)(rescrc32 & 0x000000FF); //GOOD TOO
  581.                                         message[13]= (char)((rescrc32>>8) & 0x0000FF);
  582.                                         message[14]= (char)((rescrc32>>16) & 0x00FF);
  583.                                         message[15]= (char)(rescrc32>>24);
  584.                         */
  585.                         #else
  586.  
  587.                                         message[12]= pcrc[3]; //GOOD
  588.                                         message[13]= pcrc[2]; 
  589.                                         message[14]= pcrc[1]; 
  590.                                         message[15]= pcrc[0];
  591.  
  592.                                         message[15]= (char)(rescrc32 & 0x000000FF); //GOOD TOO
  593.                                         message[14]= (char)((rescrc32>>8) & 0x0000FF);
  594.                                         message[13]= (char)((rescrc32>>16) & 0x00FF);
  595.                                         message[12]= (char)(rescrc32>>24);
  596.                         #endif
  597.  
  598.                         //write_struct(0); //ICIBUG
  599.  
  600.                         /*        
  601.                                 allmsg.card_number[14]=ptemp_badge_id[14];
  602.                                 allmsg.card_number[13]=ptemp_badge_id[13]; 
  603.                                 allmsg.card_number[12]=ptemp_badge_id[12];
  604.                                 allmsg.card_number[11]=ptemp_badge_id[11];
  605.                                 allmsg.card_number[10]=ptemp_badge_id[10]; 
  606.                                 allmsg.card_number[9]=ptemp_badge_id[9]; 
  607.                                 allmsg.card_number[8]=ptemp_badge_id[8];
  608.                                 allmsg.card_number[7]=ptemp_badge_id[7];
  609.                                 allmsg.card_number[6]=ptemp_badge_id[6]; 
  610.                                 allmsg.card_number[5]=ptemp_badge_id[5]; 
  611.                                 allmsg.card_number[4]=ptemp_badge_id[4];
  612.                                 allmsg.card_number[3]=ptemp_badge_id[3];
  613.                                 allmsg.card_number[2]=ptemp_badge_id[2]; 
  614.                                 allmsg.card_number[1]=ptemp_badge_id[1]; 
  615.                                 allmsg.card_number[0]=ptemp_badge_id[0];
  616.                         */
  617.  
  618.                         /*
  619.                         printf("containe card_number\n", allmsg.card_number);
  620.                             for(int i=0; i < 15; ++i)
  621.                             {
  622.                             printf("%2d [0x%2.2X], [0x%2.2X]\n", i,allmsg.card_number[i],ptemp_badge_id[i]);
  623.                             }
  624.                         */
  625.                         printf("Sender ID = %d\n", allmsg.sender_id);
  626.                             for(int i=0; i < 4; ++i)
  627.                             {
  628.                             printf("%2d[0x%2.2X], ", i, (unsigned char)message[i]);
  629.                             }
  630.                         printf("\nEncryption key ID = %d\n", allmsg.encrypt_key_id);
  631.                             for(int i=0; i < 4; ++i)
  632.                             {
  633.                             printf("%2d[0x%2.2X], ", i, (unsigned char)message[4+i]); 
  634.                             }
  635.                         printf("\nEncrypted size = %d\n", allmsg.encrypt_size);
  636.                             for(int i=0; i < 4; ++i)
  637.                             {
  638.                             printf("%2d[0x%2.2X], ", i, (unsigned char)message[8+i]);
  639.                             }
  640.  
  641.                         printf("\nCRC-32 = %u\n",rescrc32);
  642.                             for(int i=0; i < 4; ++i)
  643.                             {
  644.                             printf("%2d[0x%2.2X], ", i, (unsigned char)message[12+i]);
  645.                             }
  646.                         printf("\nData length = %d\n", allmsg.data_length);
  647.                             for(int i=0; i < 4; ++i)
  648.                             {
  649.                             printf("%2d[0x%2.2X], ", i, (unsigned char)message[16+i]);
  650.                             }
  651.  
  652.                         printf("\nSequence number = %d\n", allmsg.sequence_number);
  653.                             for(int i=0; i < 4; ++i)
  654.                             {
  655.                             printf("%2d[0x%2.2X], ", i, (unsigned char)message[20+i]);
  656.                             }
  657.  
  658.                         printf("\nPacket type = %d\n", allmsg.packet_type);
  659.                             for(int i=0; i < 4; ++i)
  660.                             {
  661.                             printf("%2d[0x%2.2X], ", i, (unsigned char)message[24+i]);
  662.                             }
  663.  
  664.                         printf("\ndata: READER NUMBER = %d\n", allmsg.read_number);
  665.                             for(int i=0; i < 4; ++i)
  666.                             {
  667.                             printf("%2d[0x%2.2X], ", i, (unsigned char)message[28+i]);
  668.                             }
  669.                         //printf("\ndata: CARD NUMBER = %s\n", allmsg.card_number);   printf("%s\n", _i64toa(987654321234567890LL, bin, 2)); 
  670.                         printf("\ndata: CARD NUMBER = %Ld\n", temp_badge_id ); //gbadge_id //094008 remplacer paer temp_badge_id pui 
  671.                             for(int i=0; i < 16; ++i)
  672.                             {
  673.                             printf("%2d[0x%2.2X], ", i, (unsigned char)message[32+i]);
  674.                             if (i==14)printf("\n");
  675.                             }
  676.                         printf("\ndata: PIN CODE = %d\n", allmsg.pin_code);
  677.                             for(int i=0; i < 4; ++i)
  678.                             {
  679.                             printf("%2d[0x%2.2X], ", i, (unsigned char)message[48+i]);
  680.                             }
  681.  
  682.  
  683.                         printf("\n\nCOMPLETE MESSAGE:\n");
  684.  
  685.                         for(int i=0; i < 52; ++i) //090407 68 -> 52 disable padding bytes
  686.                         {
  687.                             printf("%2d[0x%2.2X], ", i, (unsigned char)message[i]);
  688.                             if (i==14||i==29||i==44||i==59) printf("\n");
  689.                         }
  690.  
  691.                         char *remote_ip=remote_host;
  692.  
  693.                         if (!error_code) 
  694.                         {    error_code =  pCVgtNotifyDll->vgt_client_open (&pCUdpClt, remote_ip,remote_port);
  695.                             if (error_code) 
  696.                             {    printf("dans if(error_code) \n");
  697.                                 if (DEBUG) fprintf(stderr," ERROR: udp client open returned error_code %ld \n",error_code);
  698.                                 sprintf(response,"error_code= %d",ERROR_NOTIFY_CLIENT_OPEN);
  699.                             }
  700.                         }
  701.  
  702.  
  703.                         if (!error_code) 
  704.                         {
  705.                             error_code =  pCVgtNotifyDll->vgt_client_send_to(&pCUdpClt, message,68); //68 -> 66
  706.                             if (error_code) 
  707.                             {
  708.                                 if (DEBUG) fprintf(stderr," ERROR: udp client send to returned error_code %ld \n",error_code);
  709.                                 sprintf(response,"error_code= %d",ERROR_NOTIFY_CLIENT_SEND_TO);
  710.                             }
  711.                         }
  712.  
  713.                         printf("\nWait for response... \n");
  714.                         if (!error_code) 
  715.                         {
  716.                             error_code =  pCVgtNotifyDll->vgt_client_wait_response (&pCUdpClt, (char *)response, MAX_PATH, &response_length, timeout_ms); //090406
  717.                             if (error_code) 
  718.                             {
  719.                                 if (DEBUG) fprintf(stderr," ERROR: udp client wait response returned error_code %ld \n",error_code);
  720.                                 sprintf(response,"error_code= %d",ERROR_NOTIFY_CLIENT_WAIT_RESPONSE);
  721.                             }
  722.                         }
  723.  
  724.                         if (!error_code) 
  725.                         {
  726.                             error_code =  pCVgtNotifyDll->vgt_client_close (&pCUdpClt);
  727.                             if (error_code) 
  728.                             {
  729.                                 if (DEBUG) fprintf(stderr," ERROR: udp client close returned error_code %ld \n",error_code);
  730.                                 sprintf(response,"error_code= %d",ERROR_NOTIFY_CLIENT_CLOSE);
  731.                             }
  732.                         }
  733.  
  734.  
  735.                         for(int i=0; i < response_length; ++i)
  736.                         {
  737.                         printf("%2d[0x%2.2X], ",i, (unsigned char)response[i]);
  738.                         if (i==14||i==29||i==44||i==59) printf("\n");
  739.  
  740.                         }
  741.  
  742.  
  743.                         init_struct=true;
  744.                         return error_code;
  745.                         }
  746.  
  747.  
  748.                         DLL_API(long) vgt_dest_S3100()
  749.                         {
  750.                         error_code=0;
  751.  
  752.                         free(response);
  753.                         printf("\n\nExiting...\n");
  754.                         delete pCrc32;
  755.  
  756.                         printf("\n");
  757.  
  758.                         return error_code;
  759.                         }
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
And in the call file call_S3100.cpp (main)
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4.                     int main(int argc, char* argv[])
  5.                     {    
  6.  
  7.                         unsigned int rescrc32=0;
  8.  
  9.                         char remote_ip[16];
  10.                         int remote_port=0; 
  11.                         unsigned int act_doors=1;
  12.  
  13.                         __int64 badge_id=0;
  14.                         int seq_num=1;
  15.  
  16.                         ....
  17.                         ....
  18.  
  19.  
  20.                     error_code = pCVgtS3100_interface->vgt_init_S3100( remote_ip, remote_port, act_doors, seq_num);//IMPL_1
  21.                     if (error_code) 
  22.                     {
  23.                         if (DEBUG) fprintf(stderr," ERROR: S3100 init returned error_code %ld \n",error_code);
  24.                         sprintf(response,"error_code= %d",ERROR_S3100_INTERFACE_INIT);
  25.                     }
  26.                     printf("\n");
  27.  
  28.  
  29.  
  30.                         .....
  31.                         .....
  32.  
  33.                     error_code =pCVgtS3100_interface->vgt_sendto_S3100(badge_id,remote_ip,remote_port);
  34.                     if (error_code) 
  35.                     {
  36.                         if (DEBUG) fprintf(stderr," ERROR: S3100 send to returned error_code %ld \n",error_code);
  37.                         sprintf(response,"error_code= %d",ERROR_S3100_INTERFACE_SEND_TO);
  38.                     }    
  39.  
  40.                         .....
  41.  
  42.  
  43.  
In resume the problem is to use good way to pass an __int64 to an unsigned char of [16] . Perhaps us mask :

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

Could you help me please.

Thank you

Kenzo
Apr 9 '09 #1
Share this Question
Share on Google+
29 Replies


P: 16
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_number an _int64 but it is not good way
fonction pass(_int64 badgeid)
{

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

...


}
Could you fix me please ?

Thank you
Apr 10 '09 #2

Savage
Expert 100+
P: 1,764
__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:

Expand|Select|Wrap|Line Numbers
  1. __int64 number=9223372036854775807 //2^63-1--> it's just an example
  2. char Array[20]="";//but then you need 20 byte array as you can see
  3.  
and you would like your array to become:"9223372036854775807"

Expand|Select|Wrap|Line Numbers
  1.     //Then you can use _i64toa
  2.     _i64toa(number,Array,10);
  3.  
Is this what you want?
Apr 10 '09 #3

P: 16
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_number[] )
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_number[15]=ptemp_badge_id[15];
allmsg.card_number[14]=ptemp_badge_id[14];
...
..

allmsg.card_number[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
Apr 10 '09 #4

Savage
Expert 100+
P: 1,764
Have you tried a simple memcopy?

Expand|Select|Wrap|Line Numbers
  1. __int64 number=0x75BCD15;
  2. char Array[16]="";
  3. memcopy(Array,&number,8);
It should do what you want..
Apr 10 '09 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Apr 10 '09 #6

P: 16
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
Apr 11 '09 #7

Savage
Expert 100+
P: 1,764
@Kenzogio
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?
Apr 12 '09 #8

P: 16
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
Apr 12 '09 #9

P: 16
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
Apr 14 '09 #10

Savage
Expert 100+
P: 1,764
@Kenzogio
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?
Apr 14 '09 #11

P: 16
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
Apr 14 '09 #12

Banfa
Expert Mod 5K+
P: 8,916
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 -170141183460469231731687303715884105728 to 170141183460469231731687303715884105727 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.
Apr 14 '09 #13

P: 16
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:
Expand|Select|Wrap|Line Numbers
  1. "FIELD NAME"             |        "FIELD SIZE"        |    REMARKS  
  2. Sender ID                |    4 bytes                 | identifies the source system to the destination system. 
  3. ...
  4.  
  5. Card Number             |  16 byte                   | The card number is an 128bit number  in  binary format.
  6.  
  7. ....
  8.  
  9.  
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
Apr 15 '09 #14

Banfa
Expert Mod 5K+
P: 8,916
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 -170141183460469231731687303715884105728 to 170141183460469231731687303715884105727 you would be looking at a range of 0 - 340282366920938463463374607431768211455.

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 18446744073709551616 (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.
Apr 15 '09 #15

P: 16
Ok,

So if we consider we are in big endian.

using bit operators I will have:

unsigned char card_number[16];
_int64 badge_id;
..
..

allmsg.card_number[0]=(unsigned char) (badge_id & 0x00000000000000FF);
allmsg.card_number[1]=(unsigned char) ((badge_id >> 8) & 0x000000000000FF);
....
...
...

allmsg.card_number[7]=(unsigned char) ((badge_id >> 56) & 0xFF);
So like you can see I can't fit all my array, with an _int64 I can fit only till array [7]. So like you said by using 2 _int64 I will fit the rest of my array like:

allmsg.card_number[8]=(unsigned char) (badge_idsecond & 0x00000000000000FF);
...
...
allmsg.card_number[15]=(unsigned char) ((badge_idsecond >> 128) & 0xFF);
S if this way is correct, ok then next step (but in fact first step) is how store the encoded value (for example passed by arguments) this time in 2 variables??

Actually I used this way for 1 variable sure:

__int64 badge_id=0;

..

badge_id = atoll(argv[4]);

..

So how must I do for 2 variables ? I don't know maybe this way:
__int64 badge_id=0;
__int64 badge_idsecond=0;

badge_id = atoll((argv[4]) >> 32); //atoll is it right with _int64
badge_idsecond = atoll((argv[4]) >> 64);

is this correct ?


Thank you
Apr 15 '09 #16

P: 16
Sorry a little error occurs for this ;

allmsg.card_number[15]=(unsigned char) ((badge_idsecond >> 128) & 0xFF);
is not 128 but 120.

Thank you
Apr 15 '09 #17

Banfa
Expert Mod 5K+
P: 8,916
@Kenzogio
Yes but the number you were looking for is 56 :D

You can not >> argv[4] (or any other member of that array) since argv is char ** so argv[4] has type char * and >> only operates on integers.

You will have to write you own routine to accept argv[4], a very long string of digits (decimal, hexadecimal or octal?) and then store them in 2 __int64 considering each digit in turn.

This is actually not as hard as it sounds, start by writing your own atol you should then be able to modify that method to produce the answer in 2 variables rather than 1.
Apr 15 '09 #18

P: 16
Yes I understand but when you writed atol you mean atoll I suppose for an _int64 ?
At time is it better to use long long int for atoll() or _int64 is too good as well for atoll()?


For the routine could you help me more ?

Thank you
Apr 16 '09 #19

Banfa
Expert Mod 5K+
P: 8,916
If you are using MSVC then you have the source to the atol series of functions and can see how they are working. There are 2 basic operations used when converting text digits to binary

1. For decimal digits
digitValue = digitCharacter - '0';
C guarantees this
you can write similar formula for hexadecimal digits too

2. If you are read the digits most significant to least significant (left to right) then for each digit

finalValue = previousFinalValue * base + currentDigitValue;

MSVC does not appear to contain atoll only __atoi64 (at least for the versions I have 6 and 2005).
Apr 16 '09 #20

P: 16
Unfortunatly I'm using Kdevelop C++ in Linux
So I don't know how make this routine.
Could you give me an example code please ?

Thank you
Apr 16 '09 #21

Expert 100+
P: 2,400
Perhaps you should take a look at GNU Multiple Precision Arithmetic Library for a portable solution. Function names that start with underscore make me nervous.
Apr 16 '09 #22

Banfa
Expert Mod 5K+
P: 8,916
In that case you are resumably using the GNU C Standard library implementation. It should be easy enough to find the source code for that with a little googling.
Apr 16 '09 #23

P: 16
Ok,

I will try to do :)

Thank you
Apr 16 '09 #24

weaknessforcats
Expert Mod 5K+
P: 9,197
Weakness, could you please explain how is it that it will cause problems when there are two complements?
Assume a char with -1. The bits are 11111111. If you put those in the low 8 buts of an _int64 the sign bit is now part of the data and the value is screwed up.

The 2's complement -1 in the char needs to convert to s 64-bit 2's complement in the _int64: 11111111111111111111111111111111111111111etc.....

The compiler will do this if you just assign the char to the _int64.
Apr 19 '09 #25

Banfa
Expert Mod 5K+
P: 8,916
Just to be clear it was Savage and not me that asked that question :D
Apr 19 '09 #26

P: 16
Hi,

Not yet time to see quite for programming this routine :(
any help will appreciate :)

Thank you
Apr 20 '09 #27

Expert 100+
P: 2,400
@weaknessforcats
char is a special case. Whether char is signed or unsigned is implementation-dependent. You should avoid using char for anything other than characters. If you want a type to hold small integers then use signed char or unsigned char.

This advice about sign extension is certainly true for signed char.
Apr 20 '09 #28

P: 16
Hi,

I have do that routine:
Is that good the way ?


ascii2binary (char* sourceAscii, char* destBin)
{

int64 lowPart = 0;
int highPart = 0;

for(int i=0; i<20; ++i)
{
lowPart += (sourceAscii[i]-48)*exp(10,i); //example= 10 exp 0 //corresponding to number 2 of 654342.

}
for(int i=20; i<38; ++i)
{
highPart += (sourceAscii[i]-48)*exp(10,i);

}


//Here how write conversion of 2 _int64 to destBin[] with bitwise and mask operators ?

}



How must a convert into the destBin[] ?

Thank you.

Kenzo
Apr 25 '09 #29

P: 16
Hi,

So I wrote code like this

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void ascii2binary (char* sourceAscii, char* destbin, int size)
{

__int64 lowPart = 0;
__int64 highPart = 0;

for(int i=0; i<20; ++i)
{
lowPart += (sourceAscii[i]-48)*exp(i); //exemple= 10 exp 0 //corresponding to first chiffre
//of 654342, here 2.

//48 for remove 0 from ascii

}
for(int j=20; j<38; ++j)
{
highPart += (sourceAscii[j]-48)*exp(j);

}

printf("value of lowPart: %ld\n",lowPart);
printf("value of highPart: %ld\n",highPart);
printf("sizeof lowpart & higpart is : %d\n", sizeof(lowPart));


destbin[0] =(char)(lowPart & 0x00000000000000FF);
destbin[1] =(char)((lowPart>>8) & 0x000000000000FF);
destbin[2] =(char)((lowPart>>16) & 0x0000000000FF);
destbin[3] =(char)((lowPart>>24) & 0x00000000FF);
destbin[4] =(char)((lowPart>>32) & 0x000000FF);
destbin[5] =(char)((lowPart>>40) & 0x0000FF);
destbin[6] =(char)((lowPart>>48) & 0x00FF);
destbin[7] =(char)((lowPart>>56) & 0xFF);

destbin[8] =(char)(highPart & 0x00000000000000FF);
destbin[9] =(char)((highPart>>8) & 0x000000000000FF);
destbin[10]=(char)((highPart>>16) & 0x0000000000FF);
destbin[11]=(char)((highPart>>24) & 0x00000000FF);
destbin[12]=(char)((highPart>>32) & 0x000000FF);
destbin[13]=(char)((highPart>>40) & 0x0000FF);
destbin[14]=(char)((highPart>>48) & 0x00FF);
destbin[15]=(char)((highPart>>56) & 0xFF);


for( int k =0; k<size ; ++k)
{
printf("string destbin is: %c\n", destbin[k]); //bug
}

}


void main (int args, char * argv[])
{

int indice;

char *tab[20];
char* ptabdest;


// char* ptab;
// ptab = (char*)malloc(16); //
// memset (ptab,0,16);

if(args != 2){
printf("erreur\n");
printf("usage: call program with string in params");
exit(1);
}

indice = 0;
while(argv[1][indice] != '\0'){
indice++;
}

ptabdest = (char*)malloc(indice);
printf("le string %s contient %d lettres\n", argv[1], indice);

tab[0]=argv[1];
printf("passage a la variable char *tab[] contient:%s\n", argv[1]);


ascii2binary( tab[0], ptabdest, indice);

system("PAUSE");

}

but I have not what I expected for example I put parameter to my program argument to main here: 123456789123456789
and ithe display is not this. why ?
Maybe the problem is on my way to use char * ?
Could you you help me please ?

Thank you

Kenzo
May 11 '09 #30

Post your reply

Sign in to post your reply or Sign up for a free account.