?
Solved

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

Posted on 2003-02-26
4
Medium Priority
?
202 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:DWA1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:JSand
ID: 8028921
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
0
 

Expert Comment

by:JSand
ID: 8028932
theres a typo in the scanf statement, there should be a '&' before 'myNumber'.

Sorry
0
 
LVL 6

Accepted Solution

by:
gj62 earned 200 total points
ID: 8028935
Not really enough info - do you want to extract the leftmost 3 digits?  If so, you could do:

char longStr[11];
long newlong, mylong = 12823450;

sprintf(longStr,"%ld",mylong);
longStr[3] = 0;
newlong = atol(longStr);

If this isn't what you want, please provide more details...
0
 

Author Comment

by:DWA1
ID: 8029126
Thanks to you all.Gj62 that was what I was looking for,thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question