I am working on a code in ECL and trying to convert hex data into byte data. I am using the following line of code for this:
hex_data := Std.Str.ToHexPairs((>DATA<)ds.contents);
d1:=Std.Str.FromHexPairs(hex_data);
But when I uncomment the second line, I get an error. Can someone please guide me on how to correctly convert hex data to byte data in ECL?
This is the entire code am working on:
IMPORT PYTHON3 AS PYTHON;
IMPORT Std;
// Note that the filesize doesn't really show up anywhere, even though we sprayed with FILENAME,FILESIZE.
// It is embedded in the length of the contents field.
myRec := RECORD
STRING filename;
DATA contents;
END;
// I had sprayed a list of files (2) to 'thor::img.flat'. Make sure you run both the spray and the job on the thor cluster.
ds := DATASET('img.flat', myRec, THOR);
hex_data := Std.Str.ToHexPairs((>DATA<)ds.contents);
//d1:=Std.Str.FromHexPairs(hex_data);
// First output is just the file as we read it.
OUTPUT(ds[1]);
//OUTPUT(hex_data);
DATASET({UNSIGNED4 len, STRING filename, STRING img_bits, STRING img_byte}) pyProcess(STREAMED DATASET(myRec) recs) := EMBED(Python: Activity)
import binascii
for rec in recs:
# Records come in as a tuple of fields
name, dat = rec
header = dat[:16]
img_hex = binascii.hexlify(dat)
img_bytes=binascii.unhexlify(img_hex)
headerstr = ''.join(format(x, '02x') for x in header)
# We yield one record at a time (as a tuple), or we could have
# made a list and returned the whole list as one. Using yield,
# we can process as a stream, without having to load everythin
# into memory at the same time.
yield (len(dat), name, img_hex, img_bytes)
ENDEMBED;
// Second output is the results returned by python
OUTPUT(pyProcess(ds))