128-bit number to senary (base 6) efficient tranlation

This is a discussion on 128-bit number to senary (base 6) efficient tranlation within the C Programming forums, part of the General Programming Boards category; I have to translate one 128-bit-long number (witch is stored in 16 separate bytes in one array) to a senary ...

  1. #1
    Registered User t_maggot's Avatar
    Join Date
    Jun 2008
    Location
    Alexandroupolis
    Posts
    2

    128-bit number to senary (base 6) efficient tranlation

    I have to translate one 128-bit-long number (witch is stored in 16 separate bytes in one array) to a senary (base 6) 25-byte-long char (ascii) string ('0'....'5'). I am looking for code consuming the least memory space posible as this would be implemented in an mcu with very little recources (ram is below 1kb). In fact I wonder that maybe there is a way to do this translation without using at all long data types, extended divisions etc.

    Any help or directions appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Hmm, do you know how to convert from decimal to base 6? You just modulate the number by 6, and use that value as the base6 index to get the correct character, than divide the value by 6.

    Code:
    int main()
    {
        char out[25] = {0};
        int testVal = 393;
        const char base6[6] = {
            '0', '1', '2', '3', '4', '5'
        };
        
        char* ptr = &out[24];
        for(; ptr >= out; --ptr)
        {
            *ptr = base6[testVal % 6];
            testVal /= 6;
        }
    
        //test the result
        size_t i = 0;
        while(i < sizeof(out))
            printf("%c", out[i++]);
    
        return 0;
    }
    It requires a bit more to convert a large number like 128-bit.

  3. #3
    Registered User t_maggot's Avatar
    Join Date
    Jun 2008
    Location
    Alexandroupolis
    Posts
    2
    With this method I need only 24 divisions with 6 to produce the base-6 number. Of cource I can't handle the 128-bit data at once, I have to separate the division-cycle to 16 sub-divisions for each byte of the array. But I can handle this. Great!
    (As far as now I don't ever use the modulus operator, and I have done number to decade-char translations with my own custom code) This is far more efficient, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Channel number, need 12 bit
    By frenchfry164 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 01:44 PM
  2. How to change number data from base 256 to base 16?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 12:19 AM
  3. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM
  4. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-26-2001, 12:45 AM

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