Created
June 21, 2015 07:07
-
-
Save kamiyaowl/2d14ecee79528e61d2a2 to your computer and use it in GitHub Desktop.
Number -> Hex for Embedded
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef unsigned short uint16; | |
typedef unsigned char uint8; | |
const char numberTable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; | |
void itoa(uint16 src, char* buf){ | |
uint8 i; | |
buf[0] = '0'; | |
buf[1] = 'x'; | |
for (i = 0; i < 4; ++i){ | |
buf[2 + i] = numberTable[(src >> ((3 - i) << 2)) & 0xf]; | |
} | |
} | |
int main(void) { | |
char buf[6]; | |
for (int i = 0; i <= 0xffff; ++i){ | |
printf("%d -> ", i); | |
//TEST | |
itoa(i, buf); | |
for (int k = 0; k < 6; ++k){ | |
putchar(buf[k]); | |
} | |
putchar('\r'); | |
putchar('\n'); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
桁数[3,2,1,0]
シフト数生成( << 2 == * 4) [12,8,4,0 ]
シフト0xabcd -> [0x000a, 0x00ab, 0x0abc, 0xabcd]
最下位桁マスク [0xa, 0xb, 0xc, 0xd]
0~15をcharテーブルから持ってくる