Read array in sequence, and convert it's contents into single variable?


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default Read array in sequence, and convert it's contents into single variable?

    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 ?

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Read array in sequence, and convert it's contents into single variable?

    like this maybe


    Code:
    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 ]
    Warning I'm not a teacher

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Read array in sequence, and convert it's contents into single variable?

    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?

  4. #4
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Read array in sequence, and convert it's contents into single variable?

    I want to convert it to decimal and use in that way.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Read array in sequence, and convert it's contents into single variable?

    Sorry, I'm confused on this. You want to take each nibble and convert it to a decimal value, or???

  6. #6
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Read array in sequence, and convert it's contents into single variable?

    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.

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Read array in sequence, and convert it's contents into single variable?

    so where is the problem ?
    what have you tried ?
    Warning I'm not a teacher

Similar Threads

  1. Lookup with variable contents??
    By elcrcp in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th June 2015, 10:21
  2. Read/write a single bit (MCP23018)
    By vladimir059@hot in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 28th June 2011, 05:11
  3. Saving variable(s) contents before power is removed
    By emavil in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 15th July 2007, 18:26
  4. How do I convert an internal variable into frequency
    By schlaray in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 13th February 2007, 08:26
  5. Convert a word variable to decimal
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 4th December 2004, 21:02

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts