Results 1 to 7 of 7

Thread: Xor ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833

    Xor ?

    I have a unicode string high order first chr(0) & "A"

    but my program wants it to be the other way around "A" & chr(0)

    I was told XOR could do it but ?????? if I know


    where % is really NULL chr(0)


    str1 = "A%B%C%"

    str1 = xor str1

    str1 = "%A%B%C"


    ????????????
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Where did the unicode string chr(0) & "A" come from?

    How are you storing the data, in a string?
    Sounds odd to me.

    If you already have the bytes:

    "%A%B%C"

    then delete the first byte and add that byte to the end to get:

    "A%B%C%"

    Now you have a normal unicode string for VB to work with.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833
    the data is comming into the winsock control from in an "XML" format.

    I don't want the unicode at all. no need. when it comes in I want to convert it to a string

    when it goes out I want to convert it back.


    the problem is the byte order, why is thing differnet from VB !
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  4. #4
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Well once you have recived all the bytes, I assume into a byte array, all you have to do is remove the first element of the byte array and add a single byte to the end of the byte array.

    Like this:
    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, source As Any, ByVal numBytes As Long)
    
    Sub DeleteArrayItemByte(arr() As Byte, index As Long)
        If index < UBound(arr) Then
            CopyMemory arr(index), arr(index + 1), (UBound(arr) - index) * LenB(arr(index))
        End If
        arr(UBound(arr)) = Empty
    End Sub
    
    Private Sub Command1_Click()
     Dim ba() As Byte: ReDim ba(13)
     Dim i&
     ba(0) = 0
     ba(1) = 78
     ba(2) = 0
     ba(3) = 85
     ba(4) = 0
     ba(5) = 67
     ba(6) = 0
     ba(7) = 76
     ba(8) = 0
     ba(9) = 69
     ba(10) = 0
     ba(11) = 85
     ba(12) = 0
     ba(13) = 83
     
    Call DeleteArrayItemByte(ba, 0) ' Get rid of leading Byte FAST using copy memory ;)
    
    For i = 0 To UBound(ba)
        Debug.Print ba(i)
    Next
    
    MsgBox ba
    
    End Sub

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    AFAIK the Xor is to swap the two bytes around, something like:
    VB Code:
    1. Dim it(0 to 1) as Byte
    2.  
    3. it(0) = it(0) Xor it(1)
    4. it(1) = it(1) Xor it(0)
    5. it(0) = it(0) Xor it(1)
    I dunno, can't really remember
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    You need to convert back again too, well here is code to do that too, enjoy. Should be a fast way of doing it too especially if they are large byte arrays :

    Code:
    Option Explicit
    
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, source As Any, ByVal numBytes As Long)
    
    Sub InsertArrayItemByte(arr() As Byte, index As Long, newValue As Byte)
        If index < UBound(arr) Then
            ReDim Preserve arr(UBound(arr) + 1)
            CopyMemory arr(index + 1), arr(index), (UBound(arr) - index - 1) * LenB(arr(index))
        End If
        arr(index) = newValue
    End Sub
    Sub DeleteArrayItemByte(arr() As Byte, index As Long)
        If index < UBound(arr) Then
            CopyMemory arr(index), arr(index + 1), (UBound(arr) - index) * LenB(arr(index))
        End If
        arr(UBound(arr)) = Empty
    End Sub
    
    Private Sub Command1_Click()
     Dim ba() As Byte: ReDim ba(13)
     Dim i&
     ba(0) = 0
     ba(1) = 78
     ba(2) = 0
     ba(3) = 85
     ba(4) = 0
     ba(5) = 67
     ba(6) = 0
     ba(7) = 76
     ba(8) = 0
     ba(9) = 69
     ba(10) = 0
     ba(11) = 85
     ba(12) = 0
     ba(13) = 83
     
    Call DeleteArrayItemByte(ba, 0) ' Get rid of leading Byte FAST using copy memory ;)
    
    For i = 0 To UBound(ba)
        Debug.Print "convert To vb friendly", ba(i)
    Next
    
    MsgBox ba
    
    'now manipulate the String with VB here
    
    Call InsertArrayItemByte(ba, 0, 0) ' convert back To original format
    
    For i = 0 To UBound(ba)
        Debug.Print "convert back To xml", ba(i)
    Next
    
    End Sub
    Last edited by Nucleus; Jun 15th, 2001 at 08:04 AM.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833
    OK i'll give it a go...

    any idea why everything is opposite, what is the format??

    count I just do it like this?

    str1 = "%A%B%c"

    str1 = mid(str1,2) & "%"

    where %A = null
    Kurt Simons
    [I know I'm a hack but my clients don't!]

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