3

I am using the following code to convert Const char * to Unsigned long int, but the output is always 0. Where am I doing wrong? Please let me know.

Here is my code:

#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

int main() 
{
    vector<string> tok;
    tok.push_back("2");
    const char *n = tok[0].c_str();
    unsigned long int nc;
    char *pEnd;
    nc=strtoul(n,&pEnd,1);
    //cout<<n<<endl;
    cout<<nc<<endl; // it must output 2 !?
    return 0;
}
user2754070
  • 509
  • 1
  • 7
  • 16
  • 1
    BTW, if anyone wants to convert it to an `unsigned long long`, the function is [`std::stoull`](http://www.cplusplus.com/reference/string/stoull/), while an `unsigned long` is [`std::stoul`](http://www.cplusplus.com/reference/string/stoul/). – Volomike Feb 08 '16 at 21:33

3 Answers3

4

Use base-10:

nc=strtoul(n,&pEnd,10);

or allow the base to be auto-detected:

nc=strtoul(n,&pEnd,0);

The third argument to strtoul is the base to be used and you had it as base-1.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
2

You need to use:

nc=strtoul(n,&pEnd,10);

You used base=1 that means only zeroes are allowed.

If you need info about integer bases you can read this

ST3
  • 8,826
  • 3
  • 68
  • 92
  • Thanks! for the link. So this base is normally octal/ decimal/ binary/ hexa etc right? – user2754070 Oct 09 '13 at 12:46
  • Yes, also exists some *exotic* bases as in your case base(1), I cannot find word for that. – ST3 Oct 09 '13 at 12:47
  • What if I want conversion from `Const char * to Unsigned long int *` ? Can you please let me know... – user2754070 Oct 09 '13 at 13:21
  • @user result after conversion is storen into memory and its representation is variable in this case variable is named `nc`. If you want to have pointer you need to use `&nc` – ST3 Oct 09 '13 at 13:24
  • @user this needs to be changed into: `unsigned long int nc; nc=strtoul(n,&pEnd,10); cout<<&nc< – ST3 Oct 09 '13 at 13:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38890/discussion-between-user2754070-and-st3) – user2754070 Oct 09 '13 at 13:35
1

The C standard library function strtoul takes as its third argument the base/radix of the number system to be used in interpreting the char array pointed to by the first argument.

Where am I doing wrong?

nc=strtoul(n,&pEnd,1);

You're passing the base as 1, which leads to a unary numeral system i.e. the only number that can be repesented is 0. Hence you'd get only that as the output. If you need decimal system interpretation, pass 10 instead of 1.

Alternatively, passing 0 lets the function auto-detect the system based on the prefix: if it starts with 0 then it is interpreted as octal, if it is 0x or 0X it is taken as hexadecimal, if it has other numerals it is assumed as decimal.

Aside:

  • If you don't need to know the character upto which the conversion was considered then passing a dummy second parameter is not required; you can pass NULL instead.
  • When you're using a C standard library function in a C++ program, it's recommended that you include the C++ version of the header; with the prefix c, without the suffix .h e.g. in your case, it'd be #include <cstdlib>
  • using namespace std; is considered bad practice
Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222