-
+ F9F8125F17B849D7E49D39FF7B8C49D2B9A1ACD27B1D7A3FB449D2C9971098D1F22A64263D89D210D8C15B8AD963951D674AA4998E7823C19AB22E03C4F6373A
vtools/src/bits.adb
(0 . 0)(1 . 67)
27 with Interfaces; use Interfaces;
28 package body Bits is
29 -- helper functions
30 procedure ToBitstream(S: in String; B: out Bitstream ) is
31 V : Unsigned_8;
32 Pos : Natural;
33 begin
34 Pos := B'First;
35 for C of S loop
36 V := Character'Pos( C );
37 B( Pos ) := Bit( V and 1 );
38 B( Pos + 1 ) := Bit( Shift_Right( V, 1 ) and 1 );
39 B( Pos + 2 ) := Bit( Shift_Right( V, 2 ) and 1 );
40 B( Pos + 3 ) := Bit( Shift_Right( V, 3 ) and 1 );
41 B( Pos + 4 ) := Bit( Shift_Right( V, 4 ) and 1 );
42 B( Pos + 5 ) := Bit( Shift_Right( V, 5 ) and 1 );
43 B( Pos + 6 ) := Bit( Shift_Right( V, 6 ) and 1 );
44 B( Pos + 7 ) := Bit( Shift_Right( V, 7 ) and 1 );
45 Pos := Pos + 8;
46 end loop;
47 end ToBitstream;
48
49 procedure ToString(B: in Bitstream; S: out String ) is
50 N : Natural;
51 Pos : Natural;
52 begin
53 Pos := B'First;
54 for I in S'Range loop
55 N := Natural( B( Pos ) ) +
56 Natural( B( Pos + 1 ) ) * 2 +
57 Natural( B( Pos + 2 ) ) * 4 +
58 Natural( B( Pos + 3 ) ) * 8 +
59 Natural( B( Pos + 4 ) ) * 16 +
60 Natural( B( Pos + 5 ) ) * 32 +
61 Natural( B( Pos + 6 ) ) * 64 +
62 Natural( B( Pos + 7 ) ) * 128;
63 Pos := Pos + 8;
64 S( I ) := Character'Val( N );
65 end loop;
66 end ToString;
67
68 -- direction translation of C function from vdiff
69 procedure ToHex(B: in Bitstream; S: out String) is
70 Bytes: String(1..B'Length/8);
71 Hex_Digits: constant String := "0123456789abcdef";
72 I: Positive := 1;
73 V: Unsigned_8;
74 X: Positive := 1;
75 begin
76 ToString(B, Bytes);
77 for C of S loop
78 V := Character'Pos(C);
79 S(I) := Hex_Digits(Integer(Shift_Right(V, 4) + 1));
80 I := I + 1;
81 S(I) := Hex_Digits(Integer((V and 16#F#) + 1));
82 I := I + 1;
83 end loop;
84 end;
85
86 function ToHex(B: in Bitstream) return String is
87 S: String(1..B'Length/4);
88 begin
89 ToHex(B, S);
90 return S;
91 end;
92
93 end Bits;