Example #1
0
//============================================================================
//		NDataEncoder::Hex_Encode : Encode to hex.
//----------------------------------------------------------------------------
NString NDataEncoder::Hex_Encode(const NData &theValue)
{	NIndex			n, theSize;
	NData			tmpBuffer;
	NString			theResult;
	const uint8_t	*dataPtr;
	char			*textPtr;



	// Get the state we need
	theSize = theValue.GetSize();
	dataPtr = theValue.GetData();
	textPtr = (char *) tmpBuffer.AppendData((theSize * 2) + 1);

	if (theSize == 0 || dataPtr == NULL || textPtr == NULL)
		return(theResult);



	// Convert to a string
	for (n = 0; n < theSize; n++)
		{
		sprintf(textPtr, "%.2X", dataPtr[n]);
		textPtr += 2;
		}
	
	theResult = NString(tmpBuffer);
	
	return(theResult);
}
Example #2
0
//============================================================================
//		NDataEncoder::Hex_Decode : Decode from hex.
//----------------------------------------------------------------------------
NData NDataEncoder::Hex_Decode(const NString &theValue)
{	NIndex			n, theSize;
	NData			theResult;
	uint8_t			*dataPtr;
	const char		*textPtr;
	unsigned int	byteVal;



	// Validate our parameters
	NN_ASSERT(theValue.IsEmpty() || (theValue.GetSize() % 2) == 0);



	// Get the state we need
	theSize = theValue.GetSize() / 2;
	dataPtr = theResult.AppendData(theSize);
	textPtr = theValue.GetUTF8();

	if (theSize == 0 || dataPtr == NULL || textPtr == NULL)
		return(theResult);



	// Convert the string
	//
	// Visual Studio does not support 'hh' correctly, so we need to read byte
	// values into a temporary variable:
	//
	// https://connect.microsoft.com/VisualStudio/feedback/details/416843/sscanf-cannot-not-handle-hhd-format
	for (n = 0; n < theSize; n++)
		{
		sscanf(textPtr, "%2x", &byteVal);
		NN_ASSERT((byteVal & 0xFFFFFF00) == 0);

		dataPtr[n] = (uint8_t) byteVal;
		textPtr   += 2;
		}
	
	return(theResult);
}
Example #3
0
//============================================================================
//		NDBResult::GetValueData : Get a data value.
//----------------------------------------------------------------------------
NData NDBResult::GetValueData(NIndex theIndex) const
{	NData			theResult;
	const void		*thePtr;
	NIndex			theSize;



	// Validate our parameters
	NN_ASSERT(theIndex < GetSize());



	// Get the value
	thePtr  = (const void *) sqlite3_column_blob( (sqlite3_stmt *) mResult, theIndex);
	theSize = (NIndex      ) sqlite3_column_bytes((sqlite3_stmt *) mResult, theIndex);
	
	if (thePtr != NULL && theSize != 0)
		theResult.AppendData(theSize, thePtr);
	
	return(theResult);
}