comp.lang.ada
 help / color / mirror / Atom feed
From: "Hibou57 (Yannick Duchêne)" <yannick_duchene@yahoo.fr>
Subject: Re: How to convert a string containing two hex digits to a character?
Date: Tue, 5 Jan 2010 17:22:01 -0800 (PST)
Date: 2010-01-05T17:22:01-08:00	[thread overview]
Message-ID: <eb6745fd-aa93-4ce5-a925-0099716c700d@c34g2000yqn.googlegroups.com> (raw)
In-Reply-To: hi0ma4$df4$2@news.albasani.net

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;



  reply	other threads:[~2010-01-06  1:22 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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) [this message]
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
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox