Shop OBEX P1 Docs P2 Docs Learn Events
Format question — Parallax Forums

Format question

Don MDon M Posts: 1,652
edited 2014-02-01 15:20 in Propeller 1
I have a buffer: buffer[17]

In that buffer is the following characters: "-> 646E6C00BC3D"

I want to eliminate the "-> " and print the characters from the buffer to look like this by inserting a ":" every 2 characters: 64:6E:6C:00:BC:3D

So here is my code (that doesn't work right):
repeat i from 3 to 16
    term.tx(byte[@buffer][i])
    term.tx(":") 

How do I get it to print the ":" every 2 characters instead of every character?

Thanks.
Don

Comments

  • Don MDon M Posts: 1,652
    edited 2014-02-01 15:05
    I made this work but is there a simpler way?
    repeat i from 3 to 16
        term.tx(byte[@buffer][i])
        case i
          5, 7, 9, 11, 13:
            term.tx(":")
    
  • Mike GMike G Posts: 2,702
    edited 2014-02-01 15:05
    Insert a modulus statement inside the repeat to check for an even or odd count.
    IF i // 2 == 1
      term.tx(":")
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-02-01 15:07
    Don M wrote: »
    I have a buffer: buffer[17]

    In that buffer is the following characters: "-> 646E6C00BC3D"

    I want to eliminate the "-> " and print the characters from the buffer to look like this by inserting a ":" every 2 characters: 64:6E:6C:00:BC:3D

    So here is my code (that doesn't work right):
    repeat i from 3 to 16
        term.tx(byte[@buffer][i])
        term.tx(":") 
    

    How do I get it to print the ":" every 2 characters instead of every character?

    Thanks.
    Don
    You want to print two characters between colons not one.

    There are several ways to do this, here's one.
    i := 3
    repeat
        term.tx(byte[@buffer][i++])
        term.tx(byte[@buffer][i++])
        if i < 15
          term.tx(":") 
    while i < 16
    

    Edit: I see Mike G has another way.
    Edit again: I hadn't seen post #2. There are sure a lot of ways to Spin a cat.
  • Don MDon M Posts: 1,652
    edited 2014-02-01 15:11
    Thanks Mike & Duane!
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-02-01 15:20
    This is similar to what you're doing:
    repeat ch from 3 to 14
        if (lookdown(ch: 5, 7, 9, 11, 13))
          term.tx(":")
        term.tx(byte[@buffer][ch])
    


    This is longer, but might be easier to adapt as you can set the offset once, and number of byte pairs minus one at the top of the code.
    p_buf := @buffer + 3
    
      repeat 5
        repeat 2
          term.tx(byte[p_buf++])
        term.tx(":")
      repeat 2
        term.tx(byte[p_buf++])
    
Sign In or Register to comment.