Hi.
I know how to convert strings to ints however the strings i want to convert contain leading 0's, i no that ints arent stored like this, however is there anyway around?
e.g. -- "0912"
i want my int to be 0912.
Thanks in advance.
Urema.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 21, 2009, 6:11 AM EDT
Explain me What you want as Final Result .
What you will pass and what you want as Out put.. I will Quickly resolve your problem :)
person91 reputationPosted Aug 21, 2009, 6:13 AM EDT
Thanks Kirtan.
Urema.
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 21, 2009, 6:26 AM EDT
Here is What you want .. ( Code Will Convert Each Digit in Their ASCII Equivalent :)
let me know if you are having any difficulties.
private void button1_Click(object sender, EventArgs e)
{
// Convert TextBox Number Charater Array
char[] nums = textBox2.Text.Trim().ToCharArray();
// Varaible for Storing Final Complete Integer Equivalant of Number
string FinalNumber = "";
//Process Each Characters
foreach (char c in nums)
{
FinalNumber += Convert.ToInt32(c);
}
//Store Final Concataed String into Integer
int fnum = Convert.ToInt32(FinalNumber);
//Show it In MessageBox
MessageBox.Show(fnum.ToString());
}
person91 reputationPosted Aug 21, 2009, 6:39 AM EDT
Thats fine for having the ASCII equivalent, however the string, say "0912", that i want converted is actually a primary key, so passing the ascii as a parameter wont work.
Am I right in saying that ints dont store leadng zeros?
Thats basically the problem, i want ints to retain leading zeros when converted from a string to an int.
Any suggestions, Kirtan? Anyone?
Thanks
Urema.
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 21, 2009, 6:59 AM EDT
You can not store Leading Zero in int Column of Database for that you need to use varchar column .
or as another solution
Store the value in database as integer and In Front End Of Application Display it with leading Zero .
you can display this type of result using Query in Front End
select ('0'+Convert(varchar(MAX),Amount)) from Table
person91 reputationPosted Aug 21, 2009, 7:40 AM EDT
I have just implemeneted the keys now as varchar, all is grand.
Thanks again
Urema.