Shop OBEX P1 Docs P2 Docs Learn Events
Char to Str conversion? — Parallax Forums

Char to Str conversion?

worthyamherstworthyamherst Posts: 46
edited 2011-08-02 09:10 in Propeller 1
Is there a way to conver character data into string data?
We're working on uploading data to a website using the spinneret, but the issue is that the propeller is receiving the data is char form and then sending it to the website in str form. Is there a way to create a subroutine to convert the char to str?

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2011-08-02 08:08
    it is not clear to me which hardware does what

    The propeller receives single bytes (chars) now who is sending it as string to what kind of device?

    What is teh code-base of the chars? Are the chars ASCII-coded? or do you receive byte-values where a 1 means a 1 and a 128 means a 128 as a number?

    For the case that you receive ASCII-coded chars simply receive them in an array of bytes. If the receiving has finished add a zero at the end of the character-seqeunce and then
    use a send-str-method like in FullDuplexSerial to send the whole string at once.

    If the case is something else you have to provide more specific information of what is going on.
    Please add a verbal description and some examples of what you receive and what should be sended.

    keep the questions coming
    best regards

    Stefan
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2011-08-02 08:09
    A string is nothing more than an array of characters or even only one or none. In Spin and many languages the string is terminated by a null (00) character so all you have to do is pass the address of the character rather than the character and as long as the character is stored in a word or long (high bits are all zeroed) then it will look like a string.

    For example:
    tx.ch(char) ' transmits a single character
    tx.string(@char) ' transmits a single character

    But like Stephan I too wonder if your question is correct.
  • jazzedjazzed Posts: 11,803
    edited 2011-08-02 09:10
    Is there a way to create a subroutine to convert the char to str?

    If your string is null terminated like in C and Spin, you'll have to use a temporary buffer with at least 2 chars per string.
    ' define a buffer with enough space
    ' use buffer[index] like in C only with VAR variables ...
    ' DAT variables need different syntax
    var byte buffer[10]
    
    ' stuff character ch into a null terminated string
    pub char2str(ch)
      buffer[0] := ch
      buffer[1] := 0
      return @buffer ' return address of buffer
    
Sign In or Register to comment.