#include "archinc.h"
#include "charconv.h"

#ifdef CONFIG_INCLUDE_CHARCONV

int charconvFromFloat32(float inFloat32, char * pOutBytes, int numBytes)
{
  int i;
  union {
    float f;
    char ch[sizeof(float)];
    } u;
    
  u.f = inFloat32;
  
  for(i=0;i<sizeof(float);i++,pOutBytes++)
    *pOutBytes = u.ch[i];

  return 0;
}

int charconvFromSInt32(signed long int inSInt32, char * pOutBytes, int numBytes)
{
  int i;
  union {
    signed long int sint;
    char ch[sizeof(signed long int)];
    } u;
    
  u.sint = inSInt32;
  
  for(i=0;i<sizeof(signed long int);i++,pOutBytes++)
    *pOutBytes = u.ch[i];

  return 0;
}

int charconvFromSInt16(signed short int inSInt16, char * pOutBytes, int numBytes)
{
  int i;
  union {
    signed short int ssint;
    char ch[sizeof(signed short int)];
    } u;
    
  u.ssint = inSInt16;
  
  for(i=0;i<sizeof(signed short int);i++,pOutBytes++)
    *pOutBytes = u.ch[i];

  return 0;
}

int charconvFromSInt8(signed char inSInt8, char * pOutBytes, int numBytes)
{
  *pOutBytes = inSInt8;
  return 0;
}

int charconvFromUInt32(unsigned long int inUInt32, char * pOutBytes, int numBytes)
{
  int i;
  union {
    unsigned long int ulint;
    char ch[sizeof(unsigned long int)];
    } u;
    
  u.ulint = inUInt32;
  
  for(i=0;i<sizeof(unsigned long int);i++,pOutBytes++)
    *pOutBytes = u.ch[i];

  return 0;
}

int charconvFromUInt16(unsigned short int inUInt16, char * pOutBytes, int numBytes)
{
  int i;
  union {
    unsigned short int usint;
    char ch[sizeof(unsigned short int)];
    } u;
    
  u.usint = inUInt16;
  
  for(i=0;i<sizeof(signed short int);i++,pOutBytes++)
    *pOutBytes = u.ch[i];

  return 0;
}

int charconvFromUInt8(unsigned char inUInt8, char * pOutBytes, int numBytes)
{
  *pOutBytes = (char)inUInt8;
  return 0;
}

int charconvFromBoolean(boolean inBool, char * pOutBytes, int numBytes)
{
  if (inBool == true)
    *pOutBytes = 1;
  else
    *pOutBytes = 0;

  return 0;
}

int charconvToFloat32(float * pOutFloat32, char * pInBytes, int numBytes)
{
  int i;
  union {
    float f;
    char ch[sizeof(float)];
    } u;
    
  for(i=0;i<sizeof(float);i++,pInBytes++)
    u.ch[i] = *pInBytes;

  *pOutFloat32 = u.f;
  
  return 0;
}

int charconvToSInt32(signed long int * pOutSInt32, char * pInBytes, int numBytes)
{
  int i;
  union {
    signed long int sli;
    char ch[sizeof(signed long int)];
    } u;
    
  for(i=0;i<sizeof(signed long int);i++,pInBytes++)
    u.ch[i] = *pInBytes;

  *pOutSInt32 = u.sli;
  
  return 0;
}

int charconvToSInt16(signed short int * pOutSInt16, char * pInBytes, int numBytes)
{
  int i;
  union {
    signed short int ssi;
    char ch[sizeof(signed short int)];
    } u;
    
  for(i=0;i<sizeof(signed short int);i++,pInBytes++)
    u.ch[i] = *pInBytes;

  *pOutSInt16 = u.ssi;
  
  return 0;
}

int charconvToSInt8(signed char * pOutSInt8, char * pInBytes, int numBytes)
{
  *pOutSInt8 = *pInBytes;
  return 0;
}

int charconvToUInt32(unsigned long int * pOutUInt32, char * pInBytes, int numBytes)
{
  int i;
  union {
    unsigned long int uli;
    char ch[sizeof(unsigned long int)];
    } u;
    
  for(i=0;i<sizeof(unsigned long int);i++,pInBytes++)
    u.ch[i] = *pInBytes;

  *pOutUInt32 = u.uli;
  
  return 0;
}

int charconvToUInt16(unsigned short int * pOutUInt16, char * pInBytes, int numBytes)
{
  int i;
  union {
    unsigned short int usi;
    char ch[sizeof(unsigned short int)];
    } u;
    
  for(i=0;i<sizeof(unsigned short int);i++,pInBytes++)
    u.ch[i] = *pInBytes;

  *pOutUInt16 = u.usi;
  
  return 0;
}

int charconvToUInt8(unsigned char * pOutUInt8, char * pInBytes, int numBytes)
{
  *pOutUInt8 = *pInBytes;
  return 0;
}

int charconvToBoolean(boolean * pOutBool, char * pInBytes, int numBytes)
{
  if (*pInBytes != 0)
    *pOutBool = true;
  else
    *pOutBool = false;

  return 0;
}

#endif /* CONFIG_INCLUDE_CHARCONV */

