2 ** A utility program to translate SQLite varints into decimal and decimal
3 ** integers into varints.
9 #if defined(_MSC_VER) || defined(__BORLANDC__)
11 typedef unsigned __int64 u64
;
13 typedef long long int i64
;
14 typedef unsigned long long int u64
;
17 static int hexValue(char c
){
18 if( c
>='0' && c
<='9' ) return c
- '0';
19 if( c
>='a' && c
<='f' ) return c
- 'a' + 10;
20 if( c
>='A' && c
<='F' ) return c
- 'A' + 10;
24 static char toHex(unsigned char c
){
25 return "0123456789abcdef"[c
&0xf];
28 static int putVarint(unsigned char *p
, u64 v
){
30 unsigned char buf
[10];
31 if( v
& (((u64
)0xff000000)<<32) ){
32 p
[8] = (unsigned char)v
;
35 p
[i
] = (unsigned char)((v
& 0x7f) | 0x80);
42 buf
[n
++] = (unsigned char)((v
& 0x7f) | 0x80);
46 for(i
=0, j
=n
-1; j
>=0; j
--, i
++){
53 int main(int argc
, char **argv
){
59 unsigned char zHex
[20];
64 " %s HH HH HH ... Convert varint to decimal\n"
65 " %s DDDDD Convert decimal to varint\n"
66 " Add '+' or '-' before DDDDD to disambiguate.\n",
71 || (strlen(argv
[1])==2 && hexValue(argv
[1][0])>=0 && hexValue(argv
[1][1])>=0)
74 for(i
=1; i
<argc
&& i
<9; i
++){
75 if( strlen(argv
[i
])!=2 ){
76 fprintf(stderr
, "Not a hex byte: %s\n", argv
[i
]);
79 x
= (hexValue(argv
[i
][0])<<4) + hexValue(argv
[i
][1]);
80 uX
= (uX
<<7) + (x
&0x7f);
81 if( (x
&0x80)==0 ) break;
84 if( strlen(argv
[i
])!=2 ){
85 fprintf(stderr
, "Not a hex byte: %s\n", argv
[i
]);
88 x
= (hexValue(argv
[i
][0])<<4) + hexValue(argv
[i
][1]);
93 fprintf(stderr
, "Extra arguments: %s...\n", argv
[i
]);
100 else if( z
[0]=='-' ){ z
++; sign
= -1; }
103 if( z
[0]<'0' || z
[0]>'9' ){
104 fprintf(stderr
, "Not a decimal number: %s", argv
[1]);
107 uX
= uX
*10 + z
[0] - '0';
116 n
= putVarint(zHex
, uX
);
117 printf("%lld =", (i64
)uX
);
119 printf(" %c%c", toHex(zHex
[i
]>>4), toHex(zHex
[i
]&0x0f));