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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.224.130.195 with SMTP id u3mr7630861qas.1.1367449746121; Wed, 01 May 2013 16:09:06 -0700 (PDT) X-Received: by 10.50.196.227 with SMTP id ip3mr1832154igc.10.1367449746081; Wed, 01 May 2013 16:09:06 -0700 (PDT) Path: border1.nntp.ams3.giganews.com!border1.nntp.ams2.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!209.85.216.87.MISMATCH!l3no4693qak.0!news-out.google.com!ef9ni42111qab.0!nntp.google.com!m7no4619qam.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 1 May 2013 16:09:05 -0700 (PDT) In-Reply-To: <36095985-768d-42f0-a6ab-17eb0e896a4d@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.20.190.126; posting-account=lJ3JNwoAAAAQfH3VV9vttJLkThaxtTfC NNTP-Posting-Host: 69.20.190.126 References: <931385b2-3520-4d7a-b56f-6d0d0b06d467@googlegroups.com> <443a92d3-2327-423d-ad74-174b0a69e4c4@googlegroups.com> <0b7431ad-fa72-45d4-ac26-20b2c0785d16@googlegroups.com> <36095985-768d-42f0-a6ab-17eb0e896a4d@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: AVR Usart send number string with no 'Image From: Shark8 Injection-Date: Wed, 01 May 2013 23:09:06 +0000 Content-Type: text/plain; charset=ISO-8859-1 X-Original-Bytes: 3739 Xref: number.nntp.dca.giganews.com comp.lang.ada:181338 Date: 2013-05-01T16:09:05-07:00 List-Id: On Wednesday, May 1, 2013 5:00:19 PM UTC-6, Rego, P. wrote: Now with validation, that is considering not every three numeric-character input is valid: declare Use Interfaces; Subtype Digit is Character Range '0'..'9'; Subtype String3 is String(1..3); Function From_String(Input : String3) return Unsigned_8 is subtype Constrained is Natural Range Natural(Unsigned_8'First)..Natural(Unsigned_8'Last); Working : String3 := Input; Index : Natural := 1; Temp : Constrained := 0; has_space : Boolean := False; begin -- Preprocessing string: leading spaces are treated as 0, -- non-leading spaces indicate an error. for C of reverse Working loop case C is when Digit => if has_space then Raise Numeric_Error; end if; when ' ' => C:= '0'; has_space:= true; when others => Raise Numeric_Error; end case; end loop; Return Result : Unsigned_8 do for C of reverse Working loop -- We add the current character's value, multiplied -- by its position, to modify temp. Temp:= Temp + Index * (Character'Pos(C) - Character'Pos('0')); -- Multiply the index by 10. Index:= Index * 10; end loop; -- Finally we copy our temporary into Result. Result:= Unsigned_8(Temp); exception when Constraint_Error => raise Numeric_Error; End return; End From_String; -- The pad function adds the proper amount of padding. Function Pad( S : String; Length : Positive:= 3 ) Return String is ( if S'Length < Length then ' ' & S else S ); Function Pad( I : Natural ) Return String3 is Working : String renames Pad(I'Img); begin -- if the length is 3, then the 'in' test works, if Working in String3 then return Working; else -- Othereise, we only want the last three characters... return working( Working'Last-2..Working'Last ); end if; end Pad; -- Testing variable. Temp : Natural := 4; begin -- Test while Temp < 256 loop Ada.Text_IO.Put_Line( From_String(Pad(Temp))'Img ); --To_String( Unsigned_8(Temp) ) ); Temp:= Temp *2; -- I want to end w/ 255, not 128. Temp:= (if temp = 256 then 255 else temp); end loop; end;