Page 1 of 1

String to num 1st Zero lost

Posted: Fri Oct 28, 2022 5:33 am
by viktor_au
Hello
I found some answer for question below
----------------------------------------------------------:
SteveM ยป Tue Jun 10, 2014
Loosing 1st zero during converstion of num to string
There's no way to do that directly, because the values are stored within the chip as fixed length binary numbers. e.g. if you store your number as a ULONG (4 bytes), it will always be 32bits long inside the chip. When you convert the String to an Integer, any leading zero's in the string will just be ignored, and the chip pads the number with enough 'binary zeros' to fill the bytes.
---------------------
Steve suggested to get the length of the string, however this is OK during the (back) num-to-string conversion.
-------------------
Question
How can I save to integer string "077" without loosing 1st Zero?
Thanks

Re: String to num 1st Zero lost

Posted: Fri Oct 28, 2022 8:16 am
by chipfryer27
Hi

Just heading out, sorry to be brief.

What about using Right$ in a calculation?

e.g.
x$ = 123045
y$=Right$(x$,3)

y$ would equal 045

Just a thought and might not be applicable in your scenario.

Regards

Re: String to num 1st Zero lost

Posted: Fri Oct 28, 2022 8:46 am
by medelec35
Hello.
Leading zeros can't be used for non-strings as they will be treated as octal i.e. to the base 8 rather than base 10
If you did enter a leading zero in an expression eg MyInt = 0123 then the actual value of MyInt will be decimal 83, which will not be what you are expecting.
The only way of displaying a leading zero is in a string format.

Re: String to num 1st Zero lost

Posted: Fri Oct 28, 2022 10:27 pm
by viktor_au
What about using Right$ in a calculation?
-------------------------------------------------------
Sure... but I wanted the integer as 077.

The only way of displaying a leading zero is in a string format
Looks like I have to flag the converted integer 77 (from "077" string) as negative.

Thanks