Changeset 1339


Ignore:
Timestamp:
05/06/25 23:04:33 (2 days ago)
Author:
Maciej Komosinski
Message:

Added two helper conversion functions: toDouble(), toUInt()

Location:
cpp/common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/Convert.cpp

    r1285 r1339  
    1818
    1919#include <stdio.h>
     20#include <stdlib.h>
    2021#include <locale>
    2122#include <iostream>
     
    2526
    2627int Convert::toInt(string s) { return atoi(s.c_str()); }
     28unsigned int Convert::toUInt(string s)
     29{
     30        const char* begin = s.c_str();
     31        char *end;
     32        unsigned int value = strtoul(begin, &end, 10);
     33        return (end == begin) ? 0 : value;
     34}
    2735int Convert::toInt_HexIf0x(string s) { return (s.size() > 2 && s[0] == '0' && s[1] == 'x') ? (int)hexToInt(s.substr(2)) : atoi(s.c_str()); }
    2836float Convert::toFloat(string s) { return (float)atof(s.c_str()); }
     37double Convert::toDouble(string s) { return atof(s.c_str()); }
    2938string Convert::toLowerCase(string s) { std::transform(s.begin(), s.end(), s.begin(), ::tolower);  return s; }
    3039string Convert::toUpperCase(string s) { std::transform(s.begin(), s.end(), s.begin(), ::toupper);  return s; }
  • cpp/common/Convert.h

    r1285 r1339  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2025  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1212#include <cmath>
    1313
    14 #ifdef LINUX
    15 #define UINT64_FORMAT "%llu" //we want to avoid this and ambiguous "long long", but gcc does not seem to support I64u (yet?)
    16 #else
    17 #define UINT64_FORMAT "%I64u"
    18 #endif
    1914
    2015struct Convert
     
    2217public:
    2318        static int toInt(string s);
     19        static unsigned int toUInt(string s);
    2420        static int toInt_HexIf0x(string s);
    2521        static float toFloat(string s);
     22        static double toDouble(string s);
    2623        static string toLowerCase(string s);
    2724        static string toUpperCase(string s);
Note: See TracChangeset for help on using the changeset viewer.