Negative Hex to Float

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
ScEn_283436
Level 5
Level 5
5 sign-ins First solution authored 100 replies posted

Using  PSoC Creator 4.2 for  the PSoC 5. I am monitoring a thermoelectric cooler (TEC) controller that reports both positive and negative currents and voltages.  As long as everything is positive I'm ok. I'm taking an ascii string representation of hex numbers and converting it to a long that is stored in a uint32. If I get a negative number and apply the following, I always get 0x7FFFFFFF.

h2 is a global uint32 (hex 2 byte)

h2 = strtol(cbuf, (char**)NULL, 16); // create the uint32 from the payload

I'm sure there is an easy way to do this without having to write a bunch of low level code to sort the sign bit, exponent and mantissa.  Should I cast h2 as float  and use atof to convert it? Somebody has surely done this.

Thank you,

Nick

 

0 Likes
1 Solution
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I would try...

#include "project.h"
#include "stdio.h"

...

int32_t h2 ; // btw int32_t or uint32_t are 4 bytes

sscanf(cbuf, "%x", &h2) ;

...

moto

View solution in original post

0 Likes
1 Reply
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I would try...

#include "project.h"
#include "stdio.h"

...

int32_t h2 ; // btw int32_t or uint32_t are 4 bytes

sscanf(cbuf, "%x", &h2) ;

...

moto

0 Likes