Solved

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

Posted on 2003-02-26
Medium Priority
202 Views
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
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
• 2

Expert Comment

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

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

Sorry
0

LVL 6

Accepted Solution

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

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

## Featured Post

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.
###### Suggested Courses
Course of the Month8 days, 15 hours left to enroll