|
-
Mar 5th, 2008, 11:43 AM
#1
Thread Starter
Fanatic Member
[2005] convet sting to integer in the same format
dear friend,
i need to convert the following string in to integer.
"002"
i need to convert to integer then my ineger should be as follows;
002
i have tried using CInt. but it gives 2.so how can i convert this to get integer as 002.
-
Mar 5th, 2008, 11:46 AM
#2
Re: [2005] convet sting to integer in the same format
002 isn't an integer or any kind of number. If you want to keep the zeroes in front, you need to keep it as a string. Leading zeroes mean nothing in a number.
-
Mar 5th, 2008, 11:55 AM
#3
Re: [2005] convet sting to integer in the same format
As Tom said, you can't.
One thing which you might want to do is keep the leading 0's for display purposes and i've found that pad left etc... is good for this.
(Just as a quick example)
vb Code:
Dim myStr1 As String = "002" Dim myStr2 As String = "005" Dim total As Integer = CInt(myStr1) + CInt(myStr2) MessageBox.Show(total.ToString.PadLeft(3, "0"))
-
Mar 5th, 2008, 12:00 PM
#4
Thread Starter
Fanatic Member
Re: [2005] convet sting to integer in the same format
dear friends
thanks for the reply.
-
Mar 5th, 2008, 12:02 PM
#5
Re: [2005] convet sting to integer in the same format
 Originally Posted by Tom Sawyer
002 isn't an integer or any kind of number. If you want to keep the zeroes in front, you need to keep it as a string. Leading zeroes mean nothing in a number.
002 is by all means a number and an integer. VB (any programming language for that matter) just does not hold on to non significant digits in numeric data types.
002 isn't an integer = false
-
Mar 5th, 2008, 12:05 PM
#6
Re: [2005] convet sting to integer in the same format
 Originally Posted by kleinma
002 is by all means a number and an integer. VB (any programming language for that matter) just does not hold on to non significant digits in numeric data types.
002 isn't an integer = false
No, 002 is a string. 2 is an integer. When any language reads 002 as an integer, it strips off the zeros and reads it as 2.
-
Mar 5th, 2008, 12:07 PM
#7
Re: [2005] convet sting to integer in the same format
no
"002" is a string
002 is an integer
Go find a mathematician, and tell them 002 is not an integer value
-
Mar 5th, 2008, 12:09 PM
#8
Re: [2005] convet sting to integer in the same format
 Originally Posted by kleinma
no
"002" is a string
002 is an integer
Go find a mathematician, and tell them 002 is not an integer value
We're talking about programming, not theoretical mathematics.
Go into Visual Studio. Type:
Dim x as integer
x = 002
Then press enter and see what happens. Go find a programmer who will have anything else happen.
-
Mar 5th, 2008, 12:10 PM
#9
Addicted Member
Re: [2005] convet sting to integer in the same format
I guess for the purposes of programming languajes it's pointless to wheter or not 002 is an integer or string. Such discussion doesn't serve the purpose of the thread I would guess.
-
Mar 5th, 2008, 12:17 PM
#10
Re: [2005] convet sting to integer in the same format
 Originally Posted by Tom Sawyer
002 isn't an integer or any kind of number.
Tom you said 002 is not an integer or any kind of number.
Yes I know VB strips insignificant digits from numbers. That is very true. That however does NOT mean 002 is not a number. You called it a string..
However which line of code is going to give you an error?
Code:
Dim myInt as Integer = 002
Dim myString as String = 002
Sorry.. we could fight over it I suppose, but what is the point?
I know the message you were trying to convey to the OP, and I think you answered his question to his liking, I am just correcting what was incorrectly stated in your post.
-
Mar 5th, 2008, 12:24 PM
#11
Re: [2005] convet sting to integer in the same format
Dim myInt as Integer = 002 becomes
Dim myInt as Integer = 2 as soon as I press enter.
What mathematicians call a number is not relevant. What the CLR interprets a System.Int32 value as is what matters, as that is the definition of an integer in visual basic. Leading zeros are not included in this interpretation, so if you want to have something with leading zeros, you cannot use this value type to store it on the stack. For the definition of a Visual Basic Integer, that's all that matters.
-
Mar 5th, 2008, 12:26 PM
#12
Re: [2005] convet sting to integer in the same format
 Originally Posted by kleinma
