Как Вы получаете неподписанное долго из строки?

JVM Sun сделала, чтобы PermGen расположил с интервалами зафиксированный, и в конечном счете это все использовало (да, по-видимому из-за ошибки в classloader-связанном коде) => OOM.

, Если можно использовать JVM другого поставщика (например, Weblogic один), он динамично расширяет пространство PermGen, таким образом, Вы никогда не будете получать permgen-связанный OOM.

5
задан Daniel Bingham 27 September 2009 в 18:45
поделиться

6 ответов

One way to do it:

stringstream(str) >> ulongVariable;
5
ответ дан 18 December 2019 в 09:08
поделиться

You can use strtoul with no problem. The function returns an unsigned long. If convertion can not be performed the function return 0. If the correct long value is out of range the function return ULONG_MAX and the errno global variable is set to ERANGE.

5
ответ дан 18 December 2019 в 09:08
поделиться

If you can use the boost libraries (www.boost.org) look at the conversion library - it's a header only include

#include "boost/lexical_cast.hpp"

then all you need to do is

unsigned long ul = boost::lexical_cast<unsigned long>(str);
2
ответ дан 18 December 2019 в 09:08
поделиться
template <class T>
T strToNum(const std::string &inputString,
           std::ios_base &(*f)(std::ios_base&) = std::dec)
{
    T t;
    std::istringstream stringStream(inputString);

    if ((stringStream >> f >> t).fail())
    {
        throw runtime_error("Invalid conversion");
    }
    return t;
}


// Example usage
unsigned long ulongValue = strToNum<unsigned long>(strValue);
int intValue             = strToNum<int>(strValue);

int intValueFromHex      = strToNum<int>(strHexValue,std::hex);
unsigned long ulOctValue = strToNum<unsigned long>(strOctVal, std::oct);
4
ответ дан 18 December 2019 в 09:08
поделиться

Jeffrey Stedfast has a beautiful post about writing int parser routines for Mono (in C).
It generates code that uses uses native types (you need 32 bit to parse 32 bit) and error codes for overflow.

1
ответ дан 18 December 2019 в 09:08
поделиться

Надежный путь будет напишите статическую функцию и используйте ее

bool str2Ulong(const string& str,unsigned long & arValue)
{
   char *tempptr=NULL;
   arValue=strtoul(str,tempptr,10);

   return ! (arValue==0 && tempptr==str.c_str());

}
0
ответ дан 18 December 2019 в 09:08
поделиться
Другие вопросы по тегам:

Похожие вопросы: