Printing Hex into Serial Port

Hello! Can someone please help me identify where I am going wrong in this code. I am trying to print out the hexidecimal value exactly as it is shown.

char msgString[128];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
}

void loop() {
  // put your main code here, to run repeatedly:
  uint64_t receivedMsg = 0x1000000100000000;
  sprintf(msgString, "Incoming message: 0x%.16llX",receivedMsg);
  Serial.println(msgString);
  delay(1000);
}

looks correct for me:

Incoming message: 0x1000000100000000

what do you expect instead?

What do you see in the Serial Monitor after the execution of: Serial.println(msgString)?

That's exactly what I expect, however I only see
Incoming message: 0x

Incoming message: 0x

it works on a 32bit microcontroller like the ESP32.
On which microcontroller are you testing?

I am testing using Arduino UNO

do you need it in a buffer (which is later used to be printed) or do you want to get it printed on Serial i first place only?

this post will give you some ideas for both ways:

I am mainly printing it for verification purposes at the moment. I just want some way of seeing if I am correctly doing bitwise operations and things like that. It's for a larger project and I'm struggling with some of the basics. Thanks for linking that post though, I will go check it out

UNO does not know how to print uint64_t

careful as well if you use Arduino's bit manipulation macros as some are tailored for 32 bits max

(see the UL and not ull)

Why not sprintf(msgString, "Incoming message: 0x%.16X",receivedMsg); ?

without the 11 between 16 and X

because that would be for an unsigned integer (in hexadecimal format) and not a unsigned long long

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.