|
-
May 27th, 2011, 12:41 PM
#1
Thread Starter
New Member
[RESOLVED] How can I manipulate a string to byte swipe?
I am trying to write a code that will byte swipe data in a string.
The conversion should take a 16 characters string, and swipe the first to characters with the last two, then characters three and four with character thirteen and fourteen and so on, in this format –
if the input string is
A1B2C3D4E5F6G7H8
then the converted sting will be
H8G7F6E5D4C3B2A1.
I run into problem using:
left(mystring – 2)
and
right(mystring -2)
because the string to manipulate is an input of a textbox so I fail when I try to use cross data with a StringHandling class, and I can’t figure out how to incorporate StringHandling into my form class (sorry if this is a newbie question) and without it when I try something like
Dim byte1 As String = Left(MyString, 14)I get an
error saying - ‘Public ReadOnly Property Left As Integer’ has no parameters and its return type cannot be indexed.
I am using VB.net 2010.
any help will be appriciated
thank you.
-
May 27th, 2011, 01:29 PM
#2
Re: How can I manipulate a string to byte swipe?
Left is an old VB6- function... use the String.Substring method instead.
-tg
-
May 27th, 2011, 02:00 PM
#3
Thread Starter
New Member
Re: How can I manipulate a string to byte swipe?
techgnome, thank you very much. it make sense now 
my final code was
Code:
Dim toConvert As String = TextBox1.Text
If toConvert.Length <> 16 Then
MessageBox.Show("Length must be 16 characters long")
Exit Sub
Else
Dim Byte1 As String = toConvert.Substring(14, 2)
Dim Byte2 As String = toConvert.Substring(12, 2)
Dim Byte3 As String = toConvert.Substring(10, 2)
Dim Byte4 As String = toConvert.Substring(8, 2)
Dim Byte5 As String = toConvert.Substring(6, 2)
Dim Byte6 As String = toConvert.Substring(4, 2)
Dim Byte7 As String = toConvert.Substring(2, 2)
Dim byte8 As String = toConvert.Substring(0, 2)
TextBox2.Text = Byte1 & Byte2 & Byte3 & Byte4 & Byte5 & Byte6 & Byte7 & byte8
End If
works like a charm
-
May 27th, 2011, 02:09 PM
#4
Re: [RESOLVED] How can I manipulate a string to byte swipe?
As TG pointed out, Left is a holdover from VB6. The error you are getting is because Left is a property of all controls, so unless you specify the correct namespace, the compiler thinks you are accessing the Left property rather than calling the Left function. You could fix that by fully decorating the name with the namespace where the Left method is found, but you shouldn't. Instead, you should switch to .NET and use the .Substring method.
Another point you should note is that you can't really swap characters, so you might as well not think about it that way. Whenever you manipulate a string you are throwing out the old string and creating a new one. It may not look that way, but that's the way to think about it. For example, if you were to concatenate two strings together:
Code:
Dim myString As String = "Blue"
myString &= "Red"
You end up with a single variable called myString, and it holds "BlueRed", but what happend was that you created a string with the value "Blue", then created a new string with the value "BlueRed", and replaced the original "Blue" string. So what happend to the "Blue" string? It is no longer accessible, and once the garbage collector runs it will be recovered. I should add that strings are an odd object in .NET, so this may not be quite correct, but it is close enough for a mental model.
The point is that you won't be swapping characters, even if you think you are, so you might as well recognize that what you are really doing is creating a new string built from the old string. It may be easier to do that, too, since you could write up something like this:
Dim oldString As String = <your old string>
Dim newString as String
1 Get the last two characters from oldString
2 Concatenate them onto newString
3 Repeat until there is nothing left in oldString
If you can be certain that the original string will have an even number of characters, this is relatively easy. If you can't be certain about the original string, then it becomes slighly harder, but only moderately so.
My usual boring signature: Nothing
 
-
May 27th, 2011, 02:10 PM
#5
Re: [RESOLVED] How can I manipulate a string to byte swipe?
All that was typed while you were solving the problem. I hope it provides some useful thoughts, even after you have taken care of the situation.
By the way, what you ended up doing was unrolling the loop I described in my post. That's a good thing to do for a loop as short as the one you have, though you won't really see much of a performance benefit.
My usual boring signature: Nothing
 
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
|