The first argument to write!() is the writer you are writing to, not the format string. (Otherwise, how would it know what are you writing to?)
By the way, why are you allocating separate strings for the formatting? That's unnecessary. Just write the whole
format string in one go, directly to the formatter.
Also, when you print bytes in hexadecimal, you should always pad them to be 2 characters long, or you may end with missing zeroes when you concatenate multiple bytes like you do here.
The format string should be "0x{:02X}{:02X}{:02X}"
I found it after comparing what I typed with what you provide. Yes it is something else, it is a bit trivial and slightly easy to overlook at first. Anyway thanks for solving this problem.