String corrupted

Hi, I have a strange problem that looks to me like a bug. If you have this code:

dim s as string
s = chr(&ha9)+chr(&h09)
s = s + “”

set a breakpoint at the last line and launch. In the debugger you will find that s contains C2 A9 09

Where does that C2 byte come from?

Any hints?


I believe c2a9 is the UTF-8 encoding for the copyright symbol.

You are asking to append the Unicode code points 169 and 9 to your string. In UTF-8, any code points greater than 127 will be represented by 2 or more bytes.

When you look at the code point 169 as bits, you get:


We can break that up as:

000010 101001

UTF-8 will add bits to indicate leading and continuation bytes, and the leading byte will tell it how many bytes make up the character. The leading byte in this case will start with “110” (two "1"s indicate two bytes in total) and the continuation bytes will always start with “10”. So when we add these bits to the code point, we get:

11000010 10101001

In hex, these values are:

C2 A9

Now if you truly just want the bytes A9 09 in your string, do it this way:

s = ChrB( &hA9 )  + ChrB( &h09 )

Thanks Kem, chrB is the solution for my problem.

BTW, in RealStudio 2011r4 this does not happen.

They changed the behavior of Chr at some point to return UTF-8 characters instead of straight bytes.

If all you needed was bytes then CHRB is the right thing to use