Converting a blob to a string

So I use crypto.sha256() and it generated a blob but I can’t for the life of me turn that into a text representation (string) of the hash… I tried blob.readstring(32) and blob.tostring() but I still just get a blob back…

If you’re trying to hash something, normally you don’t want the exact string that comes out, but instead a string showing the bytes as hex. If that’s what you’re looking for, then this works:
local s = “”;
foreach (b in dataBlob) s += format("%02x", b); // Forces leading 0 on msB <16 and is lower case

I find using format() to do it lets me better control how it gets converted exactly.


I just checked the system tests for the agent’s crypto.sign() function, and the squirrel source contains this:

function to_hex(value) {
    local str = "";

    foreach (v in value) { 
        str += format("%02x", v);

    return str;

…which, I think you’ll agree, is more-or-less identical.

Thank you both, this is what I was trying to do :slight_smile: