Re: convert numbers

new topic     » goto parent     » topic index » view thread      » older message » newer message

--=====_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 thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu