#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    6
    Rep Power
    0

    Converting Char to String and String to Char for the Android platform


    Hello everyone!
    I was wondering if someone can help. I wrote the following code:

    Code:
    procedure TForm4.FormCreate(Sender: TObject);
    Var
     StringInputVariable : String;
     CharVariable : Char;
     StringOutputVariable : String;
    begin
     StringInputVariable := 'w';
    
     CharVariable := Char(StringInputVariable[1]);
    
     StringOutputVariable := String(CharVariable);
    
     Label1.Text := StringOutputVariable;
    end;
    When I Run for the Android platform I get absolutely no output in Label1 - I was expecting a 'w' in Label1.Text. The program compiles without any errors. When I compile for 32 - bit Windows I get a 'w' in Label1.Text as expected. My program basically takes a one character string input, then converts it to char, then converts it back to string and then writes this string on the screen as Label1. Why does this not work for the Android platform and how can I do workable conversions?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0
    I haven't tested on an Android to confirm, but I think the issue is that on Mobile, Strings are 0 based, while in Windows they remain 1 Based. So if you change CharVariable := Char(StringInputVariable[1]); to CharVariable := Char(StringInputVariable[0]); it should work.
    There is a way to stay consistent between all platforms if you prefer:
    {$ZEROBASEDSTRINGS ON} or {$ZEROBASEDSTRINGS OFF}

    Default
    {$ZEROBASEDSTRINGS ON} for Delphi mobile compilers
    {$ZEROBASEDSTRINGS OFF} for Delphi desktop compilers

    another option that will allow you to work on all platforms and keep each to their own default is to use Low(StringVarName) and High(StringVarName) to represent the first and last element respectively as in
    Code:
    for I := Low(StringInputVariable) to High(StringInputVariable) do
       //do something
    will work on all platforms.
    Last edited by majlumbo; October 17th, 2015 at 08:00 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0
    This problem reminds me of an assignment way back in University. The language was UBC Pascal and was part of a 5 language course that included LISP, Algol-68, SNOBOL. I was using Borland Pascal on my 8" Floppy based CP/M computer at home.
    The task was to parse network traffic (way before internet) and where I lost 5% on an assignment was the test:

    if ch in ['a'..'z', 'A'..'Z'] then begin ...

    Worked fine in an ASCII environment. Fell apart on an EBCDIC machine where characters didn't exist in a contiguous environment.
    However 0 based or 1 based strings are a compiler and language constraint, not an OS constraint. The compiler and the run time library can decide on how it serves strings up to the operating system.
    Like changing strings to ansistring and char to ansichar rather than adding a new type called widechar and widestring and thereby not breaking millions of lines of code Delphi looks more and more like it's designed by self taught high school programmers.
    IMHO
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    6
    Rep Power
    0
    Thank you so much MajLumbo and jcdammeyer for helping me to solve this problem. I now index strings from 0 in Android and from 1 for a Windows 32-bit compilation. Thank you so much!

IMN logo majestic logo threadwatch logo seochat tools logo