# How To Convert A String To Byte()?

**URL:** <https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636>\
**Category:** General\
**Created:** [October 14, 2013, 6:29pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636 "2013-10-14T18:29:54Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kimball\_Larsen](https://forum.xojo.com/user_avatar/forum.xojo.com/kimball_larsen/32/474_2.png) [@Kimball\_Larsen](https://forum.xojo.com/u/Kimball_Larsen)\
**Post date:** [October 14, 2013, 6:29pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/1 "2013-10-14T18:29:54Z")

</div>

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()](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!

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [October 14, 2013, 6:31pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/2 "2013-10-14T18:31:05Z")

</div>

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

maybe this?

---

<div class="post-metadata">

**Author:** ![Kimball\_Larsen](https://forum.xojo.com/user_avatar/forum.xojo.com/kimball_larsen/32/474_2.png) [@Kimball\_Larsen](https://forum.xojo.com/u/Kimball_Larsen)\
**Post date:** [October 14, 2013, 6:33pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/3 "2013-10-14T18:33:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jym\_Morton](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/ecd19e/32.png) [@Jym\_Morton](https://forum.xojo.com/u/Jym_Morton)\
**Post date:** [October 18, 2016, 9:40pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/4 "2016-10-18T21:40:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [October 19, 2016, 9:23am UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/5 "2016-10-19T09:23:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jym\_Morton](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/ecd19e/32.png) [@Jym\_Morton](https://forum.xojo.com/u/Jym_Morton)\
**Post date:** [October 19, 2016, 8:15pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/6 "2016-10-19T20:15:19Z")

</div>

Oh … so the it’s probably wrong ☹ 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 🙂

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [October 19, 2016, 8:19pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/7 "2016-10-19T20:19:07Z")

</div>

> [@293162:@Jym Morton](#):
>
> Oh … so the it’s probably wrong ☹ 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 🙂

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

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [October 19, 2016, 8:19pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/8 "2016-10-19T20:19:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jym\_Morton](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/ecd19e/32.png) [@Jym\_Morton](https://forum.xojo.com/u/Jym_Morton)\
**Post date:** [October 19, 2016, 8:33pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/9 "2016-10-19T20:33:13Z")

</div>

> [@293166:@Christian Schmitz](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [October 19, 2016, 8:42pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/10 "2016-10-19T20:42:19Z")

</div>

` 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.

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [October 19, 2016, 8:45pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/11 "2016-10-19T20:45:13Z")

</div>

Bitwise doesn’t require a byte array but …

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

---

<div class="post-metadata">

**Author:** ![Jym\_Morton](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/ecd19e/32.png) [@Jym\_Morton](https://forum.xojo.com/u/Jym_Morton)\
**Post date:** [October 20, 2016, 12:19pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/12 "2016-10-20T12:19:28Z")

</div>

[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

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [October 20, 2016, 1:16pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/13 "2016-10-20T13:16:19Z")

</div>

byref ?  
if your original takes one byte byref like

```auto
  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

```auto
    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

```auto
    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

---

<div class="post-metadata">

**Author:** ![Jym\_Morton](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/ecd19e/32.png) [@Jym\_Morton](https://forum.xojo.com/u/Jym_Morton)\
**Post date:** [October 20, 2016, 1:26pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/14 "2016-10-20T13:26:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [October 20, 2016, 1:33pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/15 "2016-10-20T13:33:20Z")

</div>

Something like that

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 4:53pm UTC](https://forum.xojo.com/t/how-to-convert-a-string-to-byte/11636/16 "2020-10-29T16:53:51Z")

</div>


