/* Copyright (c) 2004 by R. A. Vowels. Written August 2004. */ /* Permission is given to reproduce and to use these procedures */ /* as part of a program, and to include them as part of a larger work to be sold for profit. */ /* However, the user is not permitted to sell the procedures separately. Provided always */ /* that these procedures and this copyright notice are reproduced in full. */ /* This procedure converts a string of hexadecimal characters to characters. */ /* Each pair of hexadecimal characters is translated to one character. */ /* Invalid hexadecimal character pairs are translated to hex '00'. */ unhex: procedure (hex) returns (char(16383) varying) options (reorder); dcl hex character (*); dcl 1 hs union, 2 tr char (length(hex)), 2 int(length(hex)) fixed binary(7); dcl 1 cs union, 2 ci (0:length(hex)/2-1) fixed binary (7), 2 s character (length(hex)/2); dcl i fixed binary (31); dcl (COLLATE, ISLL, ISRL, IAND, IOR, LENGTH, TRANSLATE) builtin; if iand(length(hex_string), 1) ^= 0 then do; put skip list ('The hexadecimal string does not have an even number of characters'); signal error; end; tr = translate (hex, '0a0b0c0d0e0f0a0b0c0d0e0f00010203040506070809'x || (234)'00'x, 'abcdefABCDEF0123456789' || COLLATE ); do i = 1 to length(hex) by 2; (NOSIZE, NOFOFL): ci(isrl(i,1)) = ior(isll(int(i), 4), int(i+1)); end; return (cs.s); end unhex;