root/trunk/tango/scrapple/util/Hex.d

Revision 48, 7.1 kB (checked in by flithm, 6 months ago)

Remove toHexString

Line 
1 /*******************************************************************************
2
3         Copyright:      Copyright (c) 2008 Tango.Scrapple. All rights reserved.
4
5         License:        BSD style: $(LICENSE)
6
7         Version:        Aug 29 2007: Initial release
8             Feb 15 2007: add toHexString
9             Feb 25 2007: remove toHexString
10
11         Authors:        Moritz Warning
12         
13         Handy routines for working in base 16
14
15 *******************************************************************************/
16
17 module  tango.scrapple.util.Hex;
18
19 import  tango.io.Stdout;
20
21 /*******************************************************************************
22
23  Print a hex dump:
24
25  Length: 27 bytes
26  0       54 68 69 73 20 69 73 20  61 20 73 61 6d 70 6c 65   |This.is. a.sample|
27  16      20 73 74 72 69 6e 67 20  31 32 33                  |.string. 123     |
28
29  mark_pos: marker '|' appears before position in hex field
30
31   Author:                   Moritz Warning
32
33 *******************************************************************************/
34
35 void hexDump(ubyte[] data, uint mark_pos = 0)
36 {
37         alias Stdout o;
38         o("Length: ")(data.length)(" bytes");
39         if(mark_pos)
40         {
41                 o("; Mark Position: ");
42                 if(mark_pos < data.length)
43                 {
44                         o(mark_pos)(", Line ")(mark_pos/16);
45                 }
46                 else
47                 {
48                         o("[out of range]");
49                 }
50         }
51         else
52         {
53                 mark_pos = data.length;
54         }
55         o('\n');
56        
57         char[2] hex;
58        
59         //convert byte to hex
60         void putHex(ubyte x)
61         {
62                 static const char char_array[16]  = "0123456789abcdef";
63                 hex[0] = char_array[(x >> 4)];
64                 hex[1] = char_array[(x & 0xF)];
65                 o(hex);
66         };
67        
68         void putChar(char c)
69         {
70                 o((c > 32 && c < 127) ? c : '.');
71         };
72        
73         //print all full lines
74         uint pos = 0;
75         for(ushort line = 0; line < data.length/16; line++)
76         {
77                 //print current position
78                 o(pos);
79                 o('\t');
80                
81                 for (ubyte i = 0; i < 8; i++, pos++)
82                 {
83                         o((mark_pos == pos) ? '|' : ' ');
84                         putHex(data[pos]);
85                 }
86                 o(' ');
87                 for (ubyte i = 0; i < 8; i++, pos++)
88                 {
89                         o((mark_pos == pos) ? '|' : ' ');
90                         putHex(data[pos]);
91                 }
92                
93                 o("  |"c);
94                 pos -= 16;
95                
96                 for (ubyte i = 0; i < 8; i++, pos++)
97                 {
98                         putChar(data[pos]);
99                 }
100                 o(' ');
101                 for (ubyte i = 0; i < 8; i++, pos++)
102                 {
103                         putChar(data[pos]);
104                 }
105                 o("|\n"c);
106                 o.flush(); //TODO: neccessary?
107         }
108        
109         uint lpos = data.length - pos;
110         if(lpos == 0) return;
111
112         o(pos);
113         o('\t');
114        
115         if(lpos < 9) //print lines with less then 9 hex values
116         {
117                 for (ubyte i = 0; i < lpos; i++, pos++)
118                 {
119                         o((mark_pos == pos) ? '|' : ' ');
120                         putHex(data[pos]);
121                 }
122                
123                 //padding for hex field
124                 for (ubyte i = 0; i < (3 * 16) - (3 * lpos) + 1; i++)
125                 {
126                         o(' ');
127                 }
128                
129                 o("  |"c);
130                 pos -= lpos;
131                
132                 for (ubyte i = 0; i < lpos; i++, pos++)
133                 {
134                         putChar(data[pos]);
135                 }
136                 o(' ');
137                
138                 //padding for text field
139                 for (ubyte i = 0; i < 16 - lpos; i++)
140                 {
141                         o(' ');
142                 }
143                 o("|\n"c);
144         }
145         else //print lines with less then 16 hex values
146         {
147                 for (ubyte i = 0; i < 8; i++, pos++)
148                 {
149                         o((mark_pos == pos) ? '|' : ' ');
150                         putHex(data[pos]);
151                 }
152                 o(' ');
153                 for (ubyte i = 0; i < lpos - 8; i++, pos++)
154                 {
155                         o((mark_pos == pos) ? '|' : ' ');
156                         putHex(data[pos]);
157                 }
158                 //padding for hex field
159                 for (ubyte i = 0; i < (3 * 16) - (3 * lpos); i++)
160                 {
161                         o(' ');
162                 }
163                
164                 o("  |"c);
165                 pos -= lpos;
166                
167                 for (ubyte i = 0; i < 8; i++, pos++)
168                 {
169                         putChar(data[pos]);
170                 }
171                 o(' ');
172                 for (ubyte i = 0; i < lpos - 8; i++, pos++)
173                 {
174                         putChar(data[pos]);
175                 }
176                
177                 //padding for text field
178                 for (ubyte i = 0; i < 16 - lpos; i++)
179                 {
180                         o(' ');
181                 }
182                 o("|\n"c);
183         }
184         o.flush();
185 }
186
187 /*******************************************************************************
188  
189   Convert a hex string to a ubyte
190  
191   Params:       hex =           The hext string to convert
192                 ub =            The return value
193   Returns                       True on success
194
195   Author:                   Moritz Warning
196
197 *******************************************************************************/
198
199 bool hexToUbyte(char[] hex, out ubyte ub)
200 {
201     if (hex.length != 2)
202         return false;
203
204     char c1 = hex[0];
205     char c2 = hex[1];
206
207     if (c1 >= 'A' && c1 <='F')
208         c1 += 32;
209     if (c2 >= 'A' && c2 <='F')
210         c2 += 32;
211
212     int cCount;
213
214     foreach (i, d; DIGITS)
215     {
216         if (d == c1)
217         {
218             ub = ub | i << 4;
219             cCount++;
220         }
221
222         if (d == c2)
223         {
224             ub |= i;
225             cCount++;
226         }
227
228         if (cCount == 2)
229             break;
230     }
231
232     return cCount == 2;
233 }
234
235 /*******************************************************************************
236  
237   Convert a ubyte to a hex string
238  
239   Params:       hex =           The hex string to convert
240                 ub =            The return value
241
242   Author:                   Moritz Warning
243
244 *******************************************************************************/
245
246 void ubyteToHex(inout ubyte ub, char[] hex)
247 {
248     assert (hex.length == 2);
249
250     hex[0] = DIGITS[ub >> 4];
251     hex[1] = DIGITS[ub & 0x0F];
252 }
253
254 private
255 {
256     const char[] INVALID_UUID_STRING = "Invalid UUID string format";
257     const char[] DIGITS = "0123456789abcdef";
258 }
Note: See TracBrowser for help on using the browser.