Python Forum
convert c code python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: convert c code python (/thread-34939.html)



convert c code python - satyanarayana - Sep-17-2021

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


RE: convert c code python - jefsummers - Sep-17-2021

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?


file write - satyanarayana - Sep-20-2021

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


RE: convert c code python - deanhystad - Sep-20-2021

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?


RE: convert c code python - satyanarayana - Sep-21-2021

(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


RE: convert c code python - deanhystad - Sep-21-2021

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