Link to home
Start Free TrialLog in
Avatar of DWA1
DWA1

asked on

How can I get the first three numbers from a long value

Ok lets say I had a long variable that was something like this

long mylong = 12823450;

Now what I am wanting to know is how can I just get 128 from that long.Thanks for your time and help.
Avatar of JSand
JSand

what I would do is read this in as an array of characters

ex.  char myNumber[20] = {1,2,8,2,3,4,5,0};

          or
     char myNumber[20];
     scanf("%s",myNumber);

then to get the first three numbers, have another char array:
       char smallNumber[3];

       for(i = 0; i < 3; i++)
       {
         smallNumber[i] = myNumber[i];
       }

then store the array to an integer:
       long myLong;
       myLong = atoi(smallNum);


try that, I hope this helps

--JSand
theres a typo in the scanf statement, there should be a '&' before 'myNumber'.

Sorry
ASKER CERTIFIED SOLUTION
Avatar of gj62
gj62

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DWA1

ASKER

Thanks to you all.Gj62 that was what I was looking for,thanks.