Hi there,
I am back to the storing byte array issue again. Thanks to all your help I
am using the X' sqlite flag to insert the byte array which I before
converted into a string of hex values. Works fine except when the byte value
is larger then 191. Try code snippet below, I assume the reason is that 192
is not a recognized character in sqlite, when its stored in the db its
apparently stored as 65533. From a inserting/reading perspective is there
something I am doing wrong? The code has some log statements to observe what
is happening
var a=[10,190,191,192];
var hexstring="";for(var i=0;i<a.length;i++){var
_h="";if(a[i]<16){_h+="0"}_h+=a[i].toString(16);console.log(_h);hexstring+=_h;}
console.log(hexstring);
openDatabase('_test_', 1.0, '_test-', 5000).transaction(function(tx) {
tx.executeSql("DROP TABLE IF EXISTS MYTABLE", [], function(){
tx.executeSql("CREATE TABLE IF NOT EXISTS MYTABLE(content BLOB); ", [],
function(){
tx.executeSql("INSERT INTO MYTABLE values(X'"+hexstring.toUpperCase()+"')",
[], //'?' aint working with
function()
{
tx.executeSql("SELECT content FROM MYTABLE ", [],
function(transaction, results)
{
var s=results.rows.item(0).content;
for(var i=0;i<s.length;i++)
{
console.log(s.charAt(i).charCodeAt(0));
}
});
},function(transaction, error){console.log(error)})
},function(transaction, error){console.log(error)})
})
})
I think I have a solution which I wanted to avoid. Having an array 256
characters that do not cause problems and using the array as a map when
storing and reading the data. I just wanted to avoid that since its pretty
much a hack.
Any Ideas?
Appreciate it,
Stan
On Mon, Apr 11, 2011 at 10:33 PM, RobG <[email protected]> wrote:
>
>
> On Apr 12, 2:07 am, "Peter Rust" <[email protected]> wrote:
> > Stan,
> >
> > I just posted a comment to the SO post:
> >
> > Assuming SQLite returns a hexadecimal string that looks like
> > "611006F462424C4183578E51B87021", you can iterate every two characters,
> > slice, parseInt and push onto an array. Since every two characters
> > represents a byte, just write a for loop that increase the index by 2
> every
> > time instead of by 1. You can grab the two characters via String.slice(),
> > like so: hex.slice(ix, ix+2). You can convert these to an integer via
> > parseInt(str, 16). So it would look like:
> >
> > arr = [];
> >
> > for(var ix = 0; ix < hex.length; ix = ix+2) {
> >
> > arr.push(parseInt(hex.slice(ix, ix+2), 16));
> >
> > }
>
> Assuming the input is character pairs of hex values, a more efficient
> function (removing calls to push amd slice) may be:
>
> var hexToDec = (function() {
> var re = /../g;
> return function (hexString) {
> var bytes = hexString.match(re);
> var i = bytes.length;
> while (i--) {
> bytes[i] = parseInt(bytes[i],16);
> }
> return bytes;
> }
> }());
>
>
> Example:
>
> hexToDec('611006F462424C4183578E51B87021')
>
> returns: [97,16,6,244,98,66,76,65,131,87,142,81,184,112,33]
>
>
> --
> Rob
>
> --
> You received this message because you are subscribed to the Google Groups
> "iPhoneWebDev" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/iphonewebdev?hl=en.
>
>
--
"Local color. Soak it up"
Virginia Vidaura
http://www.merkwelt.com/people/stan/
--
You received this message because you are subscribed to the Google Groups
"iPhoneWebDev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/iphonewebdev?hl=en.