no
"002" is a string
002 is an integer
Go find a mathematician, and tell them 002 is not an integer value
This is correct. I cant see why you would want to keep the leading zeros in any calculations but for displaying its just a formatting issue.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 5th, 2008, 12:32 PM
#13
Re: [2005] convet sting to integer in the same format
 Originally Posted by Tom Sawyer
Dim myInt as Integer = 002 becomes
Dim myInt as Integer = 2 as soon as I press enter.
What mathematicians call a number is not relevant. What the CLR interprets a System.Int32 value as is what matters, as that is the definition of an integer in visual basic. Leading zeros are not included in this interpretation, so if you want to have something with leading zeros, you cannot use this value type to store it on the stack. For the definition of a Visual Basic Integer, that's all that matters.
it doesn't store them because they make no difference in the math that will be done with them. It would just be wasted 0s taking up space in memory.
Just because the IDE happens to be productive and strips insignificant digits when you use them, still does not mean 002 is not a number.
If I code in notepad, and compile against the VBC I could write
Dim myInteger as integer = 002
Notepad sure isn't going to strip those 0s for me. The compiler will, but I will never see that happen.
I am not trying to fight you on what the IDE or VB language does to leading 0s. I am just saying your claim that 002 is not a number simply is not correct. If you don't want to agree with that, then that is your decision.
-
Mar 5th, 2008, 12:35 PM
#14
Re: [2005] convet sting to integer in the same format
Crikey, I go away for 15 minutes and this simple little post has turned mental.
Tom,
Kleinma was only pointing out that your blanket sounding statement ("002 isn't any kind of number") wasn't accurate and is fair enough.
It's just a matter of scope really. You are saying when you press return the 0's won't stay there within the IDE which is fair enough. But that's not the same as saying 002 isn't a numerical type (inside or outside of any IDE) which is what your statement sort of implied.
I don't want to get caught in any cross fire here, so I'm waving my white flag and running away but i know exactly what has caused this and as Kleinma has said - it's not worth any more time.
*runs and ducks for cover*
-
Mar 5th, 2008, 01:26 PM
#15
Re: [2005] convet sting to integer in the same format
 Originally Posted by kleinma
I am not trying to fight you on what the IDE or VB language does to leading 0s. I am just saying your claim that 002 is not a number simply is not correct. If you don't want to agree with that, then that is your decision.
It isn't a number. By number, I of course mean a numeric value type that the CLR can read. Which one of those allocates memory on the stack for a value that contains leading zeros? If there isn't one, then 002 is not a number inside of VB or any other .NET language.
This is a programming forum, so the programmatic definition of a number is the only one that matters. The IDE's functionality, while indictive of what happens on a lower level, is not the core issue. The core issue is what gets compiled to the IL and the CLR. A mathematician's definition of an integer is as relative to the discussion as it would be if there's some cook who calls a pound of flour an integer - it just doesn't matter as relates to a VB program.
Inside VB, 002 is not a number.
-
Mar 5th, 2008, 01:31 PM
#16
Re: [2005] convet sting to integer in the same format
Like I said, there is no point in arguing this further. You keep your opinion, and I will keep mine.
Maybe someday you will go for some programming job and they will ask you what 002 is. Go ahead and tell them its not a number, its a string.
-
Mar 5th, 2008, 01:34 PM
#17
Re: [2005] convet sting to integer in the same format
 Originally Posted by kleinma
