cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with memcpy

GHrib.1
Associate III

Hello,

I am having a problem with simple line of code. If I run code bellow, the result seems off.

//Main job of this code is to tranform hex into float 
   
uint32_t hex = 0x3fc51eb8;
float f;
    
memcpy(&f, &hex, 4);

If i go into Live Expressions i get that f = 0x1 (hex), it should be f = 0x3fc51eb8. The strangest thing is that the same code works for example on an onlineGDB compiler.

I assumed the trouble was within compiler, therefore I tried to change from default GNU 11 to some others, but there were no change. Any idea what could be wrong?

I am using:

NUCLEO G474

cubeIDE Version: 1.10.1 Build: 12716_20220707_0928 (UTC)

2 REPLIES 2
GHrib.1
Associate III

I found out that everything works fine except maybe cubeIDE (Number format option in debug perspective). The problem is that there is something wrong with hex Number format. If the number format is set as Default, then floats have correct values. Is there a bug inside cubeIDE or do I not understand what does Number format option do.

I added another variable (float test = 5.3) inside the code from previous post. By changing Number format option i get.

  • Default (values are ok):

0693W00000aHF2UQAW.png 

  • Hex (only hex variable is correct):

0693W00000aHF2eQAG.png 

  • Decimal(only hex variable is correct):

0693W00000aHF2tQAG.pngIs this kind of behavior anticipated?

Seems unhelpful, but 1.54 probably doesn't print well as the actual number, in hex.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..