Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert c code python
#6
Signed? Integers are not sized in Python, so there is no unsigned int type unless you are using numpy or the ctypes library. Array? Python has lists, not arrays. There are Python packages/libraries that support arrays such as numpy or array. Are you using one of those?

If I was using numpy the code might look like this:
import numpy as np

def split_and_write(data, channels, filename):
    '''Split multiplexed time history data into channels and write each channel to own file file'''
    buffer = np.transpose(data.reshape((-1, channels)))
    for i, row in enumerate(buffer):
        row.tofile(f'{filename}{i+1}.bin')

data = np.array(range(10*7))  # Create array [0..69]
split_and_write(data, 7, 'data')  # Creates files data1.bin through data7.bin
This splits an array [0..69] into the following arrays and writes them to files
Output:
[ 0 7 14 21 28 35 42 49 56 63] -> data1.bin [ 1 8 15 22 29 36 43 50 57 64] -> data2.bin [ 2 9 16 23 30 37 44 51 58 65] -> data3.bin [ 3 10 17 24 31 38 45 52 59 66] -> data4.bin [ 4 11 18 25 32 39 46 53 60 67] -> data5.bin [ 5 12 19 26 33 40 47 54 61 68] -> data6.bin [ 6 13 20 27 34 41 48 55 62 69] -> data7.bin
When I look at a hex dump of the data1.bin I see:
Output:
00 00 00 00 07 00 00 00 0E 00 00 00 15 00 00 00 1C 00 00 00 23 00 00 00 2A 00 00 00 31 00 00 00 38 00 00 00 3F 00 00 00
Raw binary is not a good way to save information, so if I was writing this file to be processed by a Python program I would save the metadata with the binary information so it could be used on machines with different byte order
Reply


Messages In This Thread
convert c code python - by satyanarayana - Sep-17-2021, 12:12 PM
RE: convert c code python - by jefsummers - Sep-17-2021, 06:12 PM
RE: convert c code python - by deanhystad - Sep-20-2021, 03:32 PM
RE: convert c code python - by satyanarayana - Sep-21-2021, 05:19 AM
RE: convert c code python - by deanhystad - Sep-21-2021, 07:45 PM
file write - by satyanarayana - Sep-20-2021, 12:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 2,093 Dec-17-2022, 12:24 AM
Last Post: snippsat
  How to convert a python code to binary? rsurathu 0 1,844 Aug-02-2020, 08:09 AM
Last Post: rsurathu
  Error in the code ->ValueError: could not convert string to float: ' ' eagleboom 1 3,258 Nov-29-2019, 06:19 AM
Last Post: ThomasL
  Can someone please help me convert this simple C ROT cipher code to Python code? boohoo9 5 3,540 Jun-14-2019, 03:02 PM
Last Post: DeaD_EyE
  code help to convert log file to csv format bmunagala 3 12,226 Jan-21-2019, 06:08 PM
Last Post: snippsat
  Convert arduino code to python JDutch 3 22,972 Nov-07-2018, 09:16 PM
Last Post: Larz60+
  Convert C code to Python code lostMarbles 6 32,834 Sep-19-2018, 06:23 AM
Last Post: pcsailor
  convert v2 code to v3 pandeyrajeev80 2 2,730 Aug-23-2018, 11:12 AM
Last Post: DeaD_EyE
  How to convert matlab code to python? Ricop522 0 5,262 Jan-13-2018, 01:55 AM
Last Post: Ricop522
  Looking code to convert my strings of line in Dict santosh 1 2,686 Nov-10-2017, 04:49 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

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