/* * Packs a fixed-width mac address (ascii string) into 6 bytes. * String need not be null-terminated, but has to be fixed-width (i.e. always * FF:FF:FF:FF:FF:FF, not 0:7:A0:F:F2:30). * There is no error checking. * This function merely serves an example of the used algorithm to convert * a hexadecimal character string into a number. Here it does so two at a time. * A pattern can be seen when examining the ASCII table: 0 48 0011 0000 1 49 0011 0001 2 50 0011 0010 3 51 0011 0011 4 52 0011 0100 5 53 0011 0101 6 54 0011 0110 7 55 0011 0111 8 56 0011 1000 9 57 0011 1001 ^ ^^^^ A 65 0100 0001 B 66 0100 0010 C 67 0100 0011 D 68 0100 0100 E 69 0100 0101 F 70 0100 0110 ^ ^^^^ a 97 0110 0001 b 98 0110 0010 c 99 0110 0011 d 100 0110 0100 e 101 0110 0101 f 102 0110 0110 ^ ^^^^ The numbers 0-9 start from 0 on the lower half byte, and the second-to-last bit is never set. The numbers A-F and a-f start from 1 on the lower half byte, and the second- to-last bit IS set. It doesn't matter to us wether a character is capital or not, as long as it can be discerned from one of the other digits. What we do: grab the right half: t = t & 0x0F; add 9 if character is a-f or A-F: if (t>>6)t+=9; OR we do it without a jump: t += (9*(t>>6)); So in short, we'll get: t = (t & 0x0F) + (9*(t>>6)); */ static void pack_mac (char *mac, unsigned char pip[6]) { int i=0; while (i<6) { char c1 = *mac++; char c2 = *mac; mac+=2; pip[i++] = ( /* first character */ ((c1 & 0xF) /* take right half */ +(9*(c1>>6))) /* add 9 if character is a-f or A-F */ <<4 /* pack into the left half of the byte */ ) | ( /* second character */ (c2 & 0xF) +(9*(c2>>6)) ); /* leave it as the left half */ } }