From: Stephen Leake on 20 Apr 2010 03:25 tonyg <tonythegair(a)googlemail.com> writes: > I have some data coming in from a serial port which I want to convert > to hexadecimal and display on the screen. I was wondering if anyone > knows of a simple way to do this? Just to chime in with my generic solution to this (from SAL http://www.stephe-leake.org/ada/sal.html) : pragma License (Modified_GPL); generic Width : Natural; type Number_Type is mod <>; function SAL.Generic_Hex_Image (Item : in Number_Type) return String; -- Return a hexadecimal image of Item, padded with leading zeros to -- Width. If Width is too small for Item, leading digits are silently -- truncated. pragma Pure (SAL.Generic_Hex_Image); function SAL.Generic_Hex_Image (Item : in Number_Type) return String is Temp : Number_Type := Item; Nibble : Number_Type; Image : String (1 .. Width); begin for I in reverse Image'Range loop Nibble := Temp mod 16; Temp := Temp / 16; if Nibble > 9 then Image (I) := Character'Val (Character'Pos ('A') + Integer (Nibble) - 10); else Image (I) := Character'Val (Character'Pos ('0') + Integer (Nibble)); end if; end loop; return Image; end SAL.Generic_Hex_Image; -- -- Stephe
From: tonyg on 20 Apr 2010 05:00 On Apr 19, 9:50 pm, Ludovic Brenta <ludo...(a)ludovic-brenta.org> wrote: > tonyg writes on comp.lang.ada: > > > Changed the hex function to > > function To_Hex (E : in Ada.Streams.Stream_Element) return String > > is > > -- Warning: not compiled and not tested... > > X : constant array (0 .. 15) of Character := > > (1) I should have written: > > X : constant array (Ada.Streams.Stream_Element range 0 .. 15) of Character := > > > ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', > > 'C', 'D', 'E', 'F'); > > Result : String (1 .. Ada.Streams.Stream_Element'Size / 4); -- 1 hex digits = 4 bits > > Working_Copy : Ada.Streams.Stream_Element := E; > > use type Ada.Streams.Stream_Element; > > First_Character : Natural := 0; > > Base : constant := 16; > > begin > > for K in reverse Result'First .. Result'Length loop > > (2) and this should be: > > for K in reverse Result'Range loop > > > Result (K) := X (integer(Working_Copy) mod integer (Base) ); > > (3) and thanks to (1), this can come back to the simpler: > > Result (K) := X (Working_Copy mod Base); > > > Working_Copy := Working_Copy / Base; > > if Working_Copy = 0 then > > First_Character := K; > > exit; > > end if; > > end loop; > > return Result (First_Character .. Result'Last); > > end To_Hex; > > > It still seems to be dropping a few zeros though and I'm stumped where > > its going wrong > > I tested it with 42 and 0 and correctly got 2A and 0, so I don't know > what you mean by that. > > Maybe the fact that the result has a variable width is a problem? If > so, here is a fixed-width variant which is actually a bit simpler: > > function To_Hex (E : in Ada.Streams.Stream_Element) return String is > X : constant array (Ada.Streams.Stream_Element range 0 .. 15) > of Character := > ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', > 'D', 'E', 'F'); > Result : String > (1 .. Ada.Streams.Stream_Element'Size / 4) -- 1 hex digits = 4 bits > := (others => '0'); > Working_Copy : Ada.Streams.Stream_Element := E; > use type Ada.Streams.Stream_Element; > Base : constant := 16; > begin > for K in reverse Result'Range loop > Result (K) := X (Working_Copy mod Base); > Working_Copy := Working_Copy / Base; > end loop; > return Result; > end To_Hex; > > and this yields 2A and 00 for my "test vector". > > -- > Ludovic Brent Its the variable width I think.
From: Peter Hermann on 20 Apr 2010 05:25 tonyg <tonythegair(a)googlemail.com> wrote: > Its the variable width I think. http://en.wikipedia.org/wiki/Netiquette and http://tools.ietf.org/html/rfc1855 for postings, look for: But do not include the entire original! alternative: many CTRL-K
From: Warren on 20 Apr 2010 10:04 Adam Beneschan expounded in news:32ac71bf-cd1e-4068-a0a6- 9491b4750e8d(a)y38g2000prb.googlegroups.com: ... >> Now if only I could find where the mayonnaise in the >> fridge is.. > > It got shoved in the back, behind that leftover chicken casserole > that's been there way too long and needs to be thrown out. > > Hope this helps, > > -- Adam Ew! I guess it helps not to have preconceptions when looking for stuff. I looked for a "white" jar of mayonnaise and of course couldn't find it. This was because my wife bought mayonnaise in a yellow plastic jar (recently), which I mistook for mustard. She of course, had no trouble at all finding it. Warren
From: J-P. Rosen on 20 Apr 2010 10:46 Warren a �crit : > I guess it helps not to have preconceptions when looking > for stuff. I looked for a "white" jar of mayonnaise and of > course couldn't find it. This was because my wife bought > mayonnaise in a yellow plastic jar (recently), which I > mistook for mustard. She of course, had no trouble at > all finding it. > Now, you understand the importance of having unambiguous specifications that match the implementation (back on topic) ;-) -- --------------------------------------------------------- J-P. Rosen (rosen(a)adalog.fr) Visit Adalog's web site at http://www.adalog.fr
|
Pages: 1 Prev: confusion with string initialization Next: Hexadecimal and stream element arrays |