user2845313
user2845313

Reputation: 13

Logstash Ruby convert Hex representation in a float

I receive from and IoT the temperature information in Hex format : 0x41d39ce6 But I need to convert it into a float value like this : 26.451610565185546875 Here is my ruby code from now :

event.set('[ln_temperature]',event.get('[ln_temperature_B_inversion]').to_s.hex)

With 'ln_temperature_B_inversion' variable is 41d39ce6

Thanks in advance for your help.

Upvotes: 0

Views: 454

Answers (1)

Amadan
Amadan

Reputation: 198456

["41d39ce6"].pack("H*").unpack("g").first
# => 26.451610565185547

Your hex string is big-endian standard 32-byte float. Use Array#pack to get a byte string from your hex, then String#unpack to convert it into a float.

Upvotes: 3

Related Questions