[sqlite] How can I change big-endian to little-endian in the database file

2007-04-03 Thread Martin Pfeifle
Dear all,
in an upcoming project, it is required to store all integer values as little 
endian
instead of big endian (don't ask why).
Nevertheless, I would like to use SQLite in that project.
What do we have to change in the sqlite library, 
if we store the integers as little endian.
I came across some functions in B-tree.c and pager.c.

In B-tree.c
/* Read or write a two- and four-byte big-endian integer values.*/
get2byte,
get4byte,
put2byte,
put4byte 
/* pager.c*/
** All values are stored on disk as big-endian.
*/
read32bits,
write32bits
 
Is that enough.
Another question, what do we have to change if we would
also store the utf-chars as little endian?
I appreciate your help.
Martin






___ 
Der frühe Vogel fängt den Wurm. Hier gelangen Sie zum neuen Yahoo! Mail: 
http://mail.yahoo.de

Re: [sqlite] How can I change big-endian to little-endian in the database file

2007-04-03 Thread drh
Martin Pfeifle [EMAIL PROTECTED] wrote:
 Dear all,
 in an upcoming project, it is required to store all integer 
 values as little endian instead of big endian (don't ask why).
 Nevertheless, I would like to use SQLite in that project.
 What do we have to change in the sqlite library, 
 if we store the integers as little endian.
 I came across some functions in B-tree.c and pager.c.
 
 In B-tree.c
 /* Read or write a two- and four-byte big-endian integer values.*/
 get2byte,
 get4byte,
 put2byte,
 put4byte 
 /* pager.c*/
 ** All values are stored on disk as big-endian.
 */
 read32bits,
 write32bits
  
 Is that enough.

I think so.  Big-endian numbers have the nice property that
memcmp() compares positive integers in numerical order.  I
don't think we are using that property anywhere, but I might
be mistaken.  So if you do come up with a little-endian version
of SQLite, you will want to test it very carefully.

 Another question, what do we have to change if we would
 also store the utf-chars as little endian?

PRAGMA encoding=utf-16le;

The le at the end is the important part.

--
D. Richard Hipp  [EMAIL PROTECTED]


-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] How can I change big-endian to little-endian in the database file

2007-04-03 Thread drh
Martin Pfeifle [EMAIL PROTECTED] wrote:
 
 in an upcoming project, it is required to store all 
 integer values as little endian instead of big endian...

Does that mean you are not allowed to use TCP/IP which
stores everything big-endian?  ;-)

--
D. Richard Hipp  [EMAIL PROTECTED]


-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] How can I change big-endian to little-endian in the database file

2007-04-03 Thread Jakub Ladman
I think there is no need to change endianess.
Sqlite is shadowing out this and similar low level aspects.
If you need to get data from database and use it in something what needs it in 
other endianess, you may use something like this.

int change_endianess(unsigned char *data, int length)
{
unsigned char *buffer;
int i;
buffer = malloc(length);
for(i=0;ilength;i++)
{
buffer[i] = data[length - i];
}
memcpy(data, buffer, length);
free(buffer);
return length;
} 

For example, i have whole system with big endian, but only ferroelectric ram 
connected via i2c bus needs addresses in little endian, address is an 16bit 
number, so i must to swap the two bytes.

*((unsigned char *)(buffer[0])) = addr  8;
*((unsigned char *)(buffer[1])) = addr  0xff;

If i understand it correctly, changing physical representation of data in the 
database conflicts with SQL and relational databases principle.

Jakub

Dne úterý 03 duben 2007 09:49 Martin Pfeifle napsal(a):
 Dear all,
 in an upcoming project, it is required to store all integer values as
 little endian instead of big endian (don't ask why).
 Nevertheless, I would like to use SQLite in that project.
 What do we have to change in the sqlite library,
 if we store the integers as little endian.
 I came across some functions in B-tree.c and pager.c.

 In B-tree.c
 /* Read or write a two- and four-byte big-endian integer values.*/
 get2byte,
 get4byte,
 put2byte,
 put4byte
 /* pager.c*/
 ** All values are stored on disk as big-endian.
 */
 read32bits,
 write32bits

 Is that enough.
 Another question, what do we have to change if we would
 also store the utf-chars as little endian?
 I appreciate your help.
 Martin






 ___
 Der frühe Vogel fängt den Wurm. Hier gelangen Sie zum neuen Yahoo! Mail:
 http://mail.yahoo.de

-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] How can I change big-endian to little-endian in the database file

2007-04-03 Thread John Stanton
A very efficient way to re-arrange the byte ordering is to use a free 
union.  No function calls involved or tests.


Jakub Ladman wrote:

I think there is no need to change endianess.
Sqlite is shadowing out this and similar low level aspects.
If you need to get data from database and use it in something what needs it in 
other endianess, you may use something like this.


int change_endianess(unsigned char *data, int length)
{
unsigned char *buffer;
int i;
buffer = malloc(length);
for(i=0;ilength;i++)
{
buffer[i] = data[length - i];
}
memcpy(data, buffer, length);
free(buffer);
return length;
} 

For example, i have whole system with big endian, but only ferroelectric ram 
connected via i2c bus needs addresses in little endian, address is an 16bit 
number, so i must to swap the two bytes.


*((unsigned char *)(buffer[0])) = addr  8;
*((unsigned char *)(buffer[1])) = addr  0xff;

If i understand it correctly, changing physical representation of data in the 
database conflicts with SQL and relational databases principle.


Jakub

Dne úterý 03 duben 2007 09:49 Martin Pfeifle napsal(a):


Dear all,
in an upcoming project, it is required to store all integer values as
little endian instead of big endian (don't ask why).
Nevertheless, I would like to use SQLite in that project.
What do we have to change in the sqlite library,
if we store the integers as little endian.
I came across some functions in B-tree.c and pager.c.

In B-tree.c
/* Read or write a two- and four-byte big-endian integer values.*/
get2byte,
get4byte,
put2byte,
put4byte
/* pager.c*/
** All values are stored on disk as big-endian.
*/
read32bits,
write32bits

Is that enough.
Another question, what do we have to change if we would
also store the utf-chars as little endian?
I appreciate your help.
Martin






___
Der frühe Vogel fängt den Wurm. Hier gelangen Sie zum neuen Yahoo! Mail:
http://mail.yahoo.de



-
To unsubscribe, send email to [EMAIL PROTECTED]
-




-
To unsubscribe, send email to [EMAIL PROTECTED]
-