Like I said, there is no point in arguing this further. You keep your opinion, and I will keep mine.
Maybe someday you will go for some programming job and they will ask you what 002 is. Go ahead and tell them its not a number, its a string.
It's not an opinion, it's a fact. There is no System.Int32 value of 002.
Maybe someday, you will have a programming job where you will be asked to store leading zeros in a numeric data type. Good luck with that.
-
Mar 5th, 2008, 01:44 PM
#18
Re: [2005] convet sting to integer in the same format
002 = 2 = 00000000000000000000000000000000002
insignificant digits (leading 0s) is concept.... they all equal values..
The statement you made, that prompted my comment, was when you said 002 is "NOT ANY KIND OF NUMBER". To that I said you are wrong, 002 is very much a number.
I never say you could store insignificant digits in numeric data types, all I said was 002 is a number. You called it a string.
It is a fact that you can not store insigificant digits in numeric data types, it is NOT a fact that 002 is not a number, and it is very much not a string...
-
Mar 5th, 2008, 01:53 PM
#19
Re: [2005] convet sting to integer in the same format
It doesn't matter what it is as a concept, it matters what it is as a value on the stack. A System.Int32 value type does not store a value of 002 or 0000000002 or any other number of leading zeros and a 2. It only stores the 2. Therefore, inside of visual basic, there is no integer with a value of 002.
The fact that outside of visual basic, there are things called integers that can have a value of 002 is not relevant to the fact that there is nothing inside of it that can have that value.
-
Mar 5th, 2008, 01:58 PM
#20
Re: [2005] convet sting to integer in the same format
but numbers exist outside visual basic, and you said 002 is not a number.
Am I taking you too literally? Maybe... but my point in posting was just to correct that generalized statement you made, because just because YOU know how VB/CLR treats numbers, and I know as well, doesn't mean a few million others who might come across this thread know.
So to make a statement like 002 is not a number, versus saying something like System.Int32 can not represent insignificant digits, I felt compelled to post and say I don't agree with the statement.
-
Mar 5th, 2008, 02:09 PM
#21
Re: [2005] convet sting to integer in the same format
Actually you are all wrong. I am willing to bet that the system will store you integer 2 or 002 or 0000000002 as:
0x00000002
And int64 on a 64 bit operating system:
0x0000000000000002
Mathematically speaking 002 is an integer. If you want to display 002 in your program it needs to be converted to a string otherwise only significant figures will be displayed; therefore in programming speak you cannot store the number of leading zeros unless you store it as string so 002 can only be represented literally with a string. If you want to store 002 then store it as an int or a long or a byte.
Why the pointless argument?
-
Mar 5th, 2008, 02:13 PM
#22
Re: [2005] convet sting to integer in the same format
To avoid VS interference, I created a .vb file
Code:
Module Main
Sub Main()
Dim i as Int32 = 002
Console.WriteLine(i.GetType().Name)
End Sub
End Module
(notepad)
And then ran vbc.exe against it.
vbc test.vb
The .exe generated gave this output:
Int32
That means that 002 is a number, although the leading 0s may be removed at runtime.
-
Mar 5th, 2008, 02:15 PM
#23
Re: [2005] convet sting to integer in the same format
I am willing to argue in favour for the addition of this new sting data type
-
Mar 5th, 2008, 02:15 PM
#24
Re: [2005] convet sting to integer in the same format
 Originally Posted by kleinma
but numbers exist outside visual basic, and you said 002 is not a number.
Am I taking you too literally? Maybe... but my point in posting was just to correct that generalized statement you made, because just because YOU know how VB/CLR treats numbers, and I know as well, doesn't mean a few million others who might come across this thread know.
So to make a statement like 002 is not a number, versus saying something like System.Int32 can not represent insignificant digits, I felt compelled to post and say I don't agree with the statement.
If someone asks how to do something in a loop, do you tell him how to do it while he's driving on a roundabout, or do you understand that, due to the forum he's posting in, he's referring to the programmatic definition of a loop and not some other definition of the word? Why would it be different for an integer?
Integer means System.Int32. They're synonyms. Number means numeric datatype. It doesn't refer to a song someone is playing or any other definition of the word that isn't referring to a programming concept.
-
Mar 5th, 2008, 02:24 PM
#25
Re: [2005] convet sting to integer in the same format
Sorry, forgot the ILDASM output
Code:
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 24 (0x18)
.maxstack 1
.locals init (int32 V_0)
IL_0000: ldc.i4.2
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: box [mscorlib]System.Int32
IL_0008: callvirt instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
IL_000d: callvirt instance string [mscorlib]System.Reflection.MemberInfo::get_Name()
IL_0012: call void [mscorlib]System.Console::WriteLine(string)
IL_0017: ret
} // end of method Main::Main
Hope that doesn't help in any constructive way at all.
-
Mar 5th, 2008, 02:36 PM
#26
Re: [2005] convet sting to integer in the same format
 Originally Posted by visualAd
