In Java, the String class has a getBytes() method, which simply returns an array of bytes using the default platform encoding. Perhaps I’m blind, but I can’t seem to find any equivalent built-in way to do this in Xojo.
Is this the Xojo equivalent?
VB
Dim b() As Byte
b = System.Text.UnicodeEncoding.Unicode.GetBytes(txtPlain.Text)
Xojo
Dim mb As memoryblock = DefineEncoding(txtPlain.Text, Encodings.UTF16)
Dim b() As Byte
For x As Integer = 0 to mb.Size - 1
b.Append mb.Byte(x)
Next
Because here you get UTF16 and get each byte on it’s own in an array.
But as UTF16 uses 16 bit per character, this splits each character into two bytes.
I’d guess they only used single byte encodings but would not stake my life on that
WAIT … thats UTF16 that the VB gets so the xojo code should be about the same
but why pass a BYTE() ?
converting to a BYTE() and then back someplace else is a pile of work and really may be unnecessary unless you’re just doing a rote “port” of code
dim s as string = "Hello World"
s = ConvertEncoding(s, encodings.UTF8)
dim m as MemoryBlock = s
dim u as integer = m.Size-1
dim b() as UInt8
for i as integer = 0 to u
b.Append m.UInt8Value(i)
next
Break
[quote=293174:@Norman Palardy]Bitwise doesn’t require a byte array but …
a memoryblock can be addressed as if it were an array of bytes as well[/quote]
Ok so if I’m attempting to change the code to use MB instead of converting MB to byte() I’m not really understanding what I’m using in and passing to functions. For example
Function x (ByRef a As Byte)
where the code may be sending ByteArray(3) to the function and I leaving the Function alone and passing mb.byte(3) or am I changing it to
Function x (a As MemoryBlock, i as Integer)
In the function
Dim b as Byte
If (a AND b) <> 0 then
//In the case of change of function does that become
If (a.byte(i) AND b) <> 0 then
byref ?
if your original takes one byte byref like
Function x (ByRef a As Byte)
passing an individual byte from an array of bytes wont work (it also wont work to pass a single byte from a memory block byref either)
So if you currently use it like
Function x (ByRef a As Byte)
End Function
dim bytes() as Byte
for i as integer = 0 to bytes,ubound()
x(bytes(i)) // <<<<<<<<<<<<<<< this wont work with BYTE()
next
You’d need to change it to something like
Function x (a As Byte) as Byte
End Function
dim bytes() as Byte
for i as integer = 0 to bytes,ubound()
bytes(i) = x(bytes(i)) // <<<<<<<<<<<<<<<
next
This is why when you’ve brought up this specific algorithm in previous posts I’ve sad that a “port” from whatever language it is I assume VB) to Xojo may not work since they have difference capabilities
You need to port the algorithm - not the specific code