Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert c code python
#1
Hi friends,

Below is my C code
void WaveFormsBuffer::FillOutputWaveFormBufferData(char *pBuffer,int nBuffsize,uint16_t nConfig)
{
        short pchannelsBuffer[7][1024];
        short *pData = (short*)pBuffer;

        for(int i=0;i <((nBuffsize/2)/7);i++ )
        {
            for(int j=0;j <MAX_NUM_WAV_FILES;j++)
            {
                pchannelsBuffer[j][i] = *pData++;
            }
        }

   for(int k=0;k <MAX_NUM_WAV_FILES ; k++)
            {
                fwrite(&pchannelsBuffer[k][0],(nBuffsize/7),1,m_pFp[k]);
    }
}
this is my code I want to convert to python can anybody can help with coding I am new to python
Reply
#2
Straight translation of C code into Python will make for bad Python code. You are typically better off figuring out how to solve the problem in Python. From a high level, what are you trying to do?
Reply
#3
Hi freinds,

below line how to convert to python

fwrite(&pchannelsBuffer[k][0],1024,1,m_pFp[k]);

in c fwrite will take how many bytes to write in python how to write n bytes any example
Reply
#4
What you are writing is more interesting than how. Is pbuffer really going to be an array or list of short? Python supports short so it can talk to C, but it is not a commonly used datatype. Python really wants all numbers to be float or int. How are you getting pBuffer? And who will be using the output? Are there external requirements on the file format?

As jefsummers said, Python is not C. It is very unlikely that you will get good results thinking of this as C->Python translation. What is the problem you are trying to solve?
Reply
#5
(Sep-20-2021, 03:32 PM)deanhystad Wrote: What you are writing is more interesting than how. Is pbuffer really going to be an array or list of short? Python supports short so it can talk to C, but it is not a commonly used datatype. Python really wants all numbers to be float or int. How are you getting pBuffer? And who will be using the output? Are there external requirements on the file format?

As jefsummers said, Python is not C. It is very unlikely that you will get good results thinking of this as C->Python translation. What is the problem you are trying to solve?


pbuffer has filled with signed int values i want to fill data to two dimensional array in python
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 1,914 Dec-17-2022, 12:24 AM
Last Post: snippsat
  How to convert a python code to binary? rsurathu 0 1,771 Aug-02-2020, 08:09 AM
Last Post: rsurathu
  Error in the code ->ValueError: could not convert string to float: ' ' eagleboom 1 3,184 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,390 Jun-14-2019, 03:02 PM
Last Post: DeaD_EyE
  code help to convert log file to csv format bmunagala 3 12,089 Jan-21-2019, 06:08 PM
Last Post: snippsat
  Convert arduino code to python JDutch 3 22,524 Nov-07-2018, 09:16 PM
Last Post: Larz60+
  Convert C code to Python code lostMarbles 6 32,581 Sep-19-2018, 06:23 AM
Last Post: pcsailor
  convert v2 code to v3 pandeyrajeev80 2 2,649 Aug-23-2018, 11:12 AM
Last Post: DeaD_EyE
  How to convert matlab code to python? Ricop522 0 5,189 Jan-13-2018, 01:55 AM
Last Post: Ricop522
  Looking code to convert my strings of line in Dict santosh 1 2,599 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