Should this work

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Should this work

Post by FFMan »

I came back to my esp8266 project and got it working but i battled for a while with aliased variables in decoding the return.

I tried

dim strNTPTime as string * 4
dim ulNPTTime as unsignedlong alias strNPTTime

but i don't think this worked as expected. Regardless of what i put in the string i got the same number.

I changed it to

dim ulNPTime as unsignedlong
dim byTime(0 to 3) as byte alias ulNPTTime

and inserted the string return byte by byte into byTime and it works.

should the first example have worked ?
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Should this work

Post by dkinzer »

FFMan wrote:should the first example have worked ?
No, because of the way that fixed-length strings are stored in memory. In addition to the bytes for the characters of the string there are two additional bytes, one giving the string length and the other identifying the string type. In contrast, the Byte array that you got to work utilizes exactly the number of bytes specified. See the manual page below for string implementation details.

http://www.zbasic.net/doc/ZBasicRef.php?page=105

You can observe what I've described using this simple test program that does work because it takes the string storage overhead into account.

Code: Select all

Const strLen as Integer = 4
Dim strNTPTime as String * strLen
Dim ulNPTTime as UnsignedLong Based StrAddress(strNTPTime)
Dim b(1 to strLen+2) as Byte Based strNTPTime.DataAddress

Sub Main()
    Dim i as Integer
    strNTPTime = "abcd"
    Call dspTime(ulNPTTime)
    Debug.Print
    Debug.Print "Bytes of the string storage:"
    Debug.Print "[";
    For i = LBound(b) to UBound(b)
        Debug.Print CStrHex(b(i));
    Next i
    Debug.Print "]"
End Sub

Sub dspTime(ByVal uval as UnsignedLong)
    Dim s as String
    Debug.Print "Characters of the string as an UnsignedLong value:"
    s = CStrHex(uval)
    Debug.Print "["; s; "]"
End Sub
- Don Kinzer
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post by FFMan »

i see thanks Don.

I got there in the end
Post Reply