Page 1 of 1

Converting array C# to PB

Posted: Mon Feb 12, 2024 4:22 pm
by Otrebor
Hi
I'm having difficulty adapting to PureBasic.
This array:

Code: Select all

_frameInValues = new byte[_numberOfFrames][];
_numberOfFrames is defined at the beginning and not change, but the second element is variable.

Code: Select all

     _frameInValues[i] = new byte[_numberOfInReads[i]];

     for (int j = 0; j < _numberOfInReads[i]; j++)
     {
         _frameInValues[i][j] = decompresseddFrames[frameArrayPos + 4 + j];
     }
Example, if _numberOfFrames = 100, occurs these situations:

Code: Select all

_frameInValues [0] [5]= 255,200,255,200,255
                         _frameInValues [0] [0]=255
                         _frameInValues [0] [1]=200
                         _frameInValues [0] [2]=255     
                         _frameInValues [0] [3]=200
                         _frameInValues [0] [4]=255

_frameInValues [1] [2]= 126,125
_frameInValues [2] [7]= 126,125,23,56,44,123,34
...
_frameInValues [99] [1]=0
Someone can help with this conversion?

Re: Converting array C# to PB

Posted: Mon Feb 12, 2024 6:35 pm
by Olli
First info :

the byte range in C is between 0 and 255.

This seems like the ascii type in pureBasic.

Re: Converting array C# to PB

Posted: Mon Feb 12, 2024 6:37 pm
by Olli
Second information :

it is a question. Do you need the two dimensions are variable or, just only one ?

Waiting your answer, here is an example of array building, when two dimensions can be changed :

Code: Select all

structure column
 array cell.a(0)
endstructure

dim *frame.column(3) ; 3+1 columns allocated

columnHeight = 15 ; 15+1 cells per column

for i = 0 to 3
 *frame(i) = allocatememory(sizeof(column) )
 initializestructure(*frame(i), column)
 redim *frame(i)\cell(columnHeight)
next
If only one dimension must be able to be modifed, the syntax will be simpler : a single static array should do the affair.

Re: Converting array C# to PB

Posted: Mon Feb 12, 2024 7:39 pm
by Otrebor
At the beginning of C# code it is defined with 2 elements:

Code: Select all

private byte[][] _frameInValues;
While debugging i see that the range of first element is from 0 to 65535.
The range of second element is from 0 to 255, but not constant.
The second element is defined before loop.
But with new interaction its change the value:

Code: Select all

 _frameInValues[i] = new byte[_numberOfInReads[i]];
Then populated on loop by the line:

Code: Select all

  _frameInValues[i][j] = decompresseddFrames[frameArrayPos + 4 + j];
Thanks

Re: Converting array C# to PB

Posted: Tue Feb 13, 2024 3:56 pm
by Olli
So, in this way, you can use the simpler static array :

Code: Select all

Dim _frameInValues.A(65535, 255) ; 16 megabytes sized array
pureBasic allows the coder to resize the second argument (255).

Code: Select all

Redim _frameInValues.A(65535, 383)

Re: Converting array C# to PB

Posted: Tue Feb 13, 2024 9:15 pm
by Otrebor
HI
Sorry, but i can not follow how ReDim can be used in my case. Some values will be lost in this example:

Code: Select all

_numberOfInReads=3
Dim _frameInValues.a(500, _numberOfInReads)
_frameInValues=0

_frameInValues.A(_frameInValues,0)=255
_frameInValues.A(_frameInValues,1)=3
_frameInValues.A(_frameInValues,2)=12

_frameInValues + 1
_numberOfInReads=5
ReDim _frameInValues.A(500,_numberOfInReads)
_frameInValues.A(_frameInValues,0)=200
_frameInValues.A(_frameInValues,1)=20
_frameInValues.A(_frameInValues,2)=125
_frameInValues.A(_frameInValues,3)=30
_frameInValues.A(_frameInValues,4)=255

_frameInValues + 1
_numberOfInReads=1
ReDim _frameInValues.A(500,_numberOfInReads)
_frameInValues.A(_frameInValues,0)=0

;...

_frameInValues + 498
_numberOfInReads=2
ReDim _frameInValues.A(500,_numberOfInReads)
_frameInValues.A(_frameInValues,0)=128
_frameInValues.A(_frameInValues,1)=15
But it is ok. I will try to do a workaround including some other array(s) , and perhaps achieve the same result.
Thank you!

Re: Converting array C# to PB

Posted: Tue Feb 13, 2024 9:50 pm
by Olli
All right. See you last message, you have to use the previous template code I presented (dynamic arrays), but exchange horizontal and vertical table objects.

Code: Select all

structure row
 array cell.a(0)
endstructure

procedure iDim(*frame.integer)
*frame\i = allocateMemory(sizeOf(row) )
initializestructure(*frame\i, row)
endProcedure

procedure iErase(*frame.integer) ; destroys a row
clearstructure(*frame\i, row)
freememory(*frame\i)
endProcedure



dim *frame.row(65535)

iDim(@*frame(0) )
with *frame(0)
redim \cell(2)
\cell(0) = 255
\cell(1) = 3
\cell(2) = 12
endWith

iDim(@*frame(1) )
with *frame(1)
redim \cell(4)
\cell(0) = 200
\cell(1) = 20
\cell(2) = 125
\cell(3) = 30
\cell(4) = 255
endWith

; etc...


; note, if u want to delete the row #0 :
iErase(@*frame(0) )

; if u want to modify an existing cell :
*frame(y)\cell(x) = 123

Re: Converting array C# to PB

Posted: Tue Feb 13, 2024 10:25 pm
by mk-soft
Direct memory access ...

Code: Select all

Structure ArrayOfUByte
  a.a[0]
EndStructure

#max_col = 16
#max_row = 8

Macro arr(col, row)
  (#max_col * row + col)
EndMacro

*mem.ArrayOfUByte = AllocateMemory(#max_col * #max_row)

*mem\a[arr(0,0)] = 1
*mem\a[arr(15,0)] = 2
*mem\a[arr(0,7)] = 10
*mem\a[arr(15,7)] = 11

ShowMemoryViewer(*mem, #max_col * #max_row)

Re: Converting array C# to PB

Posted: Wed Feb 14, 2024 7:28 am
by Olli
mk-soft wrote: Tue Feb 13, 2024 10:25 pm Direct memory access ...
It is not the right way. In fact, what Otrebor wants, it is just a array of strings which support Chr(0).

From that, I presented the right code (normally without bug) in my last message.