/*==================================================================================================

FILE INFORMATION HEADER

USED IN PROJECT:			S1003065-01 PPS-TX/S1003067-01 PPS-RX software application
ITEM TYPE:					Source Code
ITEM NAME:					xlru_conversion.cpp
ITEM DESCRIPTION:			This file contains the implementations for convertions functions between char* to int/float/uint/etc
TARGET:						ATSAM4LC8B / ATSAM4LS8B
DOC REFERENCES:				SWDS1003065-01/SWDS1003065-01
PROGRAMMER NAME(S):			A.Chessa, L.Vallongo
COPYRIGHT:					LEONARDO-FINMECCANICA S.p.A
REVISION HISTORY:
- 01.00: first release

====================================================================================================
© Copyright LEONARDO S.p.A.. All rights reserved
Any right of industrial and intellectual property on this document,
and of technical Know-how herein contained, belongs to
LEONARDO S.p.A. and/or third parties.
According to the law, it is forbidden to disclose, reproduce or however
use this document and any data herein contained for any use without
previous written authorization by LEONARDO S.p.A.
==================================================================================================*/
#include "xlru_common_func.h"


static bool hex2u(int* res, const char* data, unsigned int size_bytes)
{
	unsigned int v=0;
	unsigned int nibbles=size_bytes*2;
	const unsigned char* d=reinterpret_cast<const unsigned char*>(data);
	for(unsigned int i=0; i<nibbles; ++i)
	{
		v=v<<4;
		unsigned int c=d[i];

		if ((c>='0') && (c<='9'))
		{
			c=c-'0';
		}
		else if ((c>='a') && (c<='f'))
		{
			c=c-'a';
			c+=10;
		}
		else if ((c>='A') && (c<='F'))
		{
			c=c-'A';
			c+=10;
		}
		else
			return false;

		c=c & 0x0F;//Just to be sure...
	    v|=c;
	}
	*res=static_cast<int>(v);
	return true;
}

bool xlru::hex2u8(int* res, const char* data)
{
	bool ok=hex2u(res, data, 1);
	return ok;
}

bool xlru::hex2u16(int* res, const char* data)
{
	bool ok=hex2u(res, data, 2);
	return ok;
}

bool xlru::hex2u32(int* res, const char* data)
{
	bool ok=hex2u(res, data, 4);
	return ok;
}

bool xlru::hex2u48(int* res, const char* data)
{
    bool ok=hex2u(res, data, 3);
    return ok;
}

bool xlru::hex2s8(int* res, const char* data)
{
	bool ok=hex2u(res, data, 1);
	signed char c=*res;
	if (ok)
		*res=c;
	return ok;
}

bool xlru::hex2s16(int* res, const char* data)
{
	bool ok=hex2u(res, data, 2);
	signed short c=*res;
	if (ok)
		*res=c;
	return ok;
}

bool xlru::hex2u8(unsigned int* d, const char* data)
{
	int res=0;
	bool ok=hex2u(&res, data, 1);
	if (ok)
		*d=res;
	return ok;
}

bool xlru::hex2u16(unsigned int* d, const char* data)
{
	int res=0;
	bool ok=hex2u(&res, data, 2);
	if (ok)
		*d=res;
	return ok;
}

bool xlru::hex3u8f(float* res, const char* data, float lsb)
{
	int tmp;
	bool ok=hex2u8(&tmp, data);
	if (ok)
		*res=float(tmp)*lsb;
	return ok;
}

bool xlru::hex3u16f(float* res, const char* data, float lsb)
{
	int tmp;
	bool ok=hex2u16(&tmp, data);
	if (ok)
		*res=float(tmp)*lsb;
	return ok;
}

bool xlru::hex3u32f(float* res, const char* data, float lsb)
{
	int tmp;
	bool ok=hex2u32(&tmp, data);
	if (ok)
		*res=float(tmp)*lsb;
	return ok;
}


bool xlru::hex3s8f(float* res, const char* data, float lsb)
{
	int tmp;
	bool ok=hex2s8(&tmp, data);
	if (ok)
		*res=float(tmp)*lsb;
	return ok;
}

bool xlru::hex3s16f(float* res, const char* data, float lsb)
{
	int tmp;
	bool ok=hex2s16(&tmp, data);
	if (ok)
		*res=float(tmp)*lsb;
	return ok;
}








