Squirrel conversion question

I am struggling a bit with the Squirrel language and miss not being able to cast. For compatibility with another device to which I am sending serial data in a predefined format, I need a function to convert a single ascii char into a decimal string equivalent - see function below. I keep hitting a brick wall in trying to convert a char ‘A’ into a byte that I can manipulate via bit operations. The only way I could think of was to use a blob to treat the char as a ‘b’ byte. However,
the diagnostic server.log(format(“char:%s”,achar)); shows an ascii ‘A’ coming in, but it gets loaded into the blob as a 0.
Any assistance would be greatly appreciated:
// Convert ascii char to decimal string // ex: char 'A' to string '65' function asciiToDecimal(achar) { server.log(format("%s",achar)) // diagnostic showing a valid ascii char comes in local s = blob(1); s.seek(0); s.writen( achar,'b'); // this always writes a 0 into the blob for some reason. s.seek(0); local i = s.readn('b'); server.log(format("?:%02X",i)); // diagnostic always showing a ?:0 local lo = i & 0x0F; local hi = (i >> 4) * 256; local result = hi + lo; return result.tostring(); }

`function asciiToDecimal(achar)
{
return achar.tostring();
}

local s = asciiToDecimal(‘A’);
server.log(s);
server.log(typeof(s));
server.log(s[0]);
server.log(s[1]);
`

04/04/2013 08:41:33 PM: 65
04/04/2013 08:41:33 PM: string
04/04/2013 08:41:33 PM: 54
04/04/2013 08:41:33 PM: 53

Is there any reason you can’t just use format("%d", c)?

server.log(format("%d", ‘A’)) logs “65”

Thanks @sjm and @Hugo.

@Hugo , My very first test was format("%d", achar) as it makes the most sense and I am familiar with the printf in other languages. But I get a very nasty red error - see log below. That is what sent me on the wild goose chase trying so many other things. The key was what @sjm pointed out and that was to use achar[0]. See test code below.
` local teststring = “ABCDEFG”;
server.log(teststring);
local achar = teststring.slice(0,1);
server.log(achar);
server.log(achar[0]);
server.log(teststring[0]);
server.log(teststring[1]);
server.log(teststring[2]);
server.log(teststring[3]);
server.log(format("%d", achar[0]));
server.log(format("%d", achar)); // Nasty errorr

Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): ABCDEFG
Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): A
Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): 65
Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): 65
Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): 66
Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): 67
Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): 68
Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): 65
Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): ERROR: integer expected for the specified format
Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): ERROR: at main:70`

Thanks!

The error message got cut off:
Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): ERROR: integer expected for the specified format
Thu Apr 04 2013 20:35:34 GMT-0700 (Pacific Daylight Time): ERROR: at main:70

Yes, that error is valid - “achar” is a string, albeit a 1 byte long one. Just as with C, you can’t pass a string in as a parameter for a %d format.

“achar[0]” is, however, a number, and so valid for %d.

It’s much more efficient to use indexes, eg string[0] if you’re trying to get a single character out of a string than to use string.slice().