Try using the attached hex.c function. You'll need to compile this as a .so and 
call sqlite3_create_function to register it.

Hth

Mau Liste <[EMAIL PROTECTED]> wrote: Hello all,
I am trying to print some table values in hexadecimal.
I've seen in the docs that there is a function called 'hex'
but the following:

create table aa (a integer);
insert into aa values(10);
insert into aa values(11);
insert into aa values(12);
insert into aa values(13);
insert into aa values(14);
insert into aa values(15);
select hex(a) from aa;

results in: SQL error: no such function: hex

while:

select min(a) from aa;

results in the correct answer: 10

I've seen in the source code that the function hex does indeed exist and 
it is inserted into the built in function table together with the min 
and a load of other functions.

Before digging more into the sources, can you tell me if I am doing 
something stupid and how the function hex may be used?
Or at least if it is possible to print hexadecimal values at all.

Thanks all.
Mau.
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

/*
** The toHexFunc returns a string of Hexidicimal values using
** a leading Prefix of 0x .
** To install use 
**  sqlite3_create_function(db,"to_hex",1, SQLITE_ANY,0,tohexFunc,0,0);
**  Library must be compiled without SQLITE_OMIT_LOAD_EXTENSION defined
*/
static void tohexFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
int i, n;
   static const char hexdigits[] = {
     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
     'A', 'B', 'C', 'D', 'E', 'F' };

  const unsigned char *pBlob;
  sqlite_int64  d64;
  char  *zHex;
  char  *z = 0;

 /* Kll, extend to include optional width */

  switch( sqlite3_value_type(argv[0]) ) {
     case SQLITE_NULL:    
     case SQLITE_FLOAT:    return; break;    
     case SQLITE_INTEGER: { 
          d64 = sqlite3_value_int64(argv[0]);
          zHex =sqlite3_mprintf("0x%lx",d64);
          sqlite3_result_text(context, zHex, -1, sqlite3_free);
          return;
     } 
     break;
     case SQLITE_TEXT:
          pBlob = sqlite3_value_text(argv[0]);
          n = sqlite3_value_bytes(argv[0]);
     break;
     case SQLITE_BLOB:
          pBlob = sqlite3_value_blob(argv[0]);
          n = sqlite3_value_bytes(argv[0]);
     break;
      default: return ; 
  }


  if( n*2+1>SQLITE_MAX_LENGTH ){
    //sqlite3_result_error_toobig(context);
    return;
  }
  z = zHex = sqlite3_malloc(n*2 + 3);
  if( zHex==0 ) return;

  for(i=0; i<n; i++, pBlob++){
    unsigned char c = *pBlob;
    *(z++) = hexdigits[(c>>4)&0xf];
    *(z++) = hexdigits[c&0xf];
  }
  *z = 0;
  sqlite3_result_text(context, zHex, n*2, sqlite3_free);
}


_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to