-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitohex.c
73 lines (61 loc) · 1.51 KB
/
itohex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef itohex_c
#define itohex_c
//+header stdio.h
//+include
//+def
int _itohex(int i,char* buf,int padding, int capitals){
if ( capitals>0 )
capitals = 55;
else
capitals = 87;
padding = padding - 8;
if ( padding < -7 )
padding = -7;
union { int n; char c[4]; } conv[2];
conv[0].n = (( i & 0xf0f0f0f0 ) >> 4);
conv[1].n = ( i & 0x0f0f0f0f );
int p = 0;
int a,b;
for ( a=3; a>=0; a-- ){
for ( b=0; b <=1; b++ ){
if ( padding != 0 ){
if ( conv[b].c[a] != 0 ){
padding = 0;
}
}
if ( padding == 0 ){
char c = conv[b].c[a];
if ( c < 0xa )
c = c + 48;
else
c = c + capitals; // 55 for big abc ..
buf[p] = c;
p++;
} else
padding++;
}
}
buf[p] = 0 ;
return(p);
}
//+doc convert a number to hexadecimal representation.
// the conversion assumes a size of 32bits for integers,
// negative values are represented as they are stored internally.
// ( -1 is 0xffffffff, -2 0xfffffffe, ... )
//+header stdio.h
//+depends _itohex
//+def
int itohex(int i,char* buf,int padding){
return(_itohex(i,buf,padding,0));
}
//+doc convert a number to hexadecimal representation with big capitals.
// the conversion assumes a size of 32bits for integers,
// negative values are represented as they are stored internally.
// ( -1 is 0xFFFFFFFF, -2 0xFFFFFFFE, ... )
//+header stdio.h
//+depends _itohex
//+def
int itoHEX(int i,char* buf,int padding){
return(_itohex(i,buf,padding,1));
}
#endif