Python Forum
convert from csv row to 16 bit bytes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert from csv row to 16 bit bytes
#1
I have a csv row like this:
1,5,14343,3456,6123,34002,4 
etc
I need to create a stream of bytes from it of size 16 bit -
So I want the above portion of the array to end up being an array like this:
0x0001,0x0005,0x3807,0x0D80,0x17EB,0x84D2,0x0004
This will get loaded into memory in my C program.
One part will be to do a join based on ','. After that use bytearray ??? But I'm not sure.
Thanks for any help.

I worked out the solution:
newArray = ''.join('{:04x}'.format(x) for x in myArray)
Reply
#2
iterate over list and pass each value to hex:
>>> print(hex(14343))
0x3807
>>> print(hex(34002))
0x84d2
>>>
Now do it in a loop
for item in row ...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert 4 bytes to an integer ? GiggsB 11 6,595 Jan-20-2022, 03:37 AM
Last Post: GiggsB
  replace bytes with other byte or bytes BigOldArt 1 10,524 Feb-02-2019, 11:00 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020