Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Conversion needed from bytearray to Floating point
#1
Hello All,

I am stuck in very strange situation. My python code is talking to some third party device and getting data which is in bytearray format. I need this final value in real/float format
import ctypes
import struct
import math
client = c.Client()
client.connect('192.168.27.11', 0, 3)

def get_db_row(db, start, size):
    data = client.db_read(db, start, size)
    return (data)
    client.disconnect()
    client.destroy()
.
It gives me following output when input is
Output:
bytearray(b'B\xc8\x00\x00')
which is equivalent to (42H C8H 00H 00H) and Float value 100.0

I have tried many ways Wall Wall but I am not able to convert this byte array to output value 100.0
Can anyone please help?

Thanks
Quote
#2
Using struct should do it.
>>> import struct
>>> 
>>> b = bytearray(b'B\xc8\x00\x00')
>>> b
bytearray(b'B\xc8\x00\x00')

>>> f = struct.unpack('>f', b)
>>> f
(100.0,)
>>> f[0]
100.0
>>> type(f[0])
<class 'float'>
ljmetzger likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  connecting the first point to the last point Matplotlib omar_mohsen 0 88 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen
  Complex floating issue arshad 2 429 Nov-05-2019, 03:26 PM
Last Post: arshad
  floating point arithmetic exDeveloper 2 221 Sep-25-2019, 04:33 PM
Last Post: DeaD_EyE
  finding the closest floating point number in a list Skaperen 17 872 Sep-19-2019, 10:39 PM
Last Post: Skaperen
  finding the next higher representable floating point value Skaperen 0 437 Sep-13-2019, 11:16 PM
Last Post: Skaperen
  int() function with bytearray() Jona66 1 221 Sep-08-2019, 12:41 PM
Last Post: ichabod801
  Subtracting values between two dictionaries/ floating point numbers FloppyPoppy 5 1,345 Mar-04-2019, 01:00 PM
Last Post: snippsat
  Value error when converting hex value to bytearray shubhamjainj 3 749 Mar-01-2019, 08:29 AM
Last Post: DeaD_EyE
  Windows DIB format in bytearray to image? dusca 2 749 Mar-28-2018, 10:35 PM
Last Post: dusca
  Bytearray questions mattps 2 11,840 Mar-25-2018, 06:54 AM
Last Post: mattps

Forum Jump:


Users browsing this thread: 1 Guest(s)