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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,6171b9ea6dd53ab X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Advice Please Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <1163141535.700889.83160@k70g2000cwa.googlegroups.com> Date: Fri, 10 Nov 2006 10:27:28 +0100 Message-ID: <1ukn0q4n4wter$.1vanf8flu3bur.dlg@40tude.net> NNTP-Posting-Date: 10 Nov 2006 10:27:28 CET NNTP-Posting-Host: 28956c02.newsspool4.arcor-online.net X-Trace: DXC=WAh^3T1TN24]E=H1Q9`7874IUKY4kAlCLU?84 X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:7396 Date: 2006-11-10T10:27:28+01:00 List-Id: On 9 Nov 2006 22:52:15 -0800, laehyung wrote: > Advice & comments please. > my test code is ... > > with Ada.Text_IO; use Ada.Text_IO; > > with Ada.Characters.Handling; > use Ada.Characters.Handling; > with Ada.Unchecked_Conversion; > with Interfaces; use Interfaces; > with Interfaces.C; > > procedure Test_Str is > > --------------------------------------- > function Conv_U8_To_Char is > new Ada.Unchecked_Conversion(Source => Unsigned_8, > Target => Character); > function Conv_Char_To_U8 is > new Ada.Unchecked_Conversion(Source => Character, > Target => Unsigned_8); > > --------------------------------------- > > function Strhex_To_Ascii_Strbyte (Item : in String) return String is > > Strsize : Natural := Item'Length; > Rtn_Length : Natural := (Strsize)/2; > > Str_Val : String(1..Rtn_Length); > > Strtemp1 : Unsigned_8; > Strtemp2 : Unsigned_8; > C : array(1..Strsize) of Unsigned_8; > > begin > > for Idx in Item'range loop > if Is_Hexadecimal_Digit(Item(Idx)) then > if Is_Digit(Item(Idx)) then > C(Idx):= > Unsigned_8(Character'Pos(Item(Idx))-Character'Pos('0')); > else > C(Idx):= Unsigned_8(Character'Pos(To_Upper(Item(Idx))) > -Character'Pos('A')+16#A#); > end if; > else > Put_Line("Program Code contains NON-HexaDecimal character. > program Break"); > raise Data_Error; > end if; > end loop; > > for I in 1..Rtn_Length loop > Strtemp1 := Shift_Left(Unsigned_8(C(2*I-1)),4) and > Unsigned_8(16#FF#); > Strtemp2 := Unsigned_8(C(2*I)) and Unsigned_8(16#FF#); > > Str_Val(I) := Conv_U8_To_Char( Strtemp1 or Strtemp2 ); > > end loop; > > return Str_Val; > end Strhex_To_Ascii_Strbyte; > --------------------------------------- > > > Input_Str : String := "0000002D0F010000002E0F000000000A10A3"; > > begin > > --Put("Output_Str =>"); > Put(Strhex_To_Ascii_Strbyte(Input_Str)); > > end Test_Str; > > ---------------------------------------------------------------- > (my machine is windows XP) > Compile & Run: > > c:\work>gnatmke -O2 -gnatvf test_str > gcc -c -O2 -gnatvf test_str.adb > > GNAT GPL 2006 (20060522-34) > Copyright 1992-2006, Free Software Foundation, Inc. > > Compiling: test_str.adb (source file time stamp: 2006-11-10 05:22:28) > 68 lines: No errors > gnatbind -x test_str.ali > gnatlink test_str.ali > > c:\work> test_str > code_test.txt > > edit code_test.txt file with hexa code editor(ex. UltraEdit-32) > > output data shold be > 00000000h:00 00 00 2D 0F 01 00 00 00 2E 0F 00 00 00 00 00 > ;...-............ > 00000010h:0A 10 A3 0D 0A ;..?. No, it should not. I should be (in a binary output mode): 00 00 00 2D 0F 01 00 00 00 2E 0F 00 00 00 00 0A 10 A3 four zeros before the last triplet. Note also that when you write a text file the OS may translate formatters (like LF and CR). That explains the effect you've got. 0A was translated into 0D 0A pair. > but result is > 00000000h:00 00 00 2D 0F 01 00 00 00 2E 0F 00 00 00 00 0D > ;...-............ > 00000010h:0A 10 A3 0D 0A ;..?. > > the data of address 0Fh is 0D, why? > How to correct this problem. There is no problem, your code works. Admittedly, the code needs much rework both in terms of efficiency and clarity. Use case statement for character classification or else a translation map. You don't need the array C, everything can be done in one pass. You don't need modular arithmetic and masking. Use integer multiplication and addition. Use 'Val attribute instead of Unchecked_Conversion. What would be the result for an odd-length string? -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de