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!
-bluewave(AlwaysStudent);
2002-2-14
{330}
(#368950@0)
-
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?
-interview(intervieweree);
2002-2-14
{239}
(#368962@0)
-
Thanks interviewer, suppose all a[i] are legal digit, 0 -- 9, do you have the other way to convert it instead of atol();
-bluewave(AlwaysStudent);
2002-2-14
(#369014@0)
-
I think each CS student has written that kind of functions several times.
-goodbaby(小宝);
2002-2-14
(#369037@0)
-
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.
-lusi(鹭鸶);
2002-2-14
{312}
(#369068@0)
-
Thanks lusi, This problem is done by using strtoul() function instead of atol(),
-bluewave(AlwaysStudent);
2002-2-14
(#369085@0)
-
sscanf is another way.
-interview(intervieweree);
2002-2-14
(#369104@0)