String to ByteArray

Just starting out? Need help? Post your questions and find answers here.
Mr52
User
User
Posts: 12
Joined: Tue Jun 21, 2011 3:59 pm

String to ByteArray

Post by Mr52 »

Well in VB we can change the String into Bytearray very easily like
Dim A() as Byte
A()=StrConv("String",vbUnicode)

But how to do it in PB ?

I have a lot of trouble with this

Tried MultiByteToWideChar But need to String into ByteArray Please help me
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: String to ByteArray

Post by c4s »

If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Mr52
User
User
Posts: 12
Joined: Tue Jun 21, 2011 3:59 pm

Re: String to ByteArray

Post by Mr52 »

Thank You c4s :) Have seen that before well dont know but this is function im trying to Code in PB

Code: Select all

Public Function X(ByVal T As String, p As String) As String
    On Error Resume Next
    Dim s(0 To 255) As Integer
    Dim i, B, c As Integer
    Dim L() As Byte
    Dim R() As Byte
    Dim D As Byte
    R() = StrConv(T, vbFromUnicode)
    L() = StrConv(p, vbFromUnicode)
    For i = 0 To 255
        s(i) = i
    Next i
    For i = 0 To 255
        D = s(i)
        s(i) = s((B + s(i) + L(i Mod Len(p))) Mod 256)
        s((B + s(i) + L(i Mod Len(p))) Mod 256) = D
    Next i
    B = 0
    For i = 0 To UBound(R)
        c = (c + s((B + 1) Mod 256)) Mod 256
        s((B + 1) Mod 256) = s(c)
        s(c) = s((B + 1) Mod 256)
        R(i) = R(i) Xor (s((s((B + 1) Mod 256) + s(c)) Mod 256))
    Next i
    X = StrConv(R(), vbUnicode)
End Function
Want to convert to PB

This is wat i have done

Code: Select all

Structure ByteArray1
  R.b[0]
EndStructure
Structure ByteArray2
  L.b[0]
EndStructure
Procedure.s X(T.s, p.s)
    ;On Error Resume Next
    Dim s.i(255)
    i.i
    B.i
    c.i
    
    D.b
    ;R() = StrConv(T, vbFromUnicode)
    ;L() = StrConv(p, vbFromUnicode)
    *MemoryR.ByteArray1 = @T
    *MemoryID.ByteArray2 = @p
    
    For i = 0 To 255
        s(i) = i
    Next i
    For i = 0 To 255
        D = s(i)
        s(i) = s((B + s(i) + *MemoryID\L[i % Len(p)]) % 256)
        s((B + s(i) + *MemoryID\L[i % Len(p)]) % 256) = D
    Next i
    B = 0
    For i = 0 To SizeOf(*MemoryR\R)
        c = (c + s((B + 1) % 256)) % 256
        s((B + 1) % 256) = s(c)
        s(c) = s((B + 1) % 256)
        *MemoryR\R[i] = *MemoryR\R[i] XOr (s((s((B + 1) % 256) + s(c)) % 256))
    Next i
    ProcedureReturn T
  EndProcedure
  


Debug X("d","d")


It says out of bounds etc
Can any one correct this please
im sure im doing a lot of mistakes
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: String to ByteArray

Post by infratec »

Hi,

again strange stuff in your original code:
In my opinion B is always 0.

But here is a working code:

Code: Select all

Structure ByteArrayStr
  b.b[0]
EndStructure


Procedure.s X(T.s, p.s)
  
  Dim s.u(255)
  Define i.i
  Define B.i
  Define c.i
  
  Define D.b
  ;R() = StrConv(T, vbFromUnicode)
  ;L() = StrConv(p, vbFromUnicode)
  *MemoryR.ByteArrayStr = @T
  *MemoryL.ByteArrayStr = @p
  
  For i = 0 To 255
    s(i) = i
  Next i
  
  For i = 0 To 255
    D = s(i)
    s(i) = s((B + s(i) + *MemoryL\b[i % Len(p)]) % 256)
    Debug s(i)
    Debug (B + s(i) + *MemoryL\b[i % Len(p)]) % 256
    s((B + s(i) + *MemoryL\b[i % Len(p)]) % 256) = D
  Next i
      
  B = 0
  Help = Len(T)
  For i = 0 To Help
    c = (c + s((B + 1) % 256)) % 256
    s((B + 1) % 256) = s(c)
    s(c) = s((B + 1) % 256)
    *MemoryR\b[i] = *MemoryR\b[i] ! (s((s((B + 1) % 256) + s(c)) % 256))
  Next i
  
  ProcedureReturn T
  
EndProcedure
  
Debug X("d","d")
S went to a negative value.
So I have changed it from .i to .u
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: String to ByteArray

Post by infratec »

Hi,

I changed the above code.
The sizeof() was wrong in the last for loop.
Mr52
User
User
Posts: 12
Joined: Tue Jun 21, 2011 3:59 pm

Re: String to ByteArray

Post by Mr52 »

whoa :o that was fast thanks :d Works
thanks again
Post Reply