Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8257893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:21:21+00:00 2026-06-08T02:21:21+00:00

So I am migrating some code from VB.NET to C# however it fails when

  • 0

So I am migrating some code from VB.NET to C# however it fails when it is doing a byte parse in C#.

here is the VB.NET code would works:

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

The C# version:

string modAccountNumberAsciiHex = "FC13";
byte iByte;
string strByte = string.Empty;

for (int iIdx = 1; iIdx <= 3; iIdx += 2)
{
    iByte = byte.Parse(("&H" + modAccountNumberAsciiHex.Substring((iIdx - 1), 2)));
    if (iByte >= 120 && iByte <= 127)
    {
        iByte = iByte |= 0x80;
        strByte = BitConverter.ToString(new byte[] { iByte });
        while (strByte.Length < 2)
        {
            strByte = "0" + strByte;
        }

        // TODO: convert the line below to C#   
        // Mid$(sModifiedAccountNumber_AsciiHex, iIdx, 2) = strByte

    }
}

so in C# I always get a FormatException when doing the byte.Parse (line straight after the for statement)

Any thoughts on what this should be in C#?

In addition – the C# version in the TODO comment would also be appreciated 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:21:23+00:00Added an answer on June 8, 2026 at 2:21 am

    The mistake is including the “&H” at the start of the string, and using byte.Parse without specifying NumberStyles.AllowHexSpecifier. It would be simpler to use Convert.ToByte though:

     byte x = Convert.ToByte(modAccountNumberAsciiHex.Substring(iIdx - 1, 2), 16)
    

    Also note that your code is currently very “1-based”. It feels like ported VB. More idiomatic C# would be:

    for (int index = 0; index < 3; index += 2)
    {
        byte x = Convert.ToByte(text.Substring(index, 2), 16);
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm migrating some code from VB.NET to C# (3.5). I find structures like: Public
I am migrating some C++ code from structures to classes. I was using structures
I'm migrating some code over to GNU trove for performance reasons. However, I do
I'm migrating some code from the ASPX view engine to Razor and I've run
I am migrating C# code from using a NetworkStream to SSLStream, however where I
I'm migrating data from an old laptop to a new laptop, including some vb.net
We are currently migrating our VB6 application to Net. The VB6 code uses some
I'm migrating some of our code from LINQ-to-SQL to Entity Framework. Previously, when run
I'm migrating some code from using XmlDocument to using XDocument . As part of
I am migrating some spreadsheets from Excel 2003/WinXP to Excel 2010/Win7. Some spreadsheets use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.