Skip to content

Instantly share code, notes, and snippets.

@kcbanner
Created June 30, 2011 02:56
Show Gist options
  • Save kcbanner/1055532 to your computer and use it in GitHub Desktop.
Save kcbanner/1055532 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <limits.h>
char* i_to_hex_a(int value, char* str) {
//convert a 32 bit value to hex counterpart
//assumes str is 9 characters long
int i;
int a;
for(i = 0; i < 8; i++){
a = (value && 0xF0000000);
if(a < 10){
str[i] = '0' + a;
} else {
str[i] = '0' + 7 + a;
}
value = value << 4;
}
str[9] = 0;
}
int main() {
char str[9];
int i;
for (i = 0; i < 0xffffffff; i++) {
i_to_hex_a(i, str);
printf("%x: 0x%s\n", i, str);
}
return 0;
}
Copy link

ghost commented Jun 30, 2011

include <stdio.h>

include <limits.h>

char* i_to_hex_a(int value, char* str) {
//convert a 32 bit value to hex counterpart
//assumes str is 9 characters long

int i;
int a;
for(i = 0; i < 8; i++){
    a = ((value & 0xF0000000) >> 28);
    if(a < 10){
        str[i] = '0' + a;
    } else {
        str[i] = '0' + 7 + a;
    }
    value = value << 4;
}
str[9] = 0;

}

int main() {
char str[9];
int i;

for (i = 0; i < 0xffffffff; i++) {
    i_to_hex_a(i, str);
    printf("%x: 0x%s\n", i, str);
}

return 0;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment