1. convert numbers

This is a multi-part message in MIME format.

------=_NextPart_000_0014_01C39018.4D8F5600
	charset="iso-8859-1"

I have a field that is displayed in trace as:
hk =3D (49'1',50'2',51 '3')
I have tried to use the "value" to get the number "123"

I don't understand

jvandal
------=_NextPart_000_0014_01C39018.4D8F5600
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1264" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>I have a field that is displayed in =
trace as:<BR>hk=20
=3D (49'1',50'2',51 '3')</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I have tried to use the "value" to get =
the number=20
"123"</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I don't understand</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>

------=_NextPart_000_0014_01C39018.4D8F5600--

new topic     » topic index » view message » categorize

2. Re: convert numbers

On Sat, 11 Oct 2003 16:54:15 -0600, sixs at ida.net wrote:

>
>I have a field that is displayed in trace as:
>hk =3D (49'1',50'2',51 '3')
>I have tried to use the "value" to get the number "123"
>
>I don't understand
>
Does running this help any?

sequence hk
hk=3D"123"
include get.e
include misc.e
sequence res
while 1 do
	res=3Dvalue(hk)
	if res[1]=3DGET_SUCCESS then
		if integer(res[2]) then
			printf(1,"Integer %d\n",{res[2]})
		elsif atom(res[2]) then
			printf(1,"Atom %g\n",{res[2]})
		elsif sequence(res[2]) then
printf(1,"Sequence \"%s\", aka %s\n",{res[2],sprint(res[2])})
		else
			puts(1,"UNKNOWN TYPE!!\n")
		end if
	else
		puts(1,"invalid somehow\n")
	end if
	puts(1,"Enter another value:")
	hk=3Dgets(0)
	if length(hk)<2 then abort(0) end if
	puts(1,"\n")
end while
abort(0)

Regards,
Pete

new topic     » goto parent     » topic index » view message » categorize

3. Re: convert numbers

----- Original Message ----- 
>From: sixs at ida.net 
>Subject: convert numbers
>
>
>
>I have a field that is displayed in trace as:
>hk = (49'1',50'2',51 '3')
>I have tried to use the "value" to get the number "123"
>
>I don't understand

 include get.e
 sequence hk
 atom     hkval
 sequence val

 hk = "123"

 val = value(hk)
 if val[1] = GET_SUCCESS then
     hkval = val[2]
 else
     ' Not a valid number
 end if

new topic     » goto parent     » topic index » view message » categorize

4. Re: convert numbers

sixs wrote:

> I have a field that is displayed in trace as:
> hk = (49'1',50'2',51 '3')
> I have tried to use the "value" to get the number "123"
>
> I don't understand

-------------------[ begin sample code ]-------------------
include get.e

sequence hk
object v

hk = {49,50,51}
-- which is the same as
hk = {'1','2','3'}
-- which is the same as
hk = "123"

v = value(hk)
if v[1] = GET_SUCCESS then
   v = v[2]
   ? v               -- v now holds the number 123
else
   puts(1, "error")
end if
if getc(0) then end if
--------------------[ end sample code ]--------------------

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

5. Re: convert numbers

--=====_106593347013311=_

The trick to understanding this is knowing that Euphoria stores text=
 strings as a series of integers with each character using the low-order 8=
 bits of each integer.  It's true that this wastes space, but it has its=
 advantages, as we shall see.  Therefore, "ABC" is exactly the same as {65,=
 66, 67}.  That's why the trace output displays it both ways.  The debugger=
 doesn't know whether you understand the value as numbers or characters.=
  ASC() and CHR() functions are used in other languages to translate back=
 and forth, but in Euphoria we don't need to convert.  They are exactly the=
 same.  {79, 75, 63}

Do you have a sequence "123" and you want to be able to convert it to a=
 number 123?
See below for my library function.  For example:

integer result
result =3D s2n("123") * 2   -- Returns the number 246.

The value() function does not simply return a number.  It returns two=
 things:  a success/failure message and the number.  Not as easy to use in=
 a calculation as above.  Although value() is more rigorous than s2n(), I=
 suspect s2n() is much faster.

Louis

-----------------------------------------------------------------
-- s2n -- Convert a numeric text string to an integer
--   Note: This only works for positive integers
-----------------------------------------------------------------
global function s2n(sequence s)
    atom n
    s -=3D '0' -- Convert ASCII to BCD
    n =3D s[1] -- First digit
    for i =3D 2 to length(s) do
        n =3D n*10 + s[i] -- The rest of the digits
    end for
    return n -- Return the number
end function

*********** REPLY SEPARATOR ***********

On 10/11/2003 at 4:54 PM sixs at ida.net wrote:

I have a field that is displayed in trace as:
hk =3D (49'1',50'2',51 '3')
I have tried to use the "value" to get the number "123"

I don't understand

jvandal


--=====_106593347013311=_
Content-Type: text/html; charset="us-ascii"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1226" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>The trick to understanding this is knowing that Euphoria stores text 
strings as a series of integers with each character using the low-order 8 bits 
of each integer.&nbsp; It's true that this wastes&nbsp;space, but it has its 
advantages, as we shall see.&nbsp; Therefore, "ABC" is exactly the same as {65, 
66, 67}.&nbsp; That's why the trace output displays it both ways.&nbsp; The 
debugger doesn't know whether you&nbsp;understand the value as&nbsp;numbers or 
characters.&nbsp; ASC() and CHR() functions are used in other languages to 
translate back and forth, but in Euphoria we don't need to convert.&nbsp; They 
are exactly the same.&nbsp; {79, 75, 63}</DIV>
<DIV>&nbsp;</DIV>
<DIV>
<DIV>Do you have a sequence "123" and you want to be able to convert it to a 
number 123?</DIV>
<DIV>See below for&nbsp;my library function.&nbsp; For example:</DIV>
<DIV>&nbsp;</DIV>
<DIV>integer result</DIV>
<DIV>result = s2n("123") * 2&nbsp;&nbsp; -- Returns the number 246.</DIV>
<DIV>&nbsp;</DIV>
<DIV>The value() function does not simply return a number.&nbsp; It returns two 
things:&nbsp; a success/failure message and the number.&nbsp; Not as easy to use
in a calculation as above.&nbsp; Although value() is more rigorous than s2n(), I
suspect s2n() is much faster.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Louis</DIV></DIV>
<DIV>&nbsp;</DIV>
<DIV>-----------------------------------------------------------------<BR>-- s2n
-- Convert a numeric text string to an integer<BR>--&nbsp;&nbsp;&nbsp;Note: This
only works for positive 
integers<BR>-----------------------------------------------------------------<BR>global
function s2n(sequence s)<BR>&nbsp;&nbsp;&nbsp; atom n<BR>&nbsp;&nbsp;&nbsp; s -=
'0'&nbsp;-- Convert ASCII to BCD<BR>&nbsp;&nbsp;&nbsp; n = s[1]&nbsp;-- First 
digit<BR>&nbsp;&nbsp;&nbsp; for i = 2 to length(s) 
do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n = n*10 + s[i]&nbsp;-- The 
rest of the digits<BR>&nbsp;&nbsp;&nbsp; end for<BR>&nbsp;&nbsp;&nbsp; return 
n&nbsp;-- Return the number<BR>end function<BR><BR><FONT face=Arial 
size=2>*********** REPLY SEPARATOR ***********<BR><BR>On 10/11/2003 at 4:54 PM 
sixs at ida.net wrote:</FONT></DIV>
<BLOCKQUOTE 
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px
solid"><PRE>============ The Euphoria Mailing List ============
</PRE>
  <DIV><FONT face=Arial size=2>I have a field that is displayed in trace 
  as:<BR>hk = (49'1',50'2',51 '3')</FONT></DIV>
<DIV><FONT face=Arial size=2>I have tried to use the "value" to get the number
  "123"</FONT></DIV>
  <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
  <DIV><FONT face=Arial size=2>I don't understand</FONT></DIV>
  <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
  <DIV><FONT face=Arial size=2>jvandal</FONT><FONT size=2 


--=====_106593347013311=_--

new topic     » goto parent     » topic index » view message » categorize

6. Re: convert numbers

On Sunday 12 October 2003 04:37 am, Louis wrote:

> The value() function does not simply return a number.  It returns two
> things:  a success/failure message and the number.  Not as easy to use in a
> calculation as above.  Although value() is more rigorous than s2n(), I
> suspect s2n() is much faster.

Indeed, your function is 5 to 8 x faster than value().
Which makes me think that I should take a look at some of my 
programs which use value() heavily - maybe I could speed them up 
significantly by writing my own conversion routines.

Irv

> -----------------------------------------------------------------
> -- s2n -- Convert a numeric text string to an integer
> --   Note: This only works for positive integers
> -----------------------------------------------------------------
> global function s2n(sequence s)
>     atom n
>     s -= '0' -- Convert ASCII to BCD
>     n = s[1] -- First digit
>     for i = 2 to length(s) do
>         n = n*10 + s[i] -- The rest of the digits
>     end for
>     return n -- Return the number
> end function

new topic     » goto parent     » topic index » view message » categorize

7. Re: convert numbers

----- Original Message ----- 
From: <irvm at ellijay.com>
To: "EUforum" <EUforum at topica.com>
Subject: Re: convert numbers


> 
> 
> On Sunday 12 October 2003 04:37 am, Louis wrote:
> 
> > The value() function does not simply return a number.  It returns two
> > things:  a success/failure message and the number.  Not as easy to use in a
> > calculation as above.  Although value() is more rigorous than s2n(), I
> > suspect s2n() is much faster.
> 
> Indeed, your function is 5 to 8 x faster than value().
> Which makes me think that I should take a look at some of my 
> programs which use value() heavily - maybe I could speed them up 
> significantly by writing my own conversion routines.

But be careful as that routine allows any string to be 'converted', even those
that are not 'numeric'.

Here is the Text-To-Number routine used by win32lib's getNumber() routine.

