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

  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
    136
    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
    28,413
    maven: atoll is already in the standard library (per C99), and you would probably have better luck suggesting strtoll instead due to base conversion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 06: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, 07: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