how pass an __int64 to unsigned char [16]

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

    #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_num ber[0]=(unsigned char) (badge_id & 0x0000000000000 0FF);
    allmsg.card_num ber[1]=(unsigned char) ((badge_id >> 8) & 0x000000000000F F);
    ....
    ...
    ...

    allmsg.card_num ber[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_num ber[8]=(unsigned char) (badge_idsecond & 0x0000000000000 0FF);
    ...
    ...
    allmsg.card_num ber[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

    Comment

    • Kenzogio
      New Member
      • Apr 2009
      • 16

      #17
      Sorry a little error occurs for this ;

      allmsg.card_num ber[15]=(unsigned char) ((badge_idsecon d >> 128) & 0xFF);
      is not 128 but 120.

      Thank you

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #18
        Originally posted by Kenzogio
        Sorry a little error occurs for this ;

        is not 128 but 120.
        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.

        Comment

        • Kenzogio
          New Member
          • Apr 2009
          • 16

          #19
          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

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #20
            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 = previousFinalVa lue * base + currentDigitVal ue;

            MSVC does not appear to contain atoll only __atoi64 (at least for the versions I have 6 and 2005).

            Comment

            • Kenzogio
              New Member
              • Apr 2009
              • 16

              #21
              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

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #22
                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.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #23
                  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.

                  Comment

                  • Kenzogio
                    New Member
                    • Apr 2009
                    • 16

                    #24
                    Ok,

                    I will try to do :)

                    Thank you

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #25
                      Originally posted by Banfa
                      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: 111111111111111 111111111111111 11111111111etc. ....

                      The compiler will do this if you just assign the char to the _int64.

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #26
                        Just to be clear it was Savage and not me that asked that question :D

                        Comment

                        • Kenzogio
                          New Member
                          • Apr 2009
                          • 16

                          #27
                          Hi,

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

                          Thank you

                          Comment

                          • donbock
                            Recognized Expert Top Contributor
                            • Mar 2008
                            • 2427

                            #28
                            Originally posted by weaknessforcats
                            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: 111111111111111 111111111111111 11111111111etc. ....

                            The compiler will do this if you just assign the char to the _int64.
                            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.

                            Comment

                            • Kenzogio
                              New Member
                              • Apr 2009
                              • 16

                              #29
                              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

                              Comment

                              • Kenzogio
                                New Member
                                • Apr 2009
                                • 16

                                #30
                                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 & 0x0000000000000 0FF);
                                destbin[1] =(char)((lowPar t>>8) & 0x000000000000F F);
                                destbin[2] =(char)((lowPar t>>16) & 0x0000000000FF) ;
                                destbin[3] =(char)((lowPar t>>24) & 0x00000000FF);
                                destbin[4] =(char)((lowPar t>>32) & 0x000000FF);
                                destbin[5] =(char)((lowPar t>>40) & 0x0000FF);
                                destbin[6] =(char)((lowPar t>>48) & 0x00FF);
                                destbin[7] =(char)((lowPar t>>56) & 0xFF);

                                destbin[8] =(char)(highPar t & 0x0000000000000 0FF);
                                destbin[9] =(char)((highPa rt>>8) & 0x000000000000F F);
                                destbin[10]=(char)((highPa rt>>16) & 0x0000000000FF) ;
                                destbin[11]=(char)((highPa rt>>24) & 0x00000000FF);
                                destbin[12]=(char)((highPa rt>>32) & 0x000000FF);
                                destbin[13]=(char)((highPa rt>>40) & 0x0000FF);
                                destbin[14]=(char)((highPa rt>>48) & 0x00FF);
                                destbin[15]=(char)((highPa rt>>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(1 6); //
                                // 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(i ndice);
                                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: 123456789123456 789
                                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

                                Comment

                                Working...