Having a problem with strtoull.

This is a discussion on Having a problem with strtoull. within the C Programming forums, part of the General Programming Boards category; I have the following code segment I am testing for an encryption program I am to wirte. Namely the blowfish ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    177

    Having a problem with strtoull.

    I have the following code segment I am testing for an encryption program I am to wirte. Namely the blowfish algorithm. I am to input from the command line up to a 64 bit hex number and convert it to a base ten number. I am using the code segment on a Dell 64 bit server with ubuntu server 64 installed using gcc from the command line. What seems to be happening is it is only converting the first 32 bits. Here is what I am inputing:
    Code:
    48656c6c6f20576f726c642120486f77277320697420676f696e673f
    and this is what my output is:
    Code:
    18446744073709551615
    Here is my code segment:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char buffer[64];
        unsigned long long ul;
    
        printf("Enter an unsigned number: ");
        fgets(buffer, 64, stdin);
    
        ul = strtoull (buffer, NULL, 16);
        printf("Output: %llu\n", ul);
        return 0;
    }
    What am I doing incorrect here and what can be done to correct it.
    Thanks in advance!

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,176
    48656c6c6f20576f726c642120486f77277320697420676f69 6e673f
    That is way more than 64 bits. At a guess, that's intended for a 1024 bit number.

    18446744073709551615
    That's also known as "2^64-1" or "the maximum value of 64 bit `unsigned long long'".

    I suggest you read the documentation for `strtoull' and download a "bignum" library.

    Soma

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Each character is 4 bits, so your example is a 224-bit number, not a 64-bit number.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    177
    Awe crap! Thanks guys, wasn't even thinking. Been a long day. Need to walk away for a while.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    826
    If you want to use explicit integer sizes, you'd better use stdint.h. For example:
    Code:
    #include <stdint.h>
    
    /* Parse a 64-bit unsigned hexadecimal number.
     * Returns the pointer to the first unparsed character,
     * or NULL if an error occurs.
    */
    const char *parse_hex64(const char *hex, uint64_t *const resultptr, int *const parsedbits)
    {
        uint64_t result = 0;
        int n = 0;
    
        if (!hex)
            return NULL;
    
        /* Skip leading whitespace and ASCII control characters. */
        while ((*hex >= 1 && *hex <= 32) || (*hex == 127))
            hex++;
    
        /* Skip common 0x or 0X prefix. */
        if (hex[0] == '0')
            if (hex[1] == 'x' || hex[1] == 'X')
                hex += 2;
    
        /* Parse up to 16 hexadecimal digits */
        while (n < 16)
            if (hex[n] >= '0' && hex[n] <= '9')
                result = (result * (uint64_t)16) + (uint64_t)(hex[n++] - '0');
            else
            if (hex[n] >= 'A' && hex[n] <= 'F')
                result = (result * (uint64_t)16) + (uint64_t)(hex[n++] - 'A' + 10);
            else
            if (hex[n] >= 'a' && hex[n] <= 'f')
                result = (result * (uint64_t)16) + (uint64_t)(hex[n++] - 'A' + 10);
            else
                break;
    
        /* No digits at all? */
        if (n < 1) {
            if (resultptr) *resultptr = 0;
            if (parsedbits) *parsedbits = 0;
            return NULL;
        }
    
        if (resultptr)
            *resultptr = result;
    
        if (parsedbits)
            *parsedbits = 4 * n;
    
        /* hex+n is the first unparsed character. */
        return hex + n;
    }
    You can use the above function to read hexadecimal data in 64-bit units. If the last parameter is not NULL, the number of bits actually parsed is saved at the integer it points to. Feel free to adapt it to your needs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 08:14 AM
  5. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 09:40 PM

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