-2

I am looking for built-in C or C++ function that allows me to convert a float into an HEX string, so far I have used itoa , but it does not work with negative values since it works with unsigned for base 16, so I was wondering which one I could use instead that may handle the negative value.

Using itoa I loose my negative value as it can be seen below,

Acceleration X: -9 | X angle: **-0.5156689167**
Acceleration Y: -69 | Y angle: **-3.9565520286**
Acceleration Z: 986 | Z angle: 80.4013519287
Value of ACC per axe (x,y,z) in HEX ->ffcdfe751f68
Data to be send x ->**ffcd**
Data to be send y ->**fe75**
Data to be send z ->1f68

What other function could I use with the same functionality?

ndarkness
  • 1,011
  • 2
  • 16
  • 36
  • http://stackoverflow.com/questions/5100718/integer-to-hex-string-in-c – Marco Apr 07 '16 at 09:40
  • @Marco thanks, I saw that solution what I am not sure whether it will work in Arduino or not. – ndarkness Apr 07 '16 at 09:43
  • If you're not sure, try it out. – ayushgp Apr 07 '16 at 09:45
  • 1
    What do you want to achieve by converting a floating-point number to hex? Can't you just send the `float` as four data bytes? – M Oehm Apr 07 '16 at 09:46
  • @MOehm what that format is for printf right? The problem in here lies on the fact that I have to pass on the data in hex format to the function that will send it out – ndarkness Apr 07 '16 at 09:46
  • 1
    It is not clear what you need exactly. Do you send a string that contains a hex representation of the floating-point number? If so, can't the string contain arbitrary data? Or is the string really just an array of bytes, that is, any data? I ask because I have the feeling that you are barking up the wrong tree with your request to convert a float to hex. – M Oehm Apr 07 '16 at 10:14

1 Answers1

1

Looking at the results you provided I would say they are correct. You got the binary complement values:

ffcd = -51 in 16-Bit binary complement
fe75 = -395 in 16-Bit binary complement
1f68 = 8040 in 16-Bit binary complement

Devide it by 100 and you get your (rounded) floating point values.

atoi can handle negative values. It indicates the negative status by setting the most valued Bit of the binary representation to 1. You will not get a - sign, if you did expect one.

You can compute the binary complement by yourself by converting the (16-Bit) HEX value to a decimal and substract 65536 from the result.

e.g

ffcd -dec-> 65485 -sub-> 65485 - 65536 = -51 -float-> -51 / 100.0 = - 0.51
Peter Paul Kiefer
  • 2,114
  • 1
  • 11
  • 16
  • thank you very much I didn't know that itoa was encoding the sign on the most significative bit!! That is perfect! – ndarkness Apr 07 '16 at 10:52
  • @ Peter Paul Kiefer another question popped my up in my head, if the number is not negative, then if I do your procedure it would be wrongly converted, wouldn't it? SO as to check whether or not is negative I should check always the most significative bit of the number, right? – ndarkness Apr 07 '16 at 11:08
  • @ndarkness , I solved my question by using `strtol` to convert from HEX to dec – ndarkness Apr 07 '16 at 12:19
  • 1
    @ndarknes Sorry, I'm a bit late. Yes you are right. A positive number can be identified by a zero in the most significant bit or if the first hex digit is less than 8. The example I provide only works on negative numbers. For positive numbers you must not substract 65536 from the decimal result. And it isa good idea to use a library function (strtol) to convert to a decimal. – Peter Paul Kiefer Apr 07 '16 at 13:12