What replaces scanf() is Squirrel?

Hi,

I have a string read from an EEPROM using the I2C bus. The data represent the values in a ‘C’ structure, e.g., the first 33 or so bytes represent in order:
int, int, string[16], byte, int, byte, byte, long, long

I would like to display them as decimal values, when the type is int, byte and long, and as an ASCII string for the string[16].
Can they be read into a structure and then indexed out for sending to server.log each followed by a new line?

Thanks

scanf() wouldn’t have helped here (it wouldn’t deal with embedded zeros, for example) but there isn’t one in squirrel so…

It’s a bit manual. The code below assumes little-endian ints/longs.

`function getint(s,o) { return(s[o]|(s[o+1]<<8)); }
function getlong(s,o) { return(s[o]|(s[o+1]<<8)|(s[o+2]<<16)|(s[o+3]<24)); }

local res=i2c.read(xxxx);
if (res != null) {
server.log("%04x %04x %s %02x %04x %02x %02x %04x %04x",
getint(res,0), getint(res,2), res.slice(4,20), res[20], getint(res,21), res[23], res[24],
getlong(res,25), getlong(res,29));
}
`

Or you could put the data in a blob and use readn() - which would make the integers less icky but not really help with the string.

Peter

Thanks for your responses which I will pursue to help learn Squirrel. However, is a parallel to the following approach possible

In ‘C’ when I read from this EEPROM I use a union of a string and a structure.

Union EE_Data { Unsigned Char raw_data[ 33]; Struct Work_Data { int var1; int var2; char char_str[16]; //includes \\0 at end of string unsigned char Byte1; int var3; unsigned char Byte2; unsigned char Byte3; long var4; long var5; } };

I2C reads into EE_Data.raw_data and I access what I want via the structure
int a = EE_Data.Work_Data.var2;

~ Martin

Edit - I wrapped your code < code > </ code > tags (the “C” in the formatting bar).

Thanks, I will do this from now on. ~ M

Peter’s suggestion of using a series of readn() with the right type indication is the most straithforward and works pretty well. Only thing to take care of is making sure you put them in the right order as they appear in the struct.
You only have to write a small piece of code to retrieve the string by reading 16 bytes, cast them to char and add them to a string

function readstr(len,blob) { local str = ""; for (local i = 0; i < len; i++) { local byte = blob.readn('b'); str += byte.tochar() ; } return str; }

Thanks to all for your help, ~ Martin