|
-
Jul 21st, 2012, 09:55 AM
#1
Thread Starter
PowerPoster
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)));
-
Jul 21st, 2012, 10:09 AM
#2
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.
-
Jul 21st, 2012, 10:13 AM
#3
Re: VB.NET conversion
Try omitting the "&H" as jmcilhinney suggests and add NumberStyle:
CSharp Code:
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)
-
Jul 21st, 2012, 10:19 AM
#4
Thread Starter
PowerPoster
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);
-
Jul 21st, 2012, 10:21 AM
#5
Thread Starter
PowerPoster
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
-
Jul 21st, 2012, 10:35 AM
#6
Re: VB.NET conversion
 Originally Posted by Techno
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.
-
Jul 21st, 2012, 10:46 AM
#7
Thread Starter
PowerPoster
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);
-
Jul 21st, 2012, 10:49 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|