Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Specific numbers from an int

Status
Not open for further replies.

SmileeTiger

Programmer
Mar 13, 2000
200
0
0
US
I have an array of type long: V[2] with V[0]=2445553 & V[1]=65556 (for example)<br><br>If I wanted to get specific digits (say the 1st 3rd and 5th) from V[0]is there an easy way to get these digits?<br><br>Cory.
 
char text[nn];<br>char first_char;<br>sprintf(text,&quot;%ld&quot;,v[0]);<br><br>fist_char=text[0]; <br><br>etc....<br><br>Easy way; Good luck;<br><br><br> <p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br>
 
<br> <p>Siddhartha Singh<br><a href=mailto:siddhu_singh@hotmail.com>siddhu_singh@hotmail.com</a><br><a href=siddhu.freehosting.net> </a><br>
 
You can use a method which consist to divide 10 & sub to the original . Look :<br><br>int a =123456<br>Last=a-INT(a/10)*10=6<br>after you divide by 10 & you redo it ...<br>Last=a-INT(a/10)*10=5<br>
 
the sprintf(text,&quot;%ld&quot;,v[0]);<br>&nbsp;method worked well!<br><br>Thanks everyone. The /10 method was what I WAS going to do but didn't wanna :)<br><br>Cory
 
What about if I wanted to go the other way?<br><br>If I had a character array <br>char array[]=&quot;123456&quot; and I wanted to get to an int?<br><br>
 
To be more specific I want to convert each number seperatly.. so I wanna do somthing like this:<br>int number=atoi(array[0])
 
I know of course I can do it this way<br>Num_Char={12,3,5,6};<br><br>for (int i=0; i&lt; 4; i++)<br>{<br><br> Number<i>=Num_char<i>;<br> Number<i>-=49; //Converts the ascii code into a int.<br>}<br><br>But I don't like it. There has to be a cleaer way
 
Why not the simple way?<br>char text[nn];<br>int number;<br><br>strcpy=text,&quot;123456&quot;);<br>number=atoi(text);<br> <p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br>
 
Well the ting is. I needed to put it into an a rray for later use. Ni biggie. What I have works fine.<br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top