Skip to content

Instantly share code, notes, and snippets.

@malkia
Created August 29, 2010 11:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malkia/556223 to your computer and use it in GitHub Desktop.
Save malkia/556223 to your computer and use it in GitHub Desktop.
#include <stdio.h>
char hex( char h )
{
signed char xa = '0' - h;
signed char xb = h - ('9' + 1);
signed char ya = ('A' - 1) - h;
signed char yb = h - ('F' + 1);
signed char za = ('a' - 1) - h;
signed char zb = h - ('f' + 1);
return -(xa & (xa&xb)>>7
| (ya-9) & (ya&yb)>>7
| (za-9) & (za&zb)>>7);
}
int main()
{
char s[]="_##$#$@0123456789abcdefABCDEF!@#!@#/ZXCzxc*&#$%";
// char s[]="0123456789abcdefABCDEF";
int i;
for(i=0; i<sizeof(s)-1; i++)
printf( "%1x ", hex(s[i]));
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment