Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting number into different notations

Tags:

c

pointers

This is the program to convert a number into different representations like octal,decimal,hexadecimal etc.

    #include<stdio.h>

    char *convert(unsigned int num, int base)
    {
    static char buff[33];
    char *ptr;
    ptr=&buff[sizeof(buff)-1];
    *ptr='\0';
    do
    {
    *--ptr="0123456789abcdef"[num%base];
    num/=base;
    }while(num!=0);
    return(ptr);
    }

    int main(){

    puts(convert(65,8));
    puts(convert(65,10));
    puts(convert(65,16));
    return 0;
    }

Output gives 101 ,65 and 41 ( i.e the number '65' represented in octal,decimal and hexadecimal notations respectively)

I pretty much understand whats going on but i have never come across anything like

*--ptr="0123456789abcdef"[num%base]

I understand its working but i dont understand how it is a valid notation. Someone please explain the 0123456789abcdef(literal character array) part here.

like image 805
Anusha Pachunuri Avatar asked Jan 17 '12 04:01

Anusha Pachunuri


3 Answers

It is a ghastly piece of work; whoever wrote it should be taken out round the back of the shed and beaten up until they promise never to write such code again outside of an entry for the IOCCC (International Obfuscated C Code Contest).

The right-hand side of the expression

*--ptr = "0123456789abcdef"[num%base];

is exploiting the fact that a string literal is converted to a pointer. If you saw:

const char digit[] = "0123456789ABCDEF";
*--ptr = digit[num%base];

you'd not be worried. The expression with the string literal is equivalent to that. (At least they had the grace not to write:

*--ptr = (num % base)["0123456789ABCDEF"];

That's also equivalent by virtue of the relation:

a[i] <==> i[a] <==> *(a + i) <==> *(i + a)

where the double-headed arrows indicate equivalence.

The use of *--ptr is extracting the digits backwards, from least to most significant. It works because the pointer is initialized to the end of the static buffer (and it is crucial that it is static since the return value of the function is a pointer to that buffer).

However, the code is not very useful because you can't save the value from a call and print it later if there's been another call. You could not write:

printf("%s = %s = %sn", convert(65,8), convert(65,10), convert(65,16));

Or, more accurately, you can, but you will see the same value three times one of "101", "164" or "140" in the first position, and either "01" or "64" or "40" for the other two positions, and it is not defined by the C standard which values you will see. The static buffer also prevents the code from being thread-safe.

All in all, it is weird show-off code for teasing beginners with. It works, but that's about all that can be said about it. It doesn't check its base for validity, either, so convert(65, 18) likely leads to undefined behaviour. (Base 17 would, at worst, reference a '\0' that was not intended, leading to confusion.)

like image 93
Jonathan Leffler Avatar answered Sep 18 '22 20:09

Jonathan Leffler


Wow, that is terse and hard to read. I understand your confusion, but I think I can help.

*--ptr = says "decrement this pointer and assign some value to the address after decrementing."

"0123456789abcdef" is a literal char array.

[num%base] is subscripting the previous char array, and the subscript is num modulo base.

This one line encompasses several things, which, if spread out fully, would look like this:

char arr[] = "0123456789abcdef";
int subscript = num % base;
ptr = ptr - 1;
*ptr = arr[subscript];
like image 26
Dan Fego Avatar answered Sep 20 '22 20:09

Dan Fego


Its sneaky short hand - think of it more like this

char array[]="0123456789abcdef";
*--ptr=array[num%base];
like image 21
Adrian Cornish Avatar answered Sep 21 '22 20:09

Adrian Cornish