Results 1 to 8 of 8

Thread: VB.NET conversion

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    VB.NET conversion

    So, this code works fine in VB.NET:

    Dim sModifiedAccountNumber_AsciiHex
    Dim iByte As Byte = 0
    Dim iIdx As Integer = 0
    Dim strByte As String = String.Empty

    sModifiedAccountNumber_AsciiHex = "FC13"
    For iIdx = 1 To 3 Step 2

    iByte = CByte("&H" & Mid$(sModifiedAccountNumber_AsciiHex, iIdx, 2))
    If iByte >= 120 And iByte <= 127 Then
    iByte = iByte Or &H80
    strByte = Hex$(iByte)
    Do While Len(strByte) < 2
    strByte = "0" & strByte
    Loop
    Mid$(sModifiedAccountNumber_AsciiHex, iIdx, 2) = strByte
    End If

    Next

    but, how would I convert this to C#?
    I mean, I have majority of it however this always results in a format exception being thrown:

    ..
    ..
    for (int iIdx = 1; iIdx <= 3; iIdx += 2)
    {
    iByte = byte.Parse(("&H" + modAccountNumberAsciiHex.Substring((iIdx - 1), 2)));

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.NET conversion

    That's horrible VB.NET code and I'm guessing that it was written in VB6 originally. "&H" should never appear in a String in VB.NET. It can work but it's dodgy. It's absolutely useless in C# though, because &H is the VB-specific prefix for hexadecimal literals. The equivalent in C# is 0x, but that shouldn't appear in a String either. If you want to convert a hexadecimal String to a Byte then you can use Byte.Parse and use NumberStyles.HexNumber or else Convert.ToByte and specify the base as 16. In both cases, DO NOT add any prefix to the String.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: VB.NET conversion

    Try omitting the "&H" as jmcilhinney suggests and add NumberStyle:
    CSharp Code:
    1. byte.Parse(s, NumberStyles.AllowHexSpecifier);

    Regards Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: VB.NET conversion

    I agree jmcilhinney you are right, it IS VB6 code....

    so now this works in C#:


    iByte = byte.Parse(modAccountNumberAsciiHex.Substring((iIdx - 1), 2), System.Globalization.NumberStyles.HexNumber);

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: VB.NET conversion

    last one:

    I know Mid$ is basically substring but how can I do this in C#?

    Code:
                    Mid$(sModifiedAccountNumber_AsciiHex, iIdx, 2) = strByte

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.NET conversion

    Quote Originally Posted by Techno View Post
    last one:

    I know Mid$ is basically substring but how can I do this in C#?

    Code:
                    Mid$(sModifiedAccountNumber_AsciiHex, iIdx, 2) = strByte
    That version of Mid is NOT basically Substring, because it is setting, not getting. You basically need to break up your String into three pieces, using Substring, and then create a new String from the first part and the third part and the new bit in between.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: VB.NET conversion

    right. sorry, I should have been clearer. What i meant is that the function Mid$ in VB is SubString on .NET. but the actual function/logic you are saying correctly is replacing/setting the variable value.

    what about a replace? would that be ok?

    C#:


    modAccountNumberAsciiHex = modAccountNumberAsciiHex.Replace(modAccountNumberAsciiHex.Substring((index), 2), strByte);

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: VB.NET conversion

    Depends on the contents. If the substring returns "xx" and that string is found elsewhere, you will have both replaced. You could ofc make sure that this will never happen by inserting some sort of signalstring at the location of the 2-digit hex-string.

    #EDIT: In your example strings like "5f5f" could cause problems using your code.
    Last edited by ThomasJohnsen; Jul 21st, 2012 at 10:55 AM.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

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