Actually you are all wrong. I am willing to bet that the system will store you integer 2 or 002 or 0000000002 as:
0x00000002
And int64 on a 64 bit operating system:
0x0000000000000002
So what you are saying then, is if you assign a number to an integer type in VB, and that number does not fill up all the bytes of that data type, then what fills those leading spaces in memory??? 0s????
-
Mar 5th, 2008, 02:39 PM
#27
Re: [2005] convet sting to integer in the same format
Actually, it helps immensely:
IL_0003: box [mscorlib]System.Int32
An extra line of processing is required to convert the value you typed in into an Int32. This is all I'm saying.
Also, how come your IDE lets you put i = 002? Mine changes it to a 2.
-
Mar 5th, 2008, 02:41 PM
#28
Re: [2005] convet sting to integer in the same format
because his IDE is notepad...
-
Mar 5th, 2008, 02:42 PM
#29
Re: [2005] convet sting to integer in the same format
I am almost 100% certain that 2
as int32 looks like this in memory
00000000000000000000000000000010
and as Int64 like this in memory
0000000000000000000000000000000000000000000000000000000000000010
As custom we write numbers without leading zero's, but it is just a custom. No mathmatical rule or formula that I am aware of would break down if we chose not to observe the custom.
1 + 1 = 2
0001 + 0001 = 0002
Last edited by dbasnett; Mar 5th, 2008 at 03:11 PM.
-
Mar 5th, 2008, 02:59 PM
#30
Re: [2005] convet sting to integer in the same format
 Originally Posted by kleinma
because his IDE is notepad...
Notepad isn't an IDE. IDE means Integrated Development Environment. Notepad is simply a text editor and this definition doesn't apply to it.
-
Mar 5th, 2008, 03:00 PM
#31
Re: [2005] convet sting to integer in the same format
No, no, let me explain. In order to avoid the IDE formatting the 002 in the first place, I created the console app in notepad and then compiled it with vbc.exe to see how the compiler would handle it.
-
Mar 5th, 2008, 03:05 PM
#32
Re: [2005] convet sting to integer in the same format
 Originally Posted by mendhak
No, no, let me explain. In order to avoid the IDE formatting the 002 in the first place, I created the console app in notepad and then compiled it with vbc.exe to see how the compiler would handle it.
That's pretty f-ing clever. Good solution.
-
Mar 5th, 2008, 03:07 PM
#33
Re: [2005] convet sting to integer in the same format
 Originally Posted by Tom Sawyer
That's pretty f-ing clever. Good solution.
cough see post #13 cough
-
Mar 5th, 2008, 03:18 PM
#34
Re: [2005] convet sting to integer in the same format
 Originally Posted by kleinma
