Results 1 to 11 of 11

Thread: [RESOLVED] Small C# Code to VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    10

    Resolved [RESOLVED] Small C# Code to VB6

    Hi to all,

    I just need some help converting this c# code to vb6.
    Code:
         int txtValue;
         txtValue = int.Parse(txt1.Text);
         MessageBox.Show(txtValue.ToString("X2"));
    I tried the below VB6 string to Hex function but it give a different result from the above C#.
    In C#, If I put 1 in textbox and convert, I got 01 result, but in VB6 I got 31?

    Here is the code that I got here.
    Any body can help to modify this code to give the same result as C# would be greatly appreciated. Thanks.
    Code:
    Public Function StringToHex(ByVal StrToHex As String) As String
    Dim strTemp   As String
    Dim strReturn As String
    Dim I         As Long
        For I = 1 To Len(StrToHex)
            strTemp = Hex$(Asc(Mid$(StrToHex, I, 1)))
            If Len(strTemp) = 1 Then strTemp = "0" & strTemp
            strReturn = strReturn & Space$(1) & strTemp
        Next I
        StringToHex = strReturn
    End Function

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Small C# Code to VB6

    Your code is showing the hex representation of a character "1" not the integer value 1.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Small C# Code to VB6

    Code:
        Dim Value As Byte
    
        Value = CByte(txt1.Text)
        If Value < 16 Then
            MsgBox "0" & Hex$(Value)
        Else
            MsgBox Hex$(Value)
        End If

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    10

    Re: Small C# Code to VB6

    thanks for pointing out. I will try to restructure the function.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    10

    Re: Small C# Code to VB6

    Thanks for this. Is there a way to handle big numbers? I tried to convert a value of 2100100.99 but I got an overflow error.

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,904

    Re: Small C# Code to VB6

    That’s a floating point number.
    The Cbyte is for converting byte values.
    The are other conversion methods, like CLng and CDbl

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,904

    Re: Small C# Code to VB6

    What’s the definition for converting real numbers to a hexadecimal representation??

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: Small C# Code to VB6

    Quote Originally Posted by Arnoutdv View Post
    What’s the definition for converting real numbers to a hexadecimal representation??
    VB6 doesn't have any mechanism for handling floating point hex numbers. Many C/C++ languages can handle them. Typically, they have a p# suffix where # is a power-of-two exponent applied to the number, but VB6 knows nothing about such things.

    You might search for "C" "hex" "floating point" if you want more information. I don't think I've ever seen anyone write anything in VB6 to handle any of this though, but it's not the worst of ideas.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: Small C# Code to VB6

    And another approach is to grab the Double's (or Single's) bits and put them into some type of integer via CopyMemory, and then convert that integer to hex. But that's really a kludge, as that hex number won't really represent the original float number at all. It'll just be like looking at a saved Double (or Single) in a hex editor, which typically doesn't mean much.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: Small C# Code to VB6

    I searched, and the primary page I had for a reference is gone. The only thing I found was here, but there's not much to it, and here's what it says:

    6.15 Hex Floats

    ISO C99 and ISO C++17 support floating-point numbers written not only in the usual decimal notation, such as 1.55e1, but also numbers such as 0x1.fp3 written in hexadecimal format. As a GNU extension, GCC supports this in C90 mode (except in some cases when strictly conforming) and in C++98, C++11 and C++14 modes. In that format the ‘0x’ hex introducer and the ‘p’ or ‘P’ exponent field are mandatory. The exponent is a decimal number that indicates the power of 2 by which the significant part is multiplied. Thus ‘0x1.f’ is 1 15/16, ‘p3’ multiplies it by 8, and the value of 0x1.fp3 is the same as 1.55e1.

    Unlike for floating-point numbers in the decimal notation the exponent is always required in the hexadecimal notation. Otherwise the compiler would not be able to resolve the ambiguity of, e.g., 0x1.f. This could mean 1.0f or 1.9375 since ‘f’ is also the extension for floating-point constants of type float.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    10

    Re: Small C# Code to VB6

    thanks a lot now I get the idea.

Tags for this Thread

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