#ifndef RR_CONV_MBI
#define RR_CONV_MBI 1

;############################################################################
; RR_CONV_MBI (rr_conv.mbi)
;    written and © by Ralph Reuchlein 1997-1999
;
; Conversion functions
;############################################################################
; HiStory:
;
; Date        MUIbase   What?
; ----        -------   -----
; 29-Mar-97   0.22      · (hex intval)
;                       · (numhex intval)
;                       · (iINT intval)
; 19-Jul-97   0.27      · updated all variable types
; 08-Apr-98   0.39      · (ddMMMyy date)
;                       · (ddMMMyyyy date)
;## FROM NOW ON MUIBASE IS PUBLIC ###########################################
; 
;############################################################################
; Includes

#ifndef RR_GENERAL_MBI
#include "MUIbase:include/rr_general.mbi"
#endif
#ifndef RR_TIMEDATE_MBI
#include "MUIbase:include/rr_timedate.mbi"
#endif
;############################################################################





;## ----------------------------------------------------------------
;## (hex intval)
;## ----------------------------------------------------------------
;## converts a integer number to a hexadecimal string (without
;## leading $ or 0x or something else)

FUNCTION hex (intval:INT)
  (LET (i:INT (s:STR ""))
    (SETQ intval (DIV intval 16))
    (SETQ s (+ (numhex (MOD intval 16)) s))
    (DO (i) (> intval 0)
      (SETQ s (+ (numhex (MOD intval 16)) s))
      (SETQ intval (DIV intval 16))
    )
    (IF (> (MOD (LEN s) 2) 0) THEN (SETQ s (+ "0" s)))
    (RETURN s)
  )
END





;## ----------------------------------------------------------------
;## (numhex intval)
;## ----------------------------------------------------------------
;## converts only values 0 <= x <= 15 into their hexadecimal
;## pendants

FUNCTION numhex_old (intval:INT)
  (RETURN
    (CASE (intval)
      (0 "0")
      (1 "1")
      (2 "2")
      (3 "3")
      (4 "4")
      (5 "5")
      (6 "6")
      (7 "7")
      (8 "8")
      (9 "9")
      (10 "A")
      (11 "B")
      (12 "C")
      (13 "D")
      (15 "E")
      (16 "F")
    )
  )
END

;# faster version ;-)
FUNCTION numhex (intval:INT)
  (NTH intval (LIST "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F"))
END


;## ----------------------------------------------------------------
;## (iINT intval)
;## ----------------------------------------------------------------
;## returns a string containing the internal representation of an
;## integer (to be exact: an 32 bit integer)
;## BEWARE: (CHR 0) returns an empty string, which leads to
;## unpredictable results!!!

FUNCTION iINT (intval:INT)
  (LET (i:INT (s:STR "") m:INT)
    (DOTIMES (i 4)
      (SETQ m (MOD intval 256))
      (SETQ intval (DIV intval 256))
      (SETQ s (+ (CHR m) s))
    )
    (RETURN (SPRINTF s))
  )
END





;## ----------------------------------------------------------------
;## (ddMMMyy date)
;## ----------------------------------------------------------------
;## converts date into a string using the format dd-MMM-yy, where
;## dd is the 2digit number for the day, MMM is the abbreviation of
;## the month name and yy is the number of years in the current
;## century
;## WARNING: I engaged my brain and wrote ONE expression! :-)

FUNCTION ddMMMyy (d:DATE)
  (SPRINTF "%02i-%.3s-%02i" (INT (LEFTSTR (STR d) 2)) (monNum2Str (INT (MIDSTR (STR d) 3 2))) (INT (RIGHTSTR (STR d) 2)) )
END





;## ----------------------------------------------------------------
;## (ddMMMyyyy date)
;## ----------------------------------------------------------------
;## converts date into a string using the format dd-MMM-yyyy, where
;## dd is the 2digit number for the day, MMM is the abbreviation of
;## the month name and yyyy is the 4digit number of years
;## WARNING: I engaged my brain and wrote ONE expression! :-)

FUNCTION ddMMMyyyy (d:DATE)
  (SPRINTF "%02i-%.3s-%04i" (INT (LEFTSTR (STR d) 2)) (monNum2Str (INT (MIDSTR (STR d) 3 2))) (INT (RIGHTSTR (STR d) 4)) )
END





;## ----------------------------------------------------------------
;## ----------------------------------------------------------------



#endif
