Currently using the following to take a string (like MAC address) and convert it to a 6 byte blob, I am unsure of the efficiency of compilestring() and wanted to see if anyone had anything better?
function stringToBlob(str) { local length = str.len()/2; local ret = blob(length); for (local idx = 0; idx < length; idx+=1) { local part = compilestring("return 0x"+str.slice(idx*2, (idx*2+2))+";")(); ret.writen(part, 'b'); } return ret; }
Is it documented anywhere what is removed from squirrel? Would help for those of us programming and testing locally then pushing to production since there isn’t a great staging methodology for the imp.
Well, compilestring() doesn’t exist on the imp or agent, for starters…
Assuming well formed input, this works well:
function stringToBlob(str) { local length = str.len()/2; local ret = blob(length); for (local idx = 0; idx < length; idx+=1) { local part = str.slice(idx*2, (idx*2+2)); ret.writen(((part[0]<='9'?(part[0]-48):(part[0]-87))<<4)+((part[1]<='9'?(part[1]-48):(part[1]-87))), 'b'); } return ret; }
But we removed compilestring() as a security measure… didn’t we?
I’d be tempted to do something with “0123456789abcdef”.find(s.toupper()), though I agree it’s a shame that “0xAB”.tointeger() doesn’t work.
Here’s a version (eerily similar to ppetrosh) that I’ve been using for a long time.
function hexStrToBlob(hexStr){
// pad string with leading 0 if not even in length
if (hexStr.len() % 2 != 0 )
hexStr = "0" + hexStr
local length = hexStr.len()/2
local result = blob(length)
for (local i = 0; i < length; i+=1) {
local hi = hexStr[i*2] - '0'
if (hi > 9 )
hi = ((hi & 0x1f) - 7)
local lo = hexStr[i*2+1] - '0'
if (lo > 9 )
lo = ((lo & 0x1f) - 7)
result[i] = hi << 4 | lo
}
return result
}
From my experience, strings are much faster in Squirrel than blobs. The only exception is string.find, which seems to be unusually inefficient. If you are using blobs, you can using index references b[n] rather than b.writen() if you’ve allocated the appropriate size already.
Hi im new to all of this, I tried the last function that @coverdriven posted, to write an array hex to a sector of spiflash and seems like wrote into the sector without problems, so is there another function to convert that “manual blob” that is wrote on the sector into an hex again?
function blobToHexString(b) {
local s = "";
for (local i = 0 ; i < b.len() ; i++) s += format("%02x", b[i]);
// Comment out the following line if you don't want an '0x' prefix
s = "0x" + s;
return s;
}