Re: convert numbers
- Posted by Louis at cwshop.com Oct 12, 2003
- 514 views
--=====_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. 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}</DIV> <DIV> </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 my library function. For example:</DIV> <DIV> </DIV> <DIV>integer result</DIV> <DIV>result = s2n("123") * 2 -- Returns the number 246.</DIV> <DIV> </DIV> <DIV>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.</DIV> <DIV> </DIV> <DIV>Louis</DIV></DIV> <DIV> </DIV> <DIV>-----------------------------------------------------------------<BR>-- s2n -- Convert a numeric text string to an integer<BR>-- Note: This only works for positive integers<BR>-----------------------------------------------------------------<BR>global function s2n(sequence s)<BR> atom n<BR> s -= '0' -- Convert ASCII to BCD<BR> n = s[1] -- First digit<BR> for i = 2 to length(s) do<BR> n = n*10 + s[i] -- The rest of the digits<BR> end for<BR> return n -- 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> </DIV> <DIV><FONT face=Arial size=2>I don't understand</FONT></DIV> <DIV><FONT face=Arial size=2></FONT> </DIV> <DIV><FONT face=Arial size=2>jvandal</FONT><FONT size=2 --=====_106593347013311=_--