ArmBasic VAL function

Questions about the BASICtools and MakeItC
Post Reply
olzeke51
Posts: 414
Joined: Sat May 17, 2014 4:22 pm
Location: South Carolina

ArmBasic VAL function

Post by olzeke51 »

Is the VAL keyword setup so as to be able to "translate" a string created by an index into a string??
ie p= val(rtc$(5))
I'm getting this error:
o=val(rtc$(4))
-ERROR C:/Coridium/Basic/Examples/Bchip_event_logger06.bas: 37: Expected ), found (4))



basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: ArmBasic VAL function

Post by basicchip »

rtc$(5) is simply a number it is not a string.

So

p = rtc$(5)

is all you need

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: ArmBasic VAL function

Post by basicchip »

PS the val(string) function is looking for a string, that is why it expected )

olzeke51
Posts: 414
Joined: Sat May 17, 2014 4:22 pm
Location: South Carolina

Re: ArmBasic VAL function

Post by olzeke51 »

I DIMmed rtc$ as a string, and then poke values into it to represent, the hours/mins/secs etc
so that I could print the whole thing at one time.
so rtc$(5) is the 6th byte in the string which is a 'short' string - very short - 1 byte.
'
I was having trouble printing a 'byte array' and thought a string array would be another way to do it.
since there is only 1 string buffer that may cause a problem as VAL is looking for a string and then
I am making a string with rtc$(5) -- so in one sense we have two strings to work with.
I thought there was a caution about recursive strings because of the 1 string buffer.
'
this is all because my DS1307 RTC does packed BCD for the different time elements.
gz/olzeke51

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: ArmBasic VAL function

Post by basicchip »

rtc$(5) is not a string, but a byte. This is true in all BASICs I know, also in all languages I know of.

So you can read the ASCII code corresponding to the byte in a string by string(element). That means an 'A' is &H41 or 65.

You can set a single byte in a string by

rtc$(5)=&H31

Which sets it to '1'

Now there are not many standards for strings in BASIC, but I use the same construction as C, which is an array of characters starting with the offset str(0), and continues until the first 0 is found.

The builtin function val(string) expects a string as an argument. Again rtc$(5) is not a string, but a single character.

Strings are not part of many earlier BASICs, but they are useful for many embedded applications, but strings can chew up a lot of memory, that is why there is only 1 string accumulator for doing string operations, and the compiler flags string operations that it can not handle.

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: ArmBasic VAL function

Post by basicchip »

To convert decimal digit to byte in a string-

rtc$(5) = &H30 + digit

ARMbasic treats single byte strings as a special case, so the following also works, and is more readable-

rtc$(5) = "0" + digit

Likewise to convert a byte in a string to a decimal digit

digit = rtc$(5) - "0"

--- another style note, while rtc$ is still legal as a string declaration, it is an older style, and the $ is no longer needed

olzeke51
Posts: 414
Joined: Sat May 17, 2014 4:22 pm
Location: South Carolina

Re: ArmBasic VAL function

Post by olzeke51 »

thanks for the ideas - I'll play around with them
'
I have two arrays -named "rtc"- one is byte and one is $$$string,
that way I can keep them in sync with each other - per the type of data I need.
but my ideas are not panning out quite like I initially thought.
'
my 'old-style' is showing through , huh!? - olzeke51

olzeke51
Posts: 414
Joined: Sat May 17, 2014 4:22 pm
Location: South Carolina

Re: ArmBasic VAL function

Post by olzeke51 »

it isn't a VAL issue, but you made this statement -- and it might apply to my error:
ARMbasic treats single byte strings as a special case, so the following also works, and is more readable-

rtc$(5) = "0" + digit

'
I was using LEFT and RIGHT(str) to assign values"
***********program - condensed version of my original program
dim rtc$(10) 'as string ******even putting in the "as string" doesn't change the error
dim sec$(1) 'as string
sec$ = "12"
rtc$(0) = sec$(0)
rtc$(1) = sec$(1)
rtc$(6) = left(sec$,1)
rtc$(7) = right(sec$,1)
print sec$
print rtc$
********************* but I get errors for both rtc$(6) and (7)
is it because the indexed string value is treated as a single byte string ?? rtc$(6)
*********
Programming Flash 1114...

rtc$(6) = left(sec$,1)
-ERROR C:/Coridium/Basic/Examples/stringer.bas: 6: expected comparison operator, found end of line


rtc$(6) = left(sec$,1)
-ERROR C:/Coridium/Basic/Examples/stringer.bas: 6: Expected Operand, found )

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: ArmBasic VAL function

Post by basicchip »

Left and right are built in string functions-

Code: Select all

FUNCTION left (str, count) as STRING
 ...
 return str     ' in this case some part of string
END FUNCTION

'So you are trying to

str(element) = some_string   ' this is an illegal type conversion
some_string in this case is the string value returned by LEFT

While you know that the string returned by LEFT in this case is 1 character long the compiler doesn't normally know that.

If you are trying to pull a single character out of a string you should use str(index), not left or right

LEFT and RIGHT are used to build a string from portions of another string.

As for single character strings, they are a special case and context sensitive. When dealing with integers, elements of an integer, byte or string array, that single character string is treated as the ASCII value.

x = "A" ' assign &H41 to x

When dealing with strings, that single character string in treated like a string--

str = "A" ' build the string with 2 elements, &H41 for the A followed by 0 terminating the string.

olzeke51
Posts: 414
Joined: Sat May 17, 2014 4:22 pm
Location: South Carolina

Re: ArmBasic VAL function

Post by olzeke51 »

thanks for confirming my suspicions,
I had already used string(index) as the left/right was failing.

Post Reply