Shop OBEX P1 Docs P2 Docs Learn Events
Convert array of Bytes to one string? — Parallax Forums

Convert array of Bytes to one string?

ErlendErlend Posts: 612
edited 2013-04-01 11:18 in Propeller 1
Instead of comparing a byte array with an other, byte by byte, I had the idea to convert them to stings and do a Strcomp of the two.
How can I do this conversion?
dataRFID is declared as a Byte[12] where the first and last byte is not used.
strngRFID is declared in the parent object as a LONG[11], and the pointer is passed
repeat i from 1 to 10                              'For each byte  
        LONG [strngRFID] [i-1] := dataRFID[i]            'Copy to string variable  
      LONG [strngRFID] [11] := 0                         'Convert to zero-terminated string  

Learning, learning,

Erlend

Comments

  • Heater.Heater. Posts: 21,230
    edited 2013-03-31 05:39
    The only difference between a byte array and a string is that a string must have a zero byte on the end. To indicate, well, the end of the string. If you can achieve that then strcomp should work fine.

    Except of course if the data in your byte array happens to contain zeros....
  • Cluso99Cluso99 Posts: 18,069
    edited 2013-03-31 05:44
    all that a string is, is a set of bytes, terminated in a nul (0). So append a 0 to yourbytes.
  • ErlendErlend Posts: 612
    edited 2013-04-01 02:28
    String of bytes with a zero at the end, and with valid ascii codes in it. I knew that, or so I thought, but when I tried it I did not get the expected results on the pst screen. Turns out it is the pst (or rather the pc) which is the problem. I've still not found the problem. I can assure you it is not much fun to debug when the debugger acts buggy. I've got Win7p, in device manager, usb, there is one serial channel. I've tried everything from 9600 b/s to 115200 - and correspondingly with the pst. I've tried FullDuplexSerial and Parallax Serial Terminal. Nothing seems to help. One thing I wonder about; the pc shows only one serial port. Should there not be a list to choose from?
    Anyway, bytes-to-string works fine.

    Erlend
  • StefanL38StefanL38 Posts: 2,292
    edited 2013-04-01 05:10
    hi Erlend,

    maybe it is just that you are using longs.

    To use strings in SPIN you have to define an array of BYTE not of longs

    Your debugging will be much easier if you start with less complex constructions
    and if you start with democode where all things are well known to work properly.

    Start with a democode just doing "Hello world"
    then start changing this code in smallps
    I mean something simple like this
    VAR
      byte MyStr[10]   'remember one byte more for the terminating zero
    
    PUB Test
      MyStr[0] := "H"   'remember bytearray starts at index zero
      MyStr[1] := "e"
      MyStr[2] := "l"
      MyStr[3] := "l"
      MyStr[4] := "o"
      MyStr[0] := 0    'zerotermination
    
      repeat
        WaitCnt(ClkFreq + cnt)
        pst.Str(string"#"))  'start-sign
        pst.Str(@MyStr)
        pst.Str(string"*"))  'end-sign
    

    modify this code in small steps.

    25 years of programming learned me if you want it to work superfast (coding it all at once an start testing if first version is finished) it will turn out superslow
    because there are so many places where the bug COULD be.

    Start with working democode and change it in small steps. Testing after EACH change will turn out to be faster because the bug must always be in the LAST change.

    best regards
    Stefan
  • ErlendErlend Posts: 612
    edited 2013-04-01 11:18
    Thanks. Doing it my way I probably spend much more time before I crack it. I know you're right of course, but I do not claim to be rational. I love start climbing a mountain instead of start climbing a hill. Anyway, I've learnt how to handle strings, bytes, longs in spin quite well - as of this afternoon. So, now full speed towards the next brick wall ; )

    Erlend
Sign In or Register to comment.