in reply to Printing byte as 2 Hex characters

You should include the output of your program in a second code tag. Perhaps the value you read was zero? Pipe it to a file and copy paste for accuracy.

Also, I expect you probably would prefer the format printf('0x%2X'...

PS: an error message of 'Could not open file' is very vague. Try "Could not open file '$data_file': OS told me '$!'\n"