/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: s_whex.c,v 1.2 1997/09/25 18:04:46 ae59 Exp $ */
/*
$Log: s_whex.c,v $
 * Revision 1.2  1997/09/25  18:04:46  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:24:54  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : s_whex.c                              */
/*                                                              */
/*      Entry           : s_trng s_whex( r, mode)               */
/*                        a_real r;                             */
/*                        a_char mode;                          */
/*                                                              */
/*      Arguments       : r      - real value                   */
/*                                                              */
/*      Description     : write   real value to hex string      */
/*                                                              */
/*      Date            : 1993-08-25                            */
/*      1993-08-25 : new                                        */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
#endif

static char lc[] = "0123456789abcdef";
static char uc[] = "0123456789ABCDEF";

#ifdef LINT_ARGS
local s_trng s_whex(a_real r, a_char mode)
#else
local s_trng s_whex(r, mode)
a_real r;
a_char mode;
#endif
        {
        size_t i;
	   s_trng s;
	   unsigned int c;
        a_char *p;

        E_TPUSH("s_whex")

        if (mode!='x' && mode!='X')
              e_trap(INV_ARG  ,4,E_TMSG,57,E_TCHR,&mode);
	   else
        {
              s_init(&s,(size_t)2*sizeof(r));
		    if (s.ptr != NULL)
		    {
		    	s.clen=s.alen;
		    	s.tmp= TRUE;
#if INTEL
              	p = ((a_char *)&r)+(sizeof(r)-1);
              	for (i=0;i<sizeof(r);i++)
		    	{
		    	  	 c=(unsigned int) ((*p & 0xf0)>>4);
        			 s.ptr[2*i] = (mode=='x') ? lc[c] : uc[c];
        			 c=(unsigned int) (*p-- & 0x0f);
        			 s.ptr[2*i+1] = (mode=='x') ? lc[c] : uc[c];
#else
              	p = (a_char *)&r;
              	for (i=0;i<sizeof(r);i++)
		    	{
		    	  	 c=(unsigned int) ((*p & 0xf0)>>4);
        			 s.ptr[2*i] = (mode=='x') ? lc[c] : uc[c];
        			 c= (unsigned int)(*p++ & 0x0f);
        			 s.ptr[2*i+1] = (mode=='x') ? lc[c] : uc[c];
#endif
              	}
              }
        }
        E_TPOPP("s_whex")
        return s;
}