cough see post #13 cough
Very well. You're clever too. I didn't see that part.
-
Mar 5th, 2008, 03:28 PM
#35
Re: [2005] convet sting to integer in the same format
I can think of a custom where leading zero's are used, military time(see below). The below list has two errors though.
12:00 AM -- 0000 hrs
1:00 AM -- 0100 hrs
2:00 AM -- 0200 hrs
3:00 AM -- 0300 hrs
4:00 AM -- 0400 hrs
5:00 AM -- 0500 hrs
6:00 AM -- 0600 hrs
7:00 AM -- 0700 hrs
8:00 AM -- 0800 hrs
9:00 AM -- 0900 hrs
10:00 AM -- 1000 hrs
11:00 AM -- 1100 hrs
12:00 PM -- 1200 hrs
1:00 PM -- 1300 hrs
2:00 PM -- 1400 hrs
3:00 PM -- 1500 hrs
4:00 PM -- 1600 hrs
5:00 PM -- 1700 hrs
6:00 PM -- 1800 hrs
7:00 PM -- 1900 hrs
8:00 PM -- 2000 hrs
9:00 PM -- 2100 hrs
10:00 PM -- 2200 hrs
11:00 PM -- 2300 hrs
Last edited by dbasnett; Mar 5th, 2008 at 03:33 PM.
-
Mar 5th, 2008, 03:32 PM
#36
Re: [2005] convet sting to integer in the same format
Let's just agree that we're all clever and I'm 002 times as clever as both of you. 
I also have a feeling that this debate will be revisited in a few months time, these things don't go away.
-
Mar 5th, 2008, 04:27 PM
#37
Re: [2005] convet sting to integer in the same format
002 as in little endian or big endian?
-
Mar 5th, 2008, 10:28 PM
#38
Re: [2005] convet sting to integer in the same format
First, this thread should be in more general forum. Why just in Visual Basic.NET. Share it with everyone.
Second, this is my opinion, you may agree or not:
In mathematics: "two always equals two", otherwise "two" does not belong to a numeric/counting system. That is the uniqeness of every number in a numeric system.
In a computer system: "two may not equal two".
"Byte" number two is stored by 8 bits as 00000010
"Integer" number two is stored by 16 bits as 0000000000000010
"Long" number two is stored by 32 bits as 00000000000000000000000000000010
(You know how to write down "Int64" number two and so on)
So, actually this "two" does not equal the other "two".
"2" is the visual represent of number "two" by mean of an Arabic numeric system. Yes, that is Arabic.
For a long time the western world use Rome's sytem: I II III IV V VI ... and in this sytem "II" represents number "two".
The Chinese people use "二" to represent number "two".
The Arabic numeric system originally did not have number "0". To make it becomes a complete numeric system (when doing subtraction a-b with a=b leading to the creation of 0), that "0" was added in not long ago, just 4-5 centuries and that "0" came from India in an original form of a small dot (·).[*]
(open this thread of our forum too view the discussion on the problem of missing "ZERO" in a counting system, that is used to denote columns in Excel)
Under Arabic numeric system (with 0), traditionally on writting a number, all leading 0's are dropped, because 2 = 02 = 002 = 0002 = 0...02. That is an axiom of this system.
So, by mean of a mathematics numeric system, 002 and 2 are two of infinite ways to present the number "two".
And 002 is really a number in this system.
Added: If you want a computer system display number "two" as 002 instead of 2, just ask Bill Gates, may be he can do it.
Added again: Have you ever seen a banking accouting report that comes straight from Cobol output? Most of numbers come with leading 0's.
[*] Correction: I mixed up between 0 (·) and decimal-dot (.). That was corrected as blue text above.
Last edited by anhn; Mar 6th, 2008 at 06:22 AM.
-
Mar 6th, 2008, 05:46 AM
#39
Re: [2005] convet sting to integer in the same format
 Originally Posted by mendhak
No, no, let me explain. In order to avoid the IDE formatting the 002 in the first place, I created the console app in notepad and then compiled it with vbc.exe to see how the compiler would handle it.
Your test was crippled by the fact that you told the compiler explicitly what you demanded the number should be cast to eg...
Code:
Dim i as Int32 = 002
So it really doesn't prove much of anything apart from the fact that your compiler works
I don't live here any more.
-
Mar 6th, 2008, 06:05 PM
#40
Hyperactive Member
Re: [2005] convet sting to integer in the same format
Pfaff! What's all this decimal tish tosh anyway?
I just entered
int a = 0x002;
in to my C# ide and it took it (where 0x is the denominator for hexadecimal). I think in vb it's
a = &H002
So in Hex, 002 is definetly a valid number. Next thing you'll be telling me is that A isn't a valid number!
"I'd rather have a full bottle in front of me than a full frontal lobotomy!"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|