[GO] C> link> Define a string and access the index> * dstPtr ++ = "0123456789ABCDEF" [nibble];

[Comment] from @ SaitoAtsushi (http://qiita.com/7of9/items/92f5a0850370d3f3e558#comment-4cd1207dc70f48f1020d).

*dstPtr++ = "0123456789ABCDEF"[nibble];

It will be a linked article for future search.

Can it be diverted to something other than the sequence from 0 to F?

Related code

stackoverflow posted an example of the above usage failure.

http://ideone.com/LoFO3r

#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));
    
	printf("%s = %s = %sn", convert(65,8), convert(65,10), convert(65,16));    

    return 0;
}

result


101
65
41
101 = 01 = 01n

~~ It seems that you need to be careful around here. ~~

I feel that the above problem is caused by something other than the writing " 0123456789ABCDEF "[nibble];.

http://ideone.com/0yHNQ0

Recommended Posts

C> link> Define a string and access the index> * dstPtr ++ = "0123456789ABCDEF" [nibble];
What is the difference between a symbolic link and a hard link?
Define the reaction pattern with SMARTS with RDKit and generate a reactant