summaryrefslogtreecommitdiff
path: root/appl/lib/encoding/base16.b
diff options
context:
space:
mode:
authorCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
committerCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
commit37da2899f40661e3e9631e497da8dc59b971cbd0 (patch)
treecbc6d4680e347d906f5fa7fca73214418741df72 /appl/lib/encoding/base16.b
parent54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff)
20060303a
Diffstat (limited to 'appl/lib/encoding/base16.b')
-rw-r--r--appl/lib/encoding/base16.b43
1 files changed, 43 insertions, 0 deletions
diff --git a/appl/lib/encoding/base16.b b/appl/lib/encoding/base16.b
new file mode 100644
index 00000000..6c91c200
--- /dev/null
+++ b/appl/lib/encoding/base16.b
@@ -0,0 +1,43 @@
+implement Encoding;
+
+include "encoding.m";
+
+hex: con "0123456789ABCDEF";
+
+enc(a: array of byte): string
+{
+ o: string;
+ for(i := 0; i < len a; i++){
+ n := int a[i];
+ o[len o] = hex[n>>4];
+ o[len o] = hex[n & 16rF];
+ }
+ return o;
+}
+
+dec(s: string): array of byte
+{
+ a := array[(len s+1)/2] of byte; # upper bound
+ o := 0;
+ j := 0;
+ n := 0;
+ for(i := 0; i < len s; i++){
+ c := s[i];
+ n <<= 4;
+ case c {
+ '0' to '9' =>
+ n |= c-'0';
+ 'A' to 'F' =>
+ n |= c-'A'+10;
+ 'a' to 'f' =>
+ n |= c-'a'+10;
+ * =>
+ continue;
+ }
+ if(++j == 2){
+ a[o++] = byte n;
+ j = n = 0;
+ }
+ }
+ return a[0:o];
+}