FreeBASIC  0.91.0
strw_hex_lng.c
Go to the documentation of this file.
1 /* hexw$ routine for long long's */
2 
3 #include "fb.h"
4 
5 static FB_WCHAR hex_table[16] = {_LC('0'),_LC('1'),_LC('2'),_LC('3'),
6  _LC('4'),_LC('5'),_LC('6'),_LC('7'),
7  _LC('8'),_LC('9'),_LC('A'),_LC('B'),
8  _LC('C'),_LC('D'),_LC('E'),_LC('F')};
9 
10 FBCALL FB_WCHAR *fb_WstrHexEx_l ( unsigned long long num, int digits )
11 {
12  FB_WCHAR *s;
13  int i;
14  unsigned long long num2;
15 
16  if( digits <= 0 ) {
17  /* Only use the minimum amount of digits needed; need to count
18  the important 4-bit (base 16) chunks in the number.
19  And if it's zero, use 1 digit for 1 zero. */
20  digits = 0;
21  num2 = num;
22  while( num2 ) {
23  digits += 1;
24  num2 >>= 4;
25  }
26  if( digits == 0 )
27  digits = 1;
28  }
29 
30  s = fb_wstr_AllocTemp( digits );
31  if( s == NULL )
32  return NULL;
33 
34  i = digits - 1;
35  while( i >= 0 ) {
36  s[i] = hex_table[num & 0xF];
37  num >>= 4;
38  i -= 1;
39  }
40 
41  s[digits] = _LC('\0');
42  return s;
43 }
44 
45 FBCALL FB_WCHAR *fb_WstrHex_l ( unsigned long long num )
46 {
47  return fb_WstrHexEx_l( num, 0 );
48 }