How to use format() with a blob?

I have some code like the below, where I receive bytes one by one, and place them into a blob (I couldn’t make strings work, and another thread here seems to suggest blobs is the way to go).
Now I’d like to output the received bytes as part of a formatted string, but I constantly get the error “ERROR: string expected for the specified format” (or similar if I use other variable qualifiers).

I understand the error as %s means a string, and a blob is not a string, which I fully agree on. But I haven’t been able to locate any info about now to perform the conversion from blob to string, so I can use it with the format function.

local PNM = blob(6);

PNM[0] = spi.receive();
PNM[1] = spi.receive();
PNM[2] = spi.receive();
PNM[3] = spi.receive();
PNM[4] = spi.receive();
PNM[5] = 0;

hardware.uart1289.write(PNM); // 5 x 8 bit ASCII - this works.
hardware.uart1289.write(format(" Product Name: %s",PNM)); // 5 x 8 bit ASCII - this produces the error.

I’m looking forward for your response.

There isn’t currently a neat way to do that, and you’re quite right that there sort of should be ("%b", maybe). Note however that, because UART output is buffered,
hardware.uart1289.write(format(" Product Name: %b",PNM));
would, if it existed, do exactly the same thing as:
hardware.uart1289.write(" Product Name: "); hardware.uart1289.write(PNM);


…also, you can make your SPI receive code a bit neater on release-14:

local PNM = spi.readblob(5); PNM[5]=0; // blob will auto-expand

Thanks, Peter and Hugo.
Looking forward to try the new spi.readblob() function!

You can use it now, release-14 is what everyone has :slight_smile: