help bit.StringToBytes

alessandro

Member
Licensed User
Hi,

help me


TextBox1="a" (ascii value 97)
buffer()=bit.StringToBytes(TextBox1.Text,0,1)
label1.Text=buffer(0)
label1=97 'OK

TextBox1="Ü" (ascii value 220)
buffer()=bit.StringToBytes(TextBox1.Text,0,1)
label1.Text=buffer(0)
label1=195 'error

why
 

agraham

Expert
Licensed User
Longtime User
.NET, and therefore Basic4ppc, uses UTF-16 characters which are coded differently to ASCII.

The US-ASCII standard only encodes characters 0 to 127. I suspect you are referring to a Windows code page. Windows-1252. Code pages were used to allow only 256 character codes to represent more than that number of characters. Code page

StringToBytes actually changes a string of UTF16 characters to a set of bytes representing a given encoding. The default encoding if you use New1 is UTF-8 where Unicode characters are of variable length between 1 and 4. In your case the UTF8 encoding of "Ü" is two bytes long as you can see if you do

B4X:
For i = 0 To ArrayLen(buffer()) - 1
   str = str & buffer(i) & " "
Next
label1.text = str

Bitwise.New2 allows you to specify a codepage where the conversion is to the single byte characters of that codepage. You will get the result you expect if you specify bit.New2(1252). If you try to convert to a codepage and the Unicode character you are specifying doesn't exist in that codepage it will be replaced by "?".

Unicode charts can be found here Code Charts - Scripts
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…