* How to convert a string containing two hex digits to a character? @ 2010-01-06 0:39 Leslie 2010-01-06 0:43 ` Leslie ` (2 more replies) 0 siblings, 3 replies; 22+ messages in thread From: Leslie @ 2010-01-06 0:39 UTC (permalink / raw) Hi, it's me again. :-) I need to be able to convert a string variable containing two hexadecimal digits (e.g. "7C") to its equivalent character value. I see from ARM 3.5.5 "Operations of Discrete Types" that I can use function S'Val(Arg : universal_integer) return S'Base where universal_integer would be the numeric value of the string, but how do I get from "7C" to 16#7C# when 7C is a string value, not a constant? I.e., Hex_Pair : unbounded_string; begin Hex_Pair := slice(some_string,1,2); Char_Code : character; -- now what? end; Leslie ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 0:39 How to convert a string containing two hex digits to a character? Leslie @ 2010-01-06 0:43 ` Leslie 2010-01-06 1:22 ` Hibou57 (Yannick Duchêne) 2010-01-06 5:17 ` tmoran 2010-01-07 14:26 ` John B. Matthews 2 siblings, 1 reply; 22+ messages in thread From: Leslie @ 2010-01-06 0:43 UTC (permalink / raw) Leslie wrote: > Hi, it's me again. :-) > > I need to be able to convert a string variable > containing two > hexadecimal digits (e.g. "7C") to its equivalent character > value. I see from ARM 3.5.5 "Operations of Discrete Types" > that I can use > > function S'Val(Arg : universal_integer) return S'Base > > where universal_integer would be the numeric value of the > string, but how do I get from "7C" to 16#7C# when 7C is a > string value, > not a constant? I.e., > OOPS! Should be > Hex_Pair : unbounded_string; > Char_Code : character; -- moved this out of the block. :-) > > begin > Hex_Pair := slice(some_string,1,2); > -- now what? > end; > > Leslie ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 0:43 ` Leslie @ 2010-01-06 1:22 ` Hibou57 (Yannick Duchêne) 2010-01-06 1:31 ` Hibou57 (Yannick Duchêne) ` (2 more replies) 0 siblings, 3 replies; 22+ messages in thread From: Hibou57 (Yannick Duchêne) @ 2010-01-06 1:22 UTC (permalink / raw) You may first convert from two 4 bits digits, that is, convert "7" and "C". 16#7#; -- 7 16#C#; -- 12 To get 16#7C# from 16#7# and 16#C#, just do ((16#7# * 16) + 16#C#) To get 16#7# and 16#C#, first get '7' and 'C' from the string, you may do something like the following (providing your string is named S, and it is an ASCII string) : High_Order_Digit_Position : constant Positive := S'First; Low_Order_Digit_Position : constant Positive := Positive'Succ (High_Order_Digit_Position); High_Order_Digit_Representation : constant Character := S (High_Order_Digit_Position); Low_Order_Digit_Representation : constant Character := S (Low_Order_Digit_Position); To get a digit Natural value from its Character representation, you may do something like the following (providing your character is stored in a Character named C) : case C is when '0' .. '9' => Digit_Value := 0 + (Character'Pos (C) - Character'Pos ('0')); when 'A' .. 'F' => Digit_Value := 10 + (Character'Pos (C) - Character'Pos ('A')); when 'a' .. 'f' => Digit_Value := 10 + (Character'Pos (C) - Character'Pos ('a')); when others => raise Program_Error; end case; Wrap the latter in a function like “ function Hexadecimal_Digit_Value (C : Character) return Natural; ” Then do : Hexadecimal_Base : constant Natural := 16; High_Order_Digit : constant Natural := Hexadecimal_Digit_Value (High_Order_Digit_Representation); Low_Order_Digit : constant Natural := Hexadecimal_Digit_Value (Low_Order_Digit_Representation); Number : constant Natural := (High_Order_Digit * Hexadecimal_Base) + Low_Order_Digit; ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 1:22 ` Hibou57 (Yannick Duchêne) @ 2010-01-06 1:31 ` Hibou57 (Yannick Duchêne) 2010-01-06 2:05 ` Leslie 2010-01-06 2:39 ` Leslie 2010-01-06 21:54 ` Maciej Sobczak 2 siblings, 1 reply; 22+ messages in thread From: Hibou57 (Yannick Duchêne) @ 2010-01-06 1:31 UTC (permalink / raw) Oops, I forget the last step. Once you have your Number, which is the character code (it will always be a valid character code, as it will not go beyond 255), do Character'Val (Character_Code) ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 1:31 ` Hibou57 (Yannick Duchêne) @ 2010-01-06 2:05 ` Leslie 0 siblings, 0 replies; 22+ messages in thread From: Leslie @ 2010-01-06 2:05 UTC (permalink / raw) Hibou57 (Yannick Duchêne) wrote: > Oops, I forget the last step. > > Once you have your Number, which is the character code (it will > always be a valid character code, as it will not go beyond > 255), do Character'Val (Character_Code) Okay; this will take a little while to digest. :-) No wonder I was struggling! :-) Thanks, Leslie ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 1:22 ` Hibou57 (Yannick Duchêne) 2010-01-06 1:31 ` Hibou57 (Yannick Duchêne) @ 2010-01-06 2:39 ` Leslie 2010-01-06 2:42 ` Leslie 2010-01-06 21:54 ` Maciej Sobczak 2 siblings, 1 reply; 22+ messages in thread From: Leslie @ 2010-01-06 2:39 UTC (permalink / raw) Hibou57 (Yannick Duchêne) wrote: > You may first convert from two 4 bits digits, that is, convert > "7" and "C". > > 16#7#; -- 7 > 16#C#; -- 12 > > To get 16#7C# from 16#7# and 16#C#, just do ((16#7# * 16) + > 16#C#) > > To get 16#7# and 16#C#, first get '7' and 'C' from the string, > you may do something like the following (providing your string > is named S, and it is an ASCII string) : > > High_Order_Digit_Position : constant Positive := S'First; > Low_Order_Digit_Position : constant Positive := > Positive'Succ > (High_Order_Digit_Position); > High_Order_Digit_Representation : constant Character := S > (High_Order_Digit_Position); > Low_Order_Digit_Representation : constant Character := S > (Low_Order_Digit_Position); > > To get a digit Natural value from its Character representation, > you may do something like the following (providing your > character is stored in a Character named C) : > > case C is > when '0' .. '9' => > Digit_Value := 0 + (Character'Pos (C) - Character'Pos > ('0')); > when 'A' .. 'F' => > Digit_Value := 10 + (Character'Pos (C) - Character'Pos > ('A')); > when 'a' .. 'f' => > Digit_Value := 10 + (Character'Pos (C) - Character'Pos > ('a')); > when others => > raise Program_Error; > end case; > > Wrap the latter in a function like “ function > Hexadecimal_Digit_Value (C : Character) return Natural; ” > > Then do : > > Hexadecimal_Base : constant Natural := 16; > High_Order_Digit : constant Natural := > Hexadecimal_Digit_Value > (High_Order_Digit_Representation); > Low_Order_Digit : constant Natural := > Hexadecimal_Digit_Value > (Low_Order_Digit_Representation); > Number : constant Natural := (High_Order_Digit * > Hexadecimal_Base) > + Low_Order_Digit; Okay, wait now; where did Hexadecimal_Digit_Value come from? It should be declared outside of the function, and is different than Decimal_Value, is that right? Leslie ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 2:39 ` Leslie @ 2010-01-06 2:42 ` Leslie 0 siblings, 0 replies; 22+ messages in thread From: Leslie @ 2010-01-06 2:42 UTC (permalink / raw) Leslie wrote: > Hibou57 (Yannick Duchêne) wrote: > >> You may first convert from two 4 bits digits, that is, convert >> "7" and "C". >> >> 16#7#; -- 7 >> 16#C#; -- 12 >> >> To get 16#7C# from 16#7# and 16#C#, just do ((16#7# * 16) + >> 16#C#) >> >> To get 16#7# and 16#C#, first get '7' and 'C' from the string, >> you may do something like the following (providing your string >> is named S, and it is an ASCII string) : >> >> High_Order_Digit_Position : constant Positive := S'First; >> Low_Order_Digit_Position : constant Positive := >> Positive'Succ >> (High_Order_Digit_Position); >> High_Order_Digit_Representation : constant Character := S >> (High_Order_Digit_Position); >> Low_Order_Digit_Representation : constant Character := S >> (Low_Order_Digit_Position); >> >> To get a digit Natural value from its Character >> representation, you may do something like the following >> (providing your character is stored in a Character named C) : >> >> case C is >> when '0' .. '9' => >> Digit_Value := 0 + (Character'Pos (C) - Character'Pos >> ('0')); >> when 'A' .. 'F' => >> Digit_Value := 10 + (Character'Pos (C) - >> Character'Pos >> ('A')); >> when 'a' .. 'f' => >> Digit_Value := 10 + (Character'Pos (C) - >> Character'Pos >> ('a')); >> when others => >> raise Program_Error; >> end case; >> >> Wrap the latter in a function like “ function >> Hexadecimal_Digit_Value (C : Character) return Natural; ” >> >> Then do : >> >> Hexadecimal_Base : constant Natural := 16; >> High_Order_Digit : constant Natural := >> Hexadecimal_Digit_Value >> (High_Order_Digit_Representation); >> Low_Order_Digit : constant Natural := >> Hexadecimal_Digit_Value >> (Low_Order_Digit_Representation); >> Number : constant Natural := (High_Order_Digit * >> Hexadecimal_Base) >> + Low_Order_Digit; > > Okay, wait now; where did Hexadecimal_Digit_Value come > from? It should be declared outside of the function, and is > different than Decimal_Value, is that right? > Gak! Never mind, that's the function call. :-p > Leslie ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 1:22 ` Hibou57 (Yannick Duchêne) 2010-01-06 1:31 ` Hibou57 (Yannick Duchêne) 2010-01-06 2:39 ` Leslie @ 2010-01-06 21:54 ` Maciej Sobczak 2010-01-06 22:19 ` Hibou57 (Yannick Duchêne) 2 siblings, 1 reply; 22+ messages in thread From: Maciej Sobczak @ 2010-01-06 21:54 UTC (permalink / raw) On 6 Sty, 02:22, Hibou57 (Yannick Duchêne) <yannick_duch...@yahoo.fr> wrote: > High_Order_Digit_Position : constant Positive := S'First; > Low_Order_Digit_Position : constant Positive := Positive'Succ > (High_Order_Digit_Position); Sorry, but I find it to be overly paranoid. What has happened to the old good "Something + 1"? -- Maciej Sobczak * www.msobczak.com * www.inspirel.com Database Access Library for Ada: www.inspirel.com/soci-ada ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 21:54 ` Maciej Sobczak @ 2010-01-06 22:19 ` Hibou57 (Yannick Duchêne) 2010-01-09 10:50 ` Hibou57 (Yannick Duchêne) 0 siblings, 1 reply; 22+ messages in thread From: Hibou57 (Yannick Duchêne) @ 2010-01-06 22:19 UTC (permalink / raw) On 6 jan, 22:54, Maciej Sobczak <see.my.homep...@gmail.com> wrote: > > Sorry, but I find it to be overly paranoid. > What has happened to the old good "Something + 1"? No such thing as paranoia here, as any way, for numbers, X + 1 and Number_Type'Succ (X) are strictly equivalent (thus, the Succ function attribute is not more paranoid than the “ + ” operator). As the question was from a student, I was just trying to give him/her some food for dinner. It's always good to know about Succ : it is required with enumerated types, and it is the only way to do something like “ + 1 ” when a numeric type is passed as a formal discrete type parameter of a generic. ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 22:19 ` Hibou57 (Yannick Duchêne) @ 2010-01-09 10:50 ` Hibou57 (Yannick Duchêne) 2010-01-09 11:13 ` Hibou57 (Yannick Duchêne) 0 siblings, 1 reply; 22+ messages in thread From: Hibou57 (Yannick Duchêne) @ 2010-01-09 10:50 UTC (permalink / raw) On 6 jan, 23:19, Hibou57 (Yannick Duchêne) <yannick_duch...@yahoo.fr> wrote: > On 6 jan, 22:54, Maciej Sobczak <see.my.homep...@gmail.com> wrote: > > > Sorry, but I find it to be overly paranoid. > > What has happened to the old good "Something + 1"? > > No such thing as paranoia here, as any way, for numbers, X + 1 and > Number_Type'Succ (X) are strictly equivalent (thus, the Succ function > attribute is not more paranoid than the “ + ” operator). > > As the question was from a student, I was just trying to give him/her > some food for dinner. It's always good to know about Succ : it is > required with enumerated types, and it is the only way to do something > like “ + 1 ” when a numeric type is passed as a formal discrete type > parameter of a generic. Number_Type'Pred and Number_Type'Succ are also useful when just increment are decrement are to be done on type whose standard operators are not visible. It allows to do this computation without even requiring a "use type". ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-09 10:50 ` Hibou57 (Yannick Duchêne) @ 2010-01-09 11:13 ` Hibou57 (Yannick Duchêne) 2010-01-09 11:33 ` Dmitry A. Kazakov 0 siblings, 1 reply; 22+ messages in thread From: Hibou57 (Yannick Duchêne) @ 2010-01-09 11:13 UTC (permalink / raw) On 9 jan, 11:50, Hibou57 (Yannick Duchêne) <yannick_duch...@yahoo.fr> wrote: > Number_Type'Pred and Number_Type'Succ are also useful when just > increment are decrement are to be done on type whose standard > operators are not visible. It allows to do this computation without > even requiring a "use type". Sorry, I'm back again on the topic (I'm joking with me, alone) : if I'm welcome to close my comments with more wordings, the addition and substraction have a formal logic definition which is based on the notion of successor and predecessor, so the most pure and native way of doing "+1" and "-1" is to use the successor and predecessor function. Ada knows that, yeah ;p (as its 'Pred and 'Succ are more universal than its standard "+ and "-" operators) ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-09 11:13 ` Hibou57 (Yannick Duchêne) @ 2010-01-09 11:33 ` Dmitry A. Kazakov 2010-01-09 14:50 ` Hibou57 (Yannick Duchêne) 0 siblings, 1 reply; 22+ messages in thread From: Dmitry A. Kazakov @ 2010-01-09 11:33 UTC (permalink / raw) On Sat, 9 Jan 2010 03:13:38 -0800 (PST), Hibou57 (Yannick Duch�ne) wrote: > I'm welcome to close my comments with more wordings, the addition and > substraction have a formal logic definition which is based on the > notion of successor and predecessor, so the most pure and native way > of doing "+1" and "-1" is to use the successor and predecessor > function. Ada knows that, yeah ;p (as its 'Pred and 'Succ are more > universal than its standard "+ and "-" operators) No. Addition is an operation (in an Abelian group) that may exist independently on the order relation. As an example consider the complex numbers, which is a field. As such it has addition and subtraction. Nevertheless it lacks order, and thus there is no Complex'Pred or Complex'Succ. And conversely, there can be an order, but no addition. Example: String. The guide line should be the semantics. If it is an iteration intended, then T'Succ is a clear favorite, just because x + 1 has nothing to do with enumeration. Unfortunately T'Succ is much too verbose, because it unnecessarily refers to the type. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-09 11:33 ` Dmitry A. Kazakov @ 2010-01-09 14:50 ` Hibou57 (Yannick Duchêne) 2010-01-09 16:04 ` Dmitry A. Kazakov 0 siblings, 1 reply; 22+ messages in thread From: Hibou57 (Yannick Duchêne) @ 2010-01-09 14:50 UTC (permalink / raw) On 9 jan, 12:33, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de> wrote: > No. Addition is an operation (in an Abelian group) that may exist > independently on the order relation. As an example consider the complex > numbers, which is a field. As such it has addition and subtraction. > Nevertheless it lacks order, and thus there is no Complex'Pred or > Complex'Succ. And conversely, there can be an order, but no addition. > Example: String. > > The guide line should be the semantics. If it is an iteration intended, > then T'Succ is a clear favorite, just because x + 1 has nothing to do with > enumeration. Unfortunately T'Succ is much too verbose, because it > unnecessarily refers to the type. Nice to read you Not “ No ”, while True as well. Right that it is not the only way to define it. You may define the attraction as the opposite of the repulsion, or you may define the repulsion as the opposite of the attraction. In some area, you may define Pred and Succ based on “ + ” and “ - ”, or in an as good way, define + and - based on Pred and Succ. There are no Pred and Succ on reals, complexes and the likes, but their “ + ” and “ - ” are not either the same too (homonym, but different things), and that's because their “ + ” and “ - ” are not the same that they do not have a Pred and Succ. This topic is a lot attractive ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-09 14:50 ` Hibou57 (Yannick Duchêne) @ 2010-01-09 16:04 ` Dmitry A. Kazakov 2010-01-11 15:53 ` Adam Beneschan 0 siblings, 1 reply; 22+ messages in thread From: Dmitry A. Kazakov @ 2010-01-09 16:04 UTC (permalink / raw) On Sat, 9 Jan 2010 06:50:17 -0800 (PST), Hibou57 (Yannick Duchêne) wrote: > On 9 jan, 12:33, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de> > wrote: >> No. Addition is an operation (in an Abelian group) that may exist >> independently on the order relation. As an example consider the complex >> numbers, which is a field. As such it has addition and subtraction. >> Nevertheless it lacks order, and thus there is no Complex'Pred or >> Complex'Succ. And conversely, there can be an order, but no addition. >> Example: String. >> >> The guide line should be the semantics. If it is an iteration intended, >> then T'Succ is a clear favorite, just because x + 1 has nothing to do with >> enumeration. Unfortunately T'Succ is much too verbose, because it >> unnecessarily refers to the type. > Nice to read you > > Not “ No ”, while True as well. Right that it is not the only way to > define it. > > You may define the attraction as the opposite of the repulsion, or you > may define the repulsion as the opposite of the attraction. Huh, not in intuitionism. The law of excluded middle is not an axiom, even in physics. Take diffuse reflection instead of repulsion as *an* opposite to attraction. > In some > area, you may define Pred and Succ based on “ + ” and “ - ”, or in an > as good way, define + and - based on Pred and Succ. I would rather say Succ *consistent* with +, IFF both exist. > There are no Pred and Succ on reals, complexes and the likes, BTW, there is Float'Succ! I tempted to say due to a language design flaw, because it exposes the nature of Float as a model. There is a big difference between real and complex numbers. The former have the relation >. Real'Succ does not exist because the set {t|t>x} is open, therefore there is no Min{t|t>x}, only Inf{t|t>x} = x. Complex numbers do not even have >. > but > their “ + ” and “ - ” are not either the same too (homonym, but > different things), and that's because their “ + ” and “ - ” are not > the same that they do not have a Pred and Succ. There exists a subset of Complex isomorphic to Real. In mathematics that subset is indistinguishable from Real. In a typed system of Ada they are different not because of their properties, but nominally due to their attribution to different types. Moreover even type X is new Float is not Float in Ada. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-09 16:04 ` Dmitry A. Kazakov @ 2010-01-11 15:53 ` Adam Beneschan 2010-01-11 18:13 ` Dmitry A. Kazakov 2010-01-15 19:59 ` Hibou57 (Yannick Duchêne) 0 siblings, 2 replies; 22+ messages in thread From: Adam Beneschan @ 2010-01-11 15:53 UTC (permalink / raw) On Jan 9, 8:04 am, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de> wrote: > > There are no Pred and Succ on reals, complexes and the likes, > > BTW, there is Float'Succ! I tempted to say due to a language design flaw, > because it exposes the nature of Float as a model. I don't see this is a flaw. It's a reminder that a Float, on any computer, in any language, is actually limited to a subset of rational numbers, which means you need to exercise some caution when using them. Programmers who think "Float" can represent any real number, and that arithmetic on them is just like doing arithmetic on real numbers, are going to get into trouble, so it doesn't seem like a good thing to hide "the nature of Float as a model". Anyway, Float'Succ was not present in Ada 83 but was added in Ada 95, so someone must have thought it was useful for some practical purpose. -- Adam ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-11 15:53 ` Adam Beneschan @ 2010-01-11 18:13 ` Dmitry A. Kazakov 2010-01-15 19:59 ` Hibou57 (Yannick Duchêne) 1 sibling, 0 replies; 22+ messages in thread From: Dmitry A. Kazakov @ 2010-01-11 18:13 UTC (permalink / raw) On Mon, 11 Jan 2010 07:53:14 -0800 (PST), Adam Beneschan wrote: > On Jan 9, 8:04�am, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de> > wrote: > >>> There are no Pred and Succ on reals, complexes and the likes, >> >> BTW, there is Float'Succ! I tempted to say due to a language design flaw, >> because it exposes the nature of Float as a model. > > I don't see this is a flaw. It's a reminder that a Float, on any > computer, in any language, is actually limited to a subset of rational > numbers, Technically no: 1. the radix can be irrational = you can have a finite set or irrational numbers 2. infinite sets of rational numbers still have no Succ But I know what you meant. Yes any set of model numbers is finite. BUT that alone is not enough to have Succ well-defined. Consider the model x=P/Q, where P and Q are bignums. In this model of real numbers there is no meaningful Succ. > which means you need to exercise some caution when using > them. Programmers who think "Float" can represent any real number, > and that arithmetic on them is just like doing arithmetic on real > numbers, are going to get into trouble, so it doesn't seem like a good > thing to hide "the nature of Float as a model". But also using operations specific to the given implementation (model) make the program fragile. I think that Succ is OK for fixed-point numbers, but less sure in the case of floating-point ones. > Anyway, Float'Succ was not present in Ada 83 but was added in Ada 95, > so someone must have thought it was useful for some practical purpose. They also added 'Adjacent, which could serve same purpose. However upper and lower pairs +,-,*,/ would be more useful. (Upper + is greater or equal to the exact +). -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-11 15:53 ` Adam Beneschan 2010-01-11 18:13 ` Dmitry A. Kazakov @ 2010-01-15 19:59 ` Hibou57 (Yannick Duchêne) 2010-01-15 22:06 ` John B. Matthews 1 sibling, 1 reply; 22+ messages in thread From: Hibou57 (Yannick Duchêne) @ 2010-01-15 19:59 UTC (permalink / raw) On 11 jan, 16:53, Adam Beneschan <a...@irvine.com> wrote: > I don't see this is a flaw. It's a reminder that a Float, on any > computer, in any language, is actually limited to a subset of rational > numbers, which means you need to exercise some caution when using > them. Programmers who think "Float" can represent any real number, > and that arithmetic on them is just like doing arithmetic on real > numbers, are going to get into trouble, so it doesn't seem like a good > thing to hide "the nature of Float as a model". > > Anyway, Float'Succ was not present in Ada 83 but was added in Ada 95, > so someone must have thought it was useful for some practical purpose. > > -- Adam While I agree with Dimitry Pred and Succ on Float are mostly a strange idea, I agree Float (computer floats) and reals are not the same. Just to give a good example about, a Python methods associated to Float : float.as_integer_ratio(), which is defined as : > Return a pair of integers whose ratio is exactly equal to the original float > and with a positive denominator. Raises OverflowError on infinities and a > ValueError on NaNs. Found here : http://docs.python.org/library/stdtypes.html Funny to apply this on Pi :P Yes, Floats are a special things, they are not reals, although this can be formalized ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-15 19:59 ` Hibou57 (Yannick Duchêne) @ 2010-01-15 22:06 ` John B. Matthews 0 siblings, 0 replies; 22+ messages in thread From: John B. Matthews @ 2010-01-15 22:06 UTC (permalink / raw) In article <95653745-d21d-4cf9-8604-315ff185021a@j5g2000yqm.googlegroups.com>, Hibou57 (Yannick Duchêne) <yannick_duchene@yahoo.fr> wrote: > Found here : http://docs.python.org/library/stdtypes.html > > Funny to apply this on Pi :P Well, I had to try it: >>> float.as_integer_ratio(math.pi); (884279719003555L, 281474976710656L) It's not as good as my favorite, due to Arima Yoriyuki: <http://en.wikipedia.org/wiki/Arima_Yoriyuki> $ cat pi.bc #!/usr/bin/bc -lq scale = 30 print "python: ",884279719003555/281474976710656,"\n" print "Arima: ",428224593349304/136308121570117,"\n" print "4*a(1): ",4*a(1),"\n" print "Wiki: 3.141592653589793238462643383279\n" quit $ ./pi.bc python: 3.141592653589793115997963468544 Arima: 3.141592653589793238462643383275 4*a(1): 3.141592653589793238462643383276 Wiki: 3.141592653589793238462643383279 -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews> ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 0:39 How to convert a string containing two hex digits to a character? Leslie 2010-01-06 0:43 ` Leslie @ 2010-01-06 5:17 ` tmoran 2010-01-06 22:19 ` Leslie 2010-01-06 22:22 ` Hibou57 (Yannick Duchêne) 2010-01-07 14:26 ` John B. Matthews 2 siblings, 2 replies; 22+ messages in thread From: tmoran @ 2010-01-06 5:17 UTC (permalink / raw) Hex_Pair : String(1 .. 2) := "7C"; Char : Character := Character'val(Integer'value("16#" & Hex_Pair & "#")); Of course if this is homework in an Intro to Ada class, the teacher may not believe you came up with this by yourself. ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 5:17 ` tmoran @ 2010-01-06 22:19 ` Leslie 2010-01-06 22:22 ` Hibou57 (Yannick Duchêne) 1 sibling, 0 replies; 22+ messages in thread From: Leslie @ 2010-01-06 22:19 UTC (permalink / raw) tmoran@acm.org wrote: > Hex_Pair : String(1 .. 2) := "7C"; > > Char : Character > := Character'val(Integer'value("16#" & Hex_Pair & "#")); > > Of course if this is homework in an Intro to Ada class, the > teacher may not believe you came up with this by yourself. No, this is not for school. :-) After 30 years of COBOL and REXX, I'm trying to teach myself Ada, which I'm finding a lot more understandable than C, especially for text manipulation. Thanks very much for this elegant solution; text handling seems to be a somewhat neglected subject in the Ada books and web sites I've been reading. Leslie ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 5:17 ` tmoran 2010-01-06 22:19 ` Leslie @ 2010-01-06 22:22 ` Hibou57 (Yannick Duchêne) 1 sibling, 0 replies; 22+ messages in thread From: Hibou57 (Yannick Duchêne) @ 2010-01-06 22:22 UTC (permalink / raw) On 6 jan, 06:17, tmo...@acm.org wrote: > Hex_Pair : String(1 .. 2) := "7C"; > > Char : Character > := Character'val(Integer'value("16#" & Hex_Pair & "#")); > > Of course if this is homework in an Intro to Ada class, the teacher may not > believe you came up with this by yourself. We do not know about the context, and depending on the context, as en example, if he/she was supposed to learn more about attributes, then your way would be the better one (not easy to give a good answer to a student when we do not know about the context and the real expectations) ^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: How to convert a string containing two hex digits to a character? 2010-01-06 0:39 How to convert a string containing two hex digits to a character? Leslie 2010-01-06 0:43 ` Leslie 2010-01-06 5:17 ` tmoran @ 2010-01-07 14:26 ` John B. Matthews 2 siblings, 0 replies; 22+ messages in thread From: John B. Matthews @ 2010-01-07 14:26 UTC (permalink / raw) In article <hi0m2j$df4$1@news.albasani.net>, Leslie <jlturriff@centurytel.net> wrote: > I need to be able to convert a string variable containing two > hexadecimal digits (e.g. "7C") to its equivalent character > value. It's not directly related to your question, but you might enjoy looking at this program for converting the other way: <http://sites.google.com/site/drjohnbmatthews/hexdump> -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews> ^ permalink raw reply [flat|nested] 22+ messages in thread
end of thread, other threads:[~2010-01-15 22:06 UTC | newest] Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2010-01-06 0:39 How to convert a string containing two hex digits to a character? Leslie 2010-01-06 0:43 ` Leslie 2010-01-06 1:22 ` Hibou57 (Yannick Duchêne) 2010-01-06 1:31 ` Hibou57 (Yannick Duchêne) 2010-01-06 2:05 ` Leslie 2010-01-06 2:39 ` Leslie 2010-01-06 2:42 ` Leslie 2010-01-06 21:54 ` Maciej Sobczak 2010-01-06 22:19 ` Hibou57 (Yannick Duchêne) 2010-01-09 10:50 ` Hibou57 (Yannick Duchêne) 2010-01-09 11:13 ` Hibou57 (Yannick Duchêne) 2010-01-09 11:33 ` Dmitry A. Kazakov 2010-01-09 14:50 ` Hibou57 (Yannick Duchêne) 2010-01-09 16:04 ` Dmitry A. Kazakov 2010-01-11 15:53 ` Adam Beneschan 2010-01-11 18:13 ` Dmitry A. Kazakov 2010-01-15 19:59 ` Hibou57 (Yannick Duchêne) 2010-01-15 22:06 ` John B. Matthews 2010-01-06 5:17 ` tmoran 2010-01-06 22:19 ` Leslie 2010-01-06 22:22 ` Hibou57 (Yannick Duchêne) 2010-01-07 14:26 ` John B. Matthews
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox