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? 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.binThis splits an array [0..69] into the following arrays and writes them to files When I look at a hex dump of the data1.bin I see: 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
|