How To Convert A String To Byte()?

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 there one?

Here is documentation on what Java’s getBytes() does: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#getBytes()

I’m porting an old java app to Xojo, and need to make sure I’m doing this right.

Thanks!

dim s as string = “Hello”
dim m as memoryblock = s

maybe this?

Ah- thanks @Christian Schmitz - a memoryblock will do nicely enough.

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

Thanks.

What do you want to do?

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.

Oh … so the it’s probably wrong :frowning: Should I be using UTF8 instead? It’s an update of a VB6 project from 2001 so I’m guessing they only had UTF8 back then :slight_smile:

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

I have no idea why you need an array of byte in the first place.

Because all the encryption that’s being done is using Bitwise.

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

there you have some code.

Bitwise doesn’t require a byte array but …

a memoryblock can be addressed as if it were an array of bytes as well

[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

TIA

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

So if I was using a memoryblock would it be like what I’ve written at the end?

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

Something like that