Results 1 to 8 of 8

Thread: Visual Basic String Shifting

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    15

    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?

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Visual Basic String Shifting

    Quote Originally Posted by Merri View Post
    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    15

    Re: Visual Basic String Shifting

    Thanks guys!

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    15

    Re: Visual Basic String Shifting

    Quote Originally Posted by Merri View Post
    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
  •  



Click Here to Expand Forum to Full Width