--/func W32_TextToNumber( sequence text )
--/ret Atom or Sequence: The number represented by the text, or
{Number,ErrorPosition}
--/desc This converts the text into a number.
-- If the text contains invalid characters, zero is returned. The text can
-- have leading and trailing whitespace characters.
--
-- /b "Note 1:" You can supply Hexadecimal values if the value is preceded by
-- a '#' character, Octal values if the value is preceded by a '@' character,
-- and Binary values if the value is preceded by a '!' character. With
-- hexadecimal values, the case of the digits 'A' - 'F' is not important. Also,
-- any period character embedded in the number is used with the correct base.
--
-- /b "Note 2:" Any underscore or comma characters, that are embedded in the
text
-- number are ignored. These can be used to help visual clarity for long
numbers.
--
--/b "Note 3:" You can supply a leading or trailing, minus or plus sign.
--
--/b "Note 4:" You can supply trailing percentage sign(s). Each one present
causes
-- the resulting value to be divided by 100.
--
-- This function can optionally return information about invalid numbers. If
-- /i text has the form of {sequence, integer} then if the integer is nonzero,
-- a sequence is returned. The first element is the value converted, and the
-- second is the position in the text where conversion stopped. If no errors
-- were found then this is zero.
--
--/code
--     sequence rc
--     atom   val
--     rc = W32_TextToNumber({"12.34a", 1})
--     --  rc ---> {12.34, 6} -- Error at position 6
--     rc = W32_TextToNumber({"12.34", 1})
--     --  rc ---> {12.34, 0} -- No errors.
--
--     val = W32_TextToNumber("12.34a")
--     --  val ---> 0
--
--      val = W32_TextToNumber("#f80c") --> 63500
--      val = W32_TextToNumber("#f80c.7aa") --> 63500.47900390625
--      val = W32_TextToNumber("@1703") --> 963
--      val = W32_TextToNumber("!101101") --> 45                            
--      val = W32_TextToNumber("12_583_891") --> 12583891
--      val = W32_TextToNumber("12_583_891%") --> 125838.91
--      val = W32_TextToNumber("12,583,891%%") --> 1258.3891
--
--/endcode                 
constant vDigits = ".0123456789ABCDEF"
global function W32_TextToNumber( sequence text)
    -- get the numeric value of text
    integer dot,sign,tstart,tend, v, note, notify
    atom lhs, rhs, lh, rh 
    integer base, pc
    atom value

    dot = 0
    lh = 0
    lhs = 0
    rh = 0
    rhs = 1
    sign = 0
    note = 0
    base = 10       
    pc = 1

    if  length(text) = 2  and
        sequence(text[1]) and
        integer(text[2])
    then
        notify  = text[2]
        text = text[1]
    else
        notify = 0
    end if

    -- convert the value of the text    
    text = upper(text)
    tstart = 1
    tend = length(text)
    -- Ignore leading whitespace
    while tstart <= tend do
        if equal(text[tstart], '-') and sign = 0 then
            sign = -1
        elsif equal (text[tstart],'+') and sign = 0 then
            sign = 1
        elsif equal (text[tstart],'#') then
            base = 16
        elsif equal (text[tstart],'@') then
            base = 8
        elsif equal (text[tstart],'!') then
            base = 2
        elsif find(text[tstart], {'\t', ' '}) = 0 then
            exit
        end if
        tstart += 1
    end while   
    
    -- Ignore trailing whitespace
    while tstart <= tend do
        if equal(text[tend], '-') and sign = 0 then
            sign = -1
        elsif equal(text[tend],'+') and sign = 0 then
            sign = 1
        elsif equal(text[tend],'%') then
            pc *= 100
        elsif find(text[tend], {'\t', ' '}) = 0 then
            exit
        end if
        tend -= 1
    end while
    
    -- Set the default sign.
    if sign = 0 then
        sign = 1
    end if
    
    for i = tstart to tend do
        if lhs > 0 and find(text[i],"_,") > 0 then
            -- ignore an embedded grouping characters.
        else
            v =  find(text[i], vDigits)
            -- Invalid char so force a zero return.
            if v = 1 then -- A dot found.
                if dot = 0 then
                    dot = 1
                else
                    note = i
                    if notify = 0 then
                        sign = 0
                    end if
                    exit
                end if
            else       
                v -= 1
                if v < 0 or v > base then
                    -- Illegal char found.
                    note = i
                    if notify = 0 then
                        sign = 0
                    end if
                    exit
                else
                    if dot = 1 then
                        rhs *= base
                        rh = (rh * base) + v - 1
                    else
                        lhs += 1
                        lh = (lh * base) + v - 1
                    end if
                end if
             end if
        end if
        
        -- I got to the end without error!
        if i = tend then
            note = 0
        end if
         
    end for
    
    if rh = 0 and pc = 1 then
        -- Common situation optimised for speed.
        value = lh * sign
    else                
        value = ((lh + (rh / rhs)) * sign) / pc
    end if
    
    if notify = 0 then
        return value
    else
        return {value, note}
    end if
end function

-- 
Derek

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu