From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,a7135c0f450945a5 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,UTF8 Path: g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!newsfeed.straub-nv.de!news.albasani.net!not-for-mail From: Leslie Newsgroups: comp.lang.ada Subject: Re: How to convert a string containing two hex digits to a character? Date: Tue, 05 Jan 2010 20:39:00 -0600 Organization: albasani.net Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8Bit X-Trace: news.albasani.net fbdRu3u5Yjqoc5+AexlerspEudD1x+8JBBU4PYvPKc3ZESoJ8pL8FJFwFZ/q1sSsj1VRTdSYs4WFpCoGbj1De6y+SN8MVjgwelT+leCOjEitEaANY+Jo3+Dlug2gciit X-Complaints-To: abuse@albasani.net NNTP-Posting-Date: Wed, 6 Jan 2010 02:38:04 +0000 (UTC) X-User-ID: Fwpwwd0TzewGrawJ9TGAy+PX8Nv8LZWkPOLQQ8HhhZLljbaGEDxYFH4dj5uOB/iWQGD39XDYjBDhCUt5l0CPqQ== Cancel-Lock: sha1:9tKSvW9BU8OEesKIjS+BHI8K2LA= User-Agent: KNode/0.10.9 X-NNTP-Posting-Host: a4YcDyYuHnlFSrLIru2cOcChIzyCZ2IhlsfwpxcobmY= Xref: g2news1.google.com comp.lang.ada:8628 Date: 2010-01-05T20:39:00-06:00 List-Id: 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