Like Tree1Likes

How to convert char array of 64 bytes to long long int

This is a discussion on How to convert char array of 64 bytes to long long int within the C Programming forums, part of the General Programming Boards category; Here's an extension of the original program I gave. Choose an "endianness" and copy the key.ints into the rd_key of ...

  1. #16
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Here's an extension of the original program I gave. Choose an "endianness" and copy the key.ints into the rd_key of your struct.
    Code:
    #include <stdio.h>
    
    //#define METHOD_ONE
    
    int main() {
        char dek[] = "016211c7f60824a8ec5df54737a08ad0"
                     "a11b2e451160601a94ccd53555a335ab";
        union {
            unsigned char bytes[32];
            unsigned int  ints[8];
        } key;
        unsigned x;
        int i, j;
    
    #ifdef METHOD_ONE
        for (i = 0; i < 32; i++) {
            sscanf(dek+i*2, "%2x", &x);
            key.bytes[i] = x;
        }
    #else
        for (i = 0; i < 8; i++)
            for (j = 0; j < 4; j++) {
                sscanf(dek+(i*4+j)*2, "%2x", &x);
                key.bytes[i*4+3-j] = x;
            }
    #endif
    
        // Print string
        printf("%s\n", dek);
    
        // Print bytes
        for (i = 0; i < 32; i++) printf("%02x", key.bytes[i]);
        printf("\n");
    
        // Print ints
        for (i = 0; i < 8; i++) printf("%08x", key.ints[i]);
        printf("\n");
    
        return 0;
    }

  2. #17
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    i think i am getting it... will try and get back to the forum.. thanks a lot oogabooga and also to other users who have replied...

  3. #18
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Actually, I've made the above code too complicated. You want something more like this:
    Code:
    void hexStringToUIntArray(char *str, unsigned *a) {
        int i;
        for (i = 0; i < 8; i++)
            sscanf(str + i * 8, "%8x", &a[i]);
    }
    
    int main() {
        char dek[] = "016211c7f60824a8ec5df54737a08ad0"
                     "a11b2e451160601a94ccd53555a335ab";
        unsigned a[8];
        int i;
    
        hexStringToUIntArray(dek, a);
    
        // Print string
        printf("%s\n", dek);
    
        // Print ints
        for (i = 0; i < 8; i++) printf("%08x", a[i]);
        printf("\n");
    
        return 0;
    }

  4. #19
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    Try this to convert string in long:
    Code:
    int main()
    {
    long long var;
    char p[]="cboard";
    var = atoll(p);
    return 0;
    }
    long long atoll(char *str)
    {
            long long num1;
            sscanf(str,"%llu",&num1);
            return num1;
    }
    S_ccess is waiting for u. Go Ahead, put u there.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    maven: atoll is already in the standard library (per C99), and you would probably have better luck suggesting strtoll instead due to base conversion.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C]Convert Char* to Unsigned Long
    By MaSSaSLaYeR in forum C Programming
    Replies: 10
    Last Post: 11-17-2011, 05:46 AM
  2. Convert two 32-bit longs to one 64-bit long long
    By chriscapitolo in forum C Programming
    Replies: 2
    Last Post: 04-27-2011, 01:43 PM
  3. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  4. cannot convert parameter 1 from 'char *' to 'long'
    By MrLucky in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2006, 06:31 AM
  5. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21