Results 1 to 14 of 14

Thread: String to byte (not array)... Quite simple (I think)

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    String to byte (not array)... Quite simple (I think)

    Hello,
    I have a string:
    MyString="1234".
    I want to convert it to an integer or long.
    MyLong=1234

    Thx
    Xmas
    Learn, this is the Keyword...

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: String to byte (not array)... Quite simple (I think)

    VB Code:
    1. Dim str As String = "1234"
    2. Dim inter As Integer
    3. inter = CType(str, Integer)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Very, very simple
    Learn, this is the Keyword...

  4. #4
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Everything you wanted to know about type conversion functions:

    ms-help://MS.VSCC/MS.MSDNVS/vblr7/html/vaGrpTypeConversion.htm

    ms-help://MS.VSCC/MS.MSDNVS/vblr7/html/vafctctype.htm

  5. #5
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    No!

    Not that simple!
    Try this:
    VB Code:
    1. Dim str As String = "1234"
    2. Dim inter As Integer
    3. inter = Val(str)

    The previous code generates error if your string contains anything besides nummbers. For example if str="1234DF"
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Right Lunatic3 . The code I posted was based on his string value .

  7. #7
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651
    I thought val was no more to be used in VB.NET. What about

    VB Code:
    1. dim i as Integer
    2.   dim s as string
    3.   s = "1234"
    4.  
    5.   i = Integer.Parse(s)
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  8. #8
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I thought val was no more to be used in VB.NET. What about
    You could have easily tested what you said. 'Val' is still present in .NET and i dont know if it is for backward compatibility or not, however Integer.Parse still produces error if the string contains anything beside numbers.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  9. #9
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    here's how to sort the integers out from the text in the string
    Code:
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String = "1234abc567xyz"
            Dim excluded As String
            Dim i As Integer
            Dim l As Integer
    
            For i = 0 To str.Length - 1
                If IsNumeric(str.Chars(i)) Then
                    l = l & str.Chars(i)
                Else
                    excluded = excluded & str.Chars(i)
                End If
    
            Next
    
            MessageBox.Show("numbers:" & l & Chr(10) & "letters:" & excluded)
    
        End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  10. #10
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651
    Lunatic3

    I didn't mean tp upset you. I was just wondering about the usage of Val() as it kind of doesn;t fit in with the new methods in .NET where most of the conversion methods are on objects Object.Method(). I would think its for backward compatibility only and Integer.Parse("2223") should be used instead. You can still put this in a Try block to catch erros.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Val() is math function . Parse() is string manipulation function .

  12. #12
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I didn't mean tp upset you.
    I didnt get upset at all mate

    As you said 'val' does not fit into .NET way of thinking and it actually belongs to Microsoft.VisualBasic namespace. Still i found it easier and safer than trying to catch the error. Imagine you catch the error, then what you want to do with it? Are you going to apply a method to retrive the number out of that string? if so then you may use Val .
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  13. #13
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by Pirate
    Val() is math function . Parse() is string manipulation function .
    val() is a math function LOCATED in the visualbasic namespace..so it needs vb backwards dll's
    \m/\m/

  14. #14
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by PT Exorcist
    val() is a math function LOCATED in the visualbasic namespace..so it needs vb backwards dll's
    Still supported in VB.NET 2003 and I bet it will be for some ahead time .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width