Writing

Software, technology, sysadmin war stories, and more. Feed
Monday, October 3, 2011

When all else fails, brute force it?

This is some real code I found tonight while researching something:

    // I know there's a better way, I'm just too lazy to change it
    int r=0, g=0, b=0;
    if (cs[0]>='a' && cs[0]<='f') r += (cs[0]-'a'+10)*16;
    else if (cs[0]>='A' && cs[0]<='F') r += (cs[0]-'A'+10)*16;
    else if (cs[0]>='0' && cs[0]<='9') r += (cs[0]-'0')*16;

... it then repeats that block for cs[1], cs[2], cs[3], cs[4], and yes, cs[5].

I guess strtoul was too scary?