how to convert unsigned char* to integer fast,

In my project i wrote such function for conversion:

    // len should be > 0
uint32_t stringToInt(unsigned char const* buffer, int len) {
    uint32_t result = buffer[0] - '0';
    for (int i = 1; i < len; i++) {
        result *= 10;
        result += buffer[i] - '0';
    return result;

Are there any stl / boost methods that can do the same with the same speed? If not so then probaly you can further improve my version?

I can not use atoi because it doesn't allow to provide len. I also don't want to create temp buffer just for atoi call.


From atoi implementation in C, with minor modification to account for length.

int my_atoi(char *p,int len) {
 int k = 0;
 for (int i=0;i<len;i++) {
 k = (k<<3)+(k<<1)+(*p)-'0';
 return k;

If you want the fastest possible atoi implementation, one way to go would be to check gcc source for their implementation of atoi and modify it to match your additional requirement for length...

Need Your Help

XMLHttpRequest SXH_OPTION_SELECT_CLIENT_SSL_CERT Naming Convention and Conflicts

c++ msxml x509

Does anyone know how the client certificate lookup works with XMLHttpRequest from MSXML.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.