CString Null characters ?

  1. 9 months ago
    Edited 9 months ago

    Hello .
    I have following code that works perfect for my project.
    The string s2 contains hexadecimal values that need to be splitted up in bytes (Uint8) into an array lVal()
    X is the CString containing the created array.

    Dim i As Integer
    Dim k As Integer
    Dim s2 As String
    Dim s3 As String
    Dim n As Uint8
    Dim lVal(5) As Uint8
    Dim X As CString
    s2 = "F0 43 73 26 00 F7"
         ' Remove all spaces in string
    s3 = RepalceAll(s2, " ", "")
    s2 = s3
         ' convert string to Uint8 (bytes)
    for k = 1 to len(s2) step 2
    s3 = midB(s2, k, 2)
    n = val("&h" + s3)
    lVal(i) = n
    if n = 247 Then Exit    ' 247 = &HF7
    i = i + 1
    X = ChrB(lVall(0)) + ChrB(lVal(1)) + ChrB(lVal(2)) + ChrB(lVal(3)) + ChrB(lVal(4)) + ChrB(lVal(5))
         ' send X to a dll that accepts CString 
    rtn = HpInsertSysex(Hpid, lTime, X)

    As mentioned this works perfect, even with the 00 included into the string s2.

    In my project the length of s2 can be up to 383 characters (127 bytes + spaces).
    So it would be a long X = ChrB(lVal(0)) + ChrB(lVal(1)) + .......... + ChrB(lVal127)).

    So I tried the following in my code
    Replace X = ChrB(lVall(0)) + ChrB(lVal(1)) + ChrB(lVal(2)) + ChrB(lVal(3)) + ChrB(lVal(4)) + ChrB(lVal(5)) by

    for i = 0 to 5
    X = X + ChrB(lVal(i))

    This does work if there are no '00' in the string s2.
    If a '00' exists in the string the CString does not includes this '00'.

    I know a CString is Null terminated and this seems normal because ChrB(&H00) = a Null,
    but why does it works even with 00 if i define X as ChrB() + Chrb() + .... and not if I create it with a for next code ?

    Can anyone explain why and is there a solution ?



  2. What does your lVal method do?

  3. lVal() is an Array
    He is declared in the 5th line of my code

    Dim lVal() As Uint8

  4. Tim H

    9 Aug 2018 Pre-Release Testers Portland, OR USA

    Don't define X as CString. Dim X as String. Then it will work. The reason your first set of code works is because Xojo builds the right hand side in a temporary String variable and then assigns it to X. Your second set of code uses X as a CString, so it discards the 00 value, as it should.

    CString is not suitable for use in Xojo code directly. It exists for use in declares.

or Sign Up to reply!