|
-
May 29th, 2009, 09:31 AM
#1
Thread Starter
New Member
Visual Basic String Shifting
Instead of StrReverse(txtString.Text) which would make 12345678 look like 87654321. I'm wanting to make it so that 12345678 would look like 78563412. Any ideas?
-
May 29th, 2009, 09:38 AM
#2
Re: Visual Basic String Shifting
Something like this:
Code:
Dim str As String
Dim newStr As String
Dim i As Long
str = "12345678"
For i = 1 To Len(str) Step 2
newStr = Mid$(str, i, 2) & newStr
Next
MsgBox newStr
-
May 29th, 2009, 09:41 AM
#3
Re: Visual Basic String Shifting
Welcome to the forums.
Here is a short example. It will give you some ideas and will work only if the number of characters are even vs odd.
Code:
Private Sub Command1_Click()
Dim sData As String, sOut As String, I As Long
sData = "12345678"
sOut = String$(Len(sData), vbNullChar) ' build a buffer for swapped data
For I = 1 To Len(sData) Step 2
Mid$(sOut, I, 2) = Mid$(sData, Len(sData) - I, 2)
Next
MsgBox sOut
End Sub
I see Joacim beat me to it
-
May 29th, 2009, 10:55 AM
#4
Re: Visual Basic String Shifting
I present to you: The One Liner
MsgBox StrConv(StrReverse(StrConv("12345678", vbFromUnicode)), vbUnicode)
One Line To Rule Them All.
Disclaimer: for ANSI compatible characters only, the only perfectly safe characters are from the ASCII range, 0 - 127
-
May 29th, 2009, 11:00 AM
#5
Re: Visual Basic String Shifting
 Originally Posted by Merri
I present to you: The One Liner
MsgBox StrConv(StrReverse(StrConv("12345678", vbFromUnicode)), vbUnicode)
One Line To Rule Them All.
Disclaimer: for ANSI compatible characters only, the only perfectly safe characters are from the ASCII range, 0 - 127
Clever
-
May 29th, 2009, 07:18 PM
#6
Thread Starter
New Member
Re: Visual Basic String Shifting
-
May 29th, 2009, 07:25 PM
#7
Re: Visual Basic String Shifting
I see no problem, the characters are the regular ones from the ASCII range: "0" = 48 ... "9" = 57, "A" = 65 ... "F" = 70 as character codes. You're not dealing with anything more magical than that. You're dealing with a String here, not with a numeric value (such as a Long).
However, if you're only dealing with hex strings and you're filling them from somewhere that are actual values (ie. you do Hex.Text = Right$("0000000" & Hex$(lngValue)) at some point or similar) it might be better to swap around the actual values.
As for why the suggestion I made works... "00F80001", as a String, takes two bytes per character. As a hex representation it looks like this (separating each hex pair with |):
30 00 30 00 | 46 00 38 00 | 30 00 30 00 | 30 00 31 00
StrConv + vbFromUnicode does this:
30 30 | 46 38 | 30 30 | 30 31
StrReverse does this:
30 31 | 30 30 | 46 38 | 30 30
StrConv + vbUnicode does this:
30 00 31 00 | 30 00 30 00 | 46 00 38 00 | 30 00 30 00
And thus we have ended up with "0100F800"
Last edited by Merri; May 29th, 2009 at 07:33 PM.
-
May 29th, 2009, 07:51 PM
#8
Thread Starter
New Member
Re: Visual Basic String Shifting
 Originally Posted by Merri
I see no problem, the characters are the regular ones from the ASCII range: "0" = 48 ... "9" = 57, "A" = 65 ... "F" = 70 as character codes. You're not dealing with anything more magical than that. You're dealing with a String here, not with a numeric value (such as a Long).
However, if you're only dealing with hex strings and you're filling them from somewhere that are actual values (ie. you do Hex.Text = Right$("0000000" & Hex$(lngValue)) at some point or similar) it might be better to swap around the actual values.
As for why the suggestion I made works... "00F80001", as a String, takes two bytes per character. As a hex representation it looks like this (separating each hex pair with |):
30 00 30 00 | 46 00 38 00 | 30 00 30 00 | 30 00 31 00
StrConv + vbFromUnicode does this:
30 30 | 46 38 | 30 30 | 30 31
StrReverse does this:
30 31 | 30 30 | 46 38 | 30 30
StrConv + vbUnicode does this:
30 00 31 00 | 30 00 30 00 | 46 00 38 00 | 30 00 30 00
And thus we have ended up with "0100F800"
Actually I found a simpler solution, by looking in my code. I beat myself up after looking at it lol. Couldn't believe I missed it.
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
|