Back to index

nux  3.0.0
NumberConversion.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 #include "NuxCore.h"
00023 #include "NumberConversion.h"
00024 
00025 namespace nux
00026 {
00027 
00034   NString IntegerToChar (int value, int base)
00035   {
00036     char result[65];
00037 
00038     // check that the base if valid
00039     if (base < 2 || base > 36)
00040     {
00041       //*result = '\0';
00042       NString str = TEXT ('\0');
00043       return str;
00044     }
00045 
00046     char *ptr = result, *ptr1 = result, tmp_char;
00047     int tmp_value;
00048 
00049     do
00050     {
00051       tmp_value = value;
00052       value /= base;
00053       *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base) ];
00054     }
00055     while ( value );
00056 
00057     // Apply negative sign
00058     if (tmp_value < 0)
00059       *ptr++ = '-';
00060 
00061     *ptr-- = '\0';
00062 
00063     while (ptr1 < ptr)
00064     {
00065       tmp_char = *ptr;
00066       *ptr-- = *ptr1;
00067       *ptr1++ = tmp_char;
00068     }
00069 
00070     NString str = result;
00071     return str;
00072   }
00073 
00074   double CharToDouble (const TCHAR *digit)
00075   {
00076     char *endptr = NULL;
00077     NString str = TCHAR_TO_ANSICHAR (digit);
00078     errno = 0;
00079     double ret = std::strtod (str.GetTCharPtr(), &endptr);
00080     unsigned int error = errno;
00081 
00082     if (error == ERANGE)
00083     {
00084       nuxDebugMsg ("[CharToDouble] Out for range value");
00085     }
00086 
00087     return ret;
00088   }
00089 
00090   NString DoubleToChar (double d)
00091   {
00092     TCHAR *buffer = new TCHAR[64];
00093     Memset (buffer, 0, 64);
00094     SPRINTF_S (buffer, 64, "%.39f", d);
00095     NString str = buffer;
00096     str = str.TrimRight (TEXT ("0") );
00097 
00098     delete[] buffer;
00099     return str;
00100   }
00101 
00102   int CharToInteger (const TCHAR *digit)
00103   {
00104     NString str = TCHAR_TO_ANSICHAR (digit);
00105     long long ret = std::atoi (str.GetTCharPtr() );
00106     return ret;
00107   }
00108 
00109 // convert an hexadecimal string to unsigned int
00110   unsigned int HexCharToInteger (const TCHAR *s)
00111   {
00112     int n = 0;         // position in string
00113     int m = 0;         // position in digit[] to shift
00114     int count;         // loop index
00115     unsigned long intValue = 0;  // integer value of hex string
00116     int digit[16];      // hold values to convert
00117 
00118     const TCHAR *hexStg = s;
00119 
00120     if ( (s[0] == TEXT ('0') ) && ( (s[1] == TEXT ('X') ) || (s[1] == TEXT ('x') ) ) )
00121     {
00122       hexStg = s + 2;
00123     }
00124 
00125     while (n < 16)
00126     {
00127       if (hexStg[n] == TEXT ('\0') )
00128         break;
00129 
00130       if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
00131         digit[n] = hexStg[n] & 0x0f;            //convert to int
00132       else if (hexStg[n] >= TEXT ('a') && hexStg[n] <= TEXT ('f') ) //if a to f
00133         digit[n] = (hexStg[n] & 0x0f) + 9;      //convert to int
00134       else if (hexStg[n] >= TEXT ('A') && hexStg[n] <= TEXT ('F') ) //if A to F
00135         digit[n] = (hexStg[n] & 0x0f) + 9;      //convert to int
00136       else break;
00137 
00138       n++;
00139     }
00140 
00141     count = n;
00142     m = n - 1;
00143     n = 0;
00144 
00145     while (n < count)
00146     {
00147       // digit[n] is value of hex digit at position n
00148       // (m << 2) is the number of positions to shift
00149       // OR the bits into return value
00150       intValue = intValue | (digit[n] << (m << 2) );
00151       m--;   // adjust the position to set
00152       n++;   // next digit to process
00153     }
00154 
00155     return (intValue);
00156   }
00157 
00158 }