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?
Printable View
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?
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
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.
I see Joacim beat me to itCode: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 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
Thanks guys!
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"