#include <String>
#include "DotNetString.h"
#include <stdlib.h>

namespace DotNetStr
{
	/*
	wchar_t*	StringToWChar( String * Str )
	{
		if( Str == NULL || Str->Length == 0 )
			return L"";
		wchar_t utf16 __gc[] = Encoding::Default->GetChars( Str );
		wchar_t __pin *charStr = &utf16[0];      
		return charStr;      
	}
	*/
	std::string   StringToChar( String * Str )
    {
        if( Str == NULL || Str->Length == 0 )
            return "";
        char utf8 __gc[] = Encoding::Default->GetBytes( Str );
        char __pin *charStr = &utf8[0];      
        return charStr;      
    }

    int   StringToInt( String * Str )
    {
		std::string retStr = DotNetStr::StringToChar( Str ); 
        //char* cStr = DotNetStr::StringToChar( Str ); 
		const char* cStr = retStr.c_str();

		int RetValue = atol(  cStr );
		
		if( RetValue != 0 )
			return RetValue;
        
		if ( cStr ) 
		{
			if(		cStr[0] != '0' 
				||  cStr[1] != 'x' )
			{
				return 0;
			}
			int len = (int)strlen( cStr );
			if ( len > 10 ) len = 10;
			for ( int i = 2 ; i < len  ; i++ ) 
			{
				RetValue = RetValue * 16;
				if ( cStr[i] >= '0' && cStr[i] <= '9' ) 
				{
					RetValue += (cStr[i] - '0') ;
				} 
				else if ( cStr[i] >= 'A' && cStr[i] <= 'F' ) 
				{
					RetValue += (cStr[i] - 'A' + 10);
				} 
				else if ( cStr[i] >= 'a' && cStr[i] <= 'f' ) 
				{
					RetValue += (cStr[i] - 'a' + 10);
				} 
				else 
				{
					return 0;
				}
			}
		}
		return RetValue;
    }

	float   StringToFloat( String * Str )
	{
		//char* cStr = DotNetStr::StringToChar( Str ); 
		return (float)atof(  DotNetStr::StringToChar( Str ).c_str() );
	}
}
