PDA

View Full Version : Read array in sequence, and convert it's contents into single variable?



CuriousOne
- 25th September 2016, 06:26
Hello, say I have 32 bit array, in each of it's "cells" is written either 1 or 0. I want to read this array in a way, that these cells add next to each other. Say, content of first 4 cells is "1" "1" "0" "1". How can I convert it into single byte variable, which will read as "1101" ?. If there were string variables, then task would be simple, by just reading current array position into string variable, and adding to itself. But in PBP we don't have string variables, how should I ?

richard
- 25th September 2016, 08:05
like this maybe



myarray var byte[4] ; a 32 bit array
nbyte var byte ;result

inx var byte ;start index in array
x var byte

arraywrite myarray,[105,100,10,116] ; fill array


inx=3 ; start a bit 3
nbyte=0 ;clr result

for x= 0 to 3 ;get the 4 bits
nbyte.0[x] = myarray.0[x+inx]
next

serout2 PORTa.0,84, [13,10,"nb ",bin4 nbyte ]

picster
- 25th September 2016, 14:46
How do you want to work with it afterwards? did you want to process it as decimal 1101 or binary 1101 or just "string" 1101?

CuriousOne
- 25th September 2016, 15:43
I want to convert it to decimal and use in that way.

picster
- 27th September 2016, 12:54
Sorry, I'm confused on this. You want to take each nibble and convert it to a decimal value, or???

CuriousOne
- 29th September 2016, 19:49
No, imagine array consists of "1" "0" "1" "1" digits, I want to "merge" them into one binary variable, which I will convert to decimal later.

richard
- 30th September 2016, 00:22
so where is the problem ?
what have you tried ?