Serial port questions: queues

Started by arhodes19044 August 7, 2005
Actually I mis-typed. I meant that I did not improve on a general
purpose CStringInt function. My very limited custom version was a
great improvement, but not the more general.

Here is the code:
(CStringInt is the limited version, CStringInt5 is the fuller

public sub cstringint (byRef TBuffer as string, ByVal Value as
'converts int to XXX, dropping only the first leading zero
'assumes the value is no more than 999

dim char as integer

TBuffer = "" char = value \ 100
if (char >0) then
TBuffer = Tbuffer & chr(cbyte(char)+48)
value = value - char *100
end if char = value \ 10
TBuffer = Tbuffer & chr(cbyte(char)+48)
value = value - char *10

TBuffer = Tbuffer & chr(cbyte(value)+48)

End Sub '____________________________________________________________________
public sub cstringint5 (byRef TBuffer as string, ByVal Value as
Integer) 'converts an integer to a string no leading

dim index as byte
dim char as integer
dim pow10(1 to 4) as integer
dim not_leading as boolean

if value = 0 then
Tbuffer = "0"
exit sub
end if

if value < 0 then
TBuffer = "-"
value = (-value)
TBuffer = ""
end if pow10(1) = 10
pow10(2) = 100
pow10(3) = 1000
pow10(4) = 10000
not_leading = false

for index = 4 to 1 step -1
char = value \ pow10(index)

if (char = 0) then
if (not_leading) then
Tbuffer = Tbuffer & "0"
end if
TBuffer = TBuffer & chr(cbyte(char)+48)
not_leading = true
value = value - char *pow10(index)
end if

next 'index

tbuffer = Tbuffer & chr(cbyte(value+48))

End Sub

Additionally, I do not love either of these functions because they
require divisions, which as you know are really slow. I also wrote
this CStringFloat function that is also very limited but it fits my
specific need. It too is a big improvement on the General Purpose
built-in version. However, it does not replace all the functions of
the GP.

public sub cstringfloat (byRef TBuffer as string, ByVal Value as
single) 'converts XX.XXXXXX to string "YY.YYYY"
'assumes that the value is no larger than 99.9999

dim index as byte
dim char as byte
dim last_value as single

last_value = value/10.0
'the only division needed, thankfully!
TBuffer = ""

for index = 1 to 6
char = FixB(last_value) 'Fix is pretty efficient
TBuffer = Tbuffer & chr(char+48)
last_value = value - Csng(char)*10.0
value = 10.0 * last_value
if index = 2 then
TBuffer = TBuffer & "."
end if

End Sub
'_____________________________________________________________ So these are my limited replacements. Whaddya think? I did not
post them before because I am sure they could be done better somehow.


--- In basicx@basi..., Mike Perks <basicx@a...> wrote:
> arhodes19044 wrote:
> > Oddly, I was not able to improve meaningfully on CStr(). I did
> > slightly less stack by a couple of bytes, but my code was longer
> > eventhough my function was very limited and customized and had no
> > error checking. So, I went back to the original function.
> I am confused. CStr() is a polymorphic function that takes a
variety of
> numerical formats and converts them to a string. Under the covers
> are only actually four functions that do the work, CStringBoolean
> booleans, CStringFloat for single, CStringUnsignedLong for
> and CStringLong for everything else. All the byte and integer
types are
> converted to a long before CStringLong is called.
> >
> > I did make significant improvements on the built-in functions of
> > cstringfloat() and cstringlong() by extensively limiting what it
> > does and no error checking. Very customized. Actually my
> > cstringlong() is really cstringint() because I will not be using
> > long integers.
> >
> Perhaps you can share your results for other people to use.
> Mike