I have some numeric data that is given to me in INTEGER
form. I insert it along with other data into SQLite but when I write it out The INTEGER
Numbers need to be 8 digit Hex
Numbers with leading zeros.
Input
400
800
25
76
Output
00000190
00000320
00000019
0000004C
Originally I was converting them as I read them in and storing them as TEXT
like this.
stringstream temp;
temp << right << setw(8) << setfill('0') << hex << uppercase << VALUE;
But life is never easy and now I have to create a second output in INTEGER
form not HEX
. Is there a way to convert INTEGER
numbers to HEX
or HEX
numbers to INTEGERS
in SQLite?
I'd like to avoid using C++ to change data after it is in SQLite because I've written a few convent export functions that take a queries result and print them to a file. If I need to touch the data during the queries return I couldn't use them.
I've looked at the HEX()
function in SQLite but that didn't have desired results. Could I make a function or would that be very inefficient? I'm doing this over a really big data set so anything expensive should be avoided.
Note: I'm using SQLites C/C++ interface with Visual Studios 2010.
To convert a hexadecimal string to a numberUse the ToInt32(String, Int32) method to convert the number expressed in base-16 to an integer. The first argument of the ToInt32(String, Int32) method is the string to convert. The second argument describes what base the number is expressed in; hexadecimal is base 16.
SQLite hex() function The hex() function interprets its argument as a BLOB and returns a string which is the upper-case hexadecimal rendering of the content of that blob.
HEX() : This function in MySQL is used to return an equivalent hexadecimal string value of a string or numeric Input. If the input is a string then each byte of each character in the string is converted to two hexadecimal digits.
You can use sqlite3 printf() function:
CREATE TABLE T (V integer);
insert into T(V) values(400), (800), (25), (76);
select printf('%08X', V) from T;
You can use sqlite3_create_function
. See an example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With