User Tools

Site Tools


string_char_byte

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
string_char_byte [2018/01/24 05:24] – created wolfgangriedmannstring_char_byte [2018/08/20 04:04] (current) wolfgangriedmann
Line 1: Line 1:
-====== String, Char und Byte ======+====== String, Char and Byte ======
  
 **In .NET every string is Unicode**. **In .NET every string is Unicode**.
Line 10: Line 10:
 That was possible in VO or other Win32 languages, but it is not more correct in .NET. That was possible in VO or other Win32 languages, but it is not more correct in .NET.
 In .NET, if reading binary data, you need to use variables defined as ''byte[]'', that means .NET array of bytes: In .NET, if reading binary data, you need to use variables defined as ''byte[]'', that means .NET array of bytes:
- +<code visualfoxpro>local aBitmap as byte[]
-<code>local aBitmap as byte[]+
  
 aBitmap := File.ReadAllBytes( "c:\data\MyImage.png" )</code> aBitmap := File.ReadAllBytes( "c:\data\MyImage.png" )</code>
  
 If you are reading text data, you can read it into a string: If you are reading text data, you can read it into a string:
- +<code visualfoxpro>local cBuffer as string
-<code>local cBuffer as string+
  
 cBuffer := File.ReadAllText( "c:\data\MyText.txt", Encoding.ASCII )</code> cBuffer := File.ReadAllText( "c:\data\MyText.txt", Encoding.ASCII )</code>
Line 23: Line 21:
 Of course, you can convert for and back between strings and character arrays, but you need to specify the ''Encoding''. In some functions, you can also omit it, but then the .NET framework will use the system defined values, and this can lead to unexpected results. Of course, you can convert for and back between strings and character arrays, but you need to specify the ''Encoding''. In some functions, you can also omit it, but then the .NET framework will use the system defined values, and this can lead to unexpected results.
  
 +To convert byte arrays to string, there are conversion functions. Please look at the Encoding class in the System.Text namespace in the .NET framework documentation.
 +<code visualfoxpro>local aBytes as byte[]
 +local cString as string
 +
 +System.Text.Encoding.Unicode.GetString( aBytes )</code>
 +
 +The inverse process is also possible:
 +<code visualfoxpro>local aBytes as byte[]
 +local cString as string
 +
 +aBytes := System.Text.Encoding.Unicode:GetBytes( cString )</code>
 +
 +An array of char values can also be used instead of a string.
 +
 +To build a string from a char array is very simple, you can use the constructor of the string class:
 +<code visualfoxpro>local aChars as char[]
 +local cString as string
 +
 +cString := String{ aChars }</code>
 +
 +For the inverse operation (from a string to a char array) you can use a method of the string class:
 +<code visualfoxpro>local cString as string
 +local aChars as char[]
 +
 +aChars := cString:ToCharArray()</code>
  
 +If you need to initialize a byte array (as every array), you need to write as follows:
 +<code visualfoxpro>local aBuffer as byte[]
  
 +aBuffer := Byte[]{ 128 }</code>
string_char_byte.1516771457.txt.gz · Last modified: 2018/01/24 05:24 by wolfgangriedmann