;+
; Project     :	SOLAR-B - EIS
;
; Name        :	EIS_CPT_HEXTOBYTES
;
; Purpose     :	Converts hex character values into byte values.
;
; Explanation : Input values are coded as two hex characters per byte
;
; Use         :	< bytes = eis_cpt_hextobytes(hex_bytes) >
;
; Inputs      :	hex_bytes : BYTARR[2n] hex character values.
;
; Opt. Inputs : None.
;
; Outputs     : bytes     : BYTARR[n] corresponding byte values
;
; Opt. Outputs:	None.
;
; Keywords    : ERR : named variable for reporting error..
;
; Calls       : None.
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	EIS_CPT.
;
; Prev. Hist. :	None.
;
; Written     :	Martin Carter RAL 22/10/04
;
; Modified    :	Version 0.0, 22/10/04, MKC
;
; Version     :	Version 0.0, 22/10/04
;-
;**********************************************************

FUNCTION eis_cpt_hextobytes, hex_bytes, ERR=err

  ; this could probably be better coded using READS

  ; check 2 alphanumeric characters per byte

  IF (N_ELEMENTS(hex_bytes) AND 1) NE 0 THEN GOTO, error

  ; convert lower case values to upper case
  ; A-F corresponds to byte values 65-70
  ; a-f corresponds to byte values 97-102

  list = WHERE ( hex_bytes GE 97b, count)

  IF count GT 0 THEN raw_data[list] = raw_data[list] - 32b

  ; check data is '0-9' or 'A-F'
  ; corresponding to byte values 48-57, 65-70

  dummy = WHERE (hex_bytes LT 48b OR (hex_bytes GT 57b AND hex_bytes LT 65b) OR  hex_bytes GT 70b, count)

  IF count GT 0 THEN GOTO, error

  ; convert each hex byte to 4 bit value
  ; data is '0-9' or 'A-F'
  ; corresponding to byte values 48-57, 65-70

  bytes = hex_bytes-48b

  list = WHERE(bytes GT 9b, count)

  IF count GT 0 THEN bytes[list] = bytes[list] - 7b

  ; convert each set of two 4 bit values to byte
  ; NB 1st nibble is most significant

  bytes = REFORM(bytes,2,N_ELEMENTS(bytes)/2)

  bytes = REFORM(ISHFT(bytes[0,*],4) OR bytes[1,*])

  ; return data

  RETURN, bytes

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; set error

  err = 1

  eis_cpt_message, 'INVALID HEX TO BYTE CONVERSION'

  RETURN, 0B

END





