×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT杂谈 / Please help C questions- convert array to unsigned long: I have an array b[0] ='1'; ..b[6]='7'; b[7]=0; I want convert to unsigned long usl; usl = atol(buffer); there is no problem, but ...
    Please help C questions- convert array to unsigned long: I have an array b[0] ='1'; ..b[6]='7'; b[7]=0; I want convert to unsigned long usl; usl = atol(buffer); there is no problem, but I find I have some problem in some special digit, so do we have the other way to convert an array to unsigned long digit? thanks in advance!
    • But .............
      it really depends on your business logic,
      for instance,
      you have n elements array
      a[0]='1';
      a[1]='3';
      ....
      a[m]='\x07';
      .....
      a[n-2]='3';
      a[n-1]='\0';

      what will you do with '\x07'?
      treat it as 7 or 0 or skip it?
    • Thanks interviewer, suppose all a[i] are legal digit, 0 -- 9, do you have the other way to convert it instead of atol();
      • I think each CS student has written that kind of functions several times.
    • sample code:
      char buffer[] = "123456.12";
      long number;
      number = atol(buffer); // number = 123456
      buffer[6] = '\x37'; // '7'
      number = atol(buffer); // number = 123456712;

      The things to pay attention to is atol() should less than LONG_MAX, otherwise, you'll got garbagge data in number because of overflow error.
    • Thanks lusi, This problem is done by using strtoul() function instead of atol(),
      • sscanf is another way.