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

  • Home
  • SEARCH
  • 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 6353029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:20:20+00:00 2026-05-24T22:20:20+00:00

I am trying to convert some vb6 code to c# and I am struggling

  • 0

I am trying to convert some vb6 code to c# and I am struggling a bit.
I have looked at this page below and others similar, but am still stumped.

Why use hex?

vb6 code below:

    Dim Cal As String
    Cal = vbNull

    For i = 1 To 8
        Cal = Cal + Hex(Xor1 Xor Xor2)
    Next i

This is my c# code – it still has some errors.

        string Cal = null;
        int Xor1 = 0;
        int Xor2 = 0;

        for (i = 1; i <= 8; i++)
            {
            Cal = Cal + Convert.Hex(Xor1 ^ Xor2);
            }

The errors are:

        Cal = Cal + Convert.Hex(Xor1 ^ Xor2 ^ 6);

Any advice as to why I cant get the hex to convert would be appreciated.
I suspect its my lack of understanding the .Hex on line 3 above and the “&H” on line 1/2 above.

  • 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-05-24T22:20:20+00:00Added an answer on May 24, 2026 at 10:20 pm

    Note: This answer was written at a point where the lines Xor1 = CDec("&H" + Mid(SN1, i, 1))
    and Xor1 = Convert.ToDecimal("&H" + SN1.Substring(i, 1)); were still present in the question.


    What’s the &H?

    In Visual Basic (old VB6 and also VB.NET), hexadecimal constants can be used by prefixing them with &H. E.g., myValue = &H20 would assign the value 32 to the variable myValue. Due to this convention, the conversion functions of VB6 also accepted this notation. For example, CInt("20") returned the integer 20, and CInt("&H20") returned the integer 32.

    Your code example uses CDec to convert the value to the data type Decimal (actually, to the Decimal subtype of Variant) and then assigns the result to an integer, causing an implicit conversion. This is actually not necessary, using CInt would be correct. Apparently, the VB6 code was written by someone who did not understand that (a) the Decimal data type and (b) representing a number in decimal notation are two completely different things.


    So, how do I convert between strings in hexadecimal notation and number data types in C#?

    To convert a hexadecimal string into a number use

    int number = Convert.ToInt32(hex, 16);   // use this instead of Convert.ToDecimal
    

    In C#, there’s no need to pad the value with “&H” in the beginning. The second parameter,16, tells the conversion function that the value is in base 16 (i.e., hexadecimal).

    On the other hand, to convert a number into its hex representation, use

    string hex = number.ToString("X");       // use this instead of Convert.ToHex
    

    What you are using, Convert.ToDecimal, does something completely different: It converts a value into the decimal data type, which is a special data type used for floating-point numbers with decimal precision. That’s not what you need. Your other method, Convert.Hex simply does not exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert some code that worked great in VB, but I can't
I'm trying to convert old QuickTime framework code to the 64-bit Cocoa-based QTKit on
I'm trying to convert some strings that are in French Canadian and basically, I'd
I'm trying to convert some Xaml to HTML using the .NET XslCompiledTransform and am
I am trying to convert some of my stored procedures to Linq and am
I've been trying to convert SVG images to PNG using C#, without having to
I am trying to convert an ASP.NET website into a web application project. The
I am trying to convert an int into three bytes representing that int (big
I'm trying to convert an incoming sting of 1s and 0s from stdin into
I am trying to convert an access datetime field to a mysdl format, using

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.