Skip to content

string.IndexOf("string") not working in Raspberry Pi project #33997

Closed

Description

@digital-spinner

I have recently upgraded .NET Core 3.1 SDK to version v3.1.200 with Visual Studio Community 2019 and I'm building and application for Raspberry Pi where one part of it reads the content on specific file from filesystem and parses it (very simple).

Some days ago (don't remember previous core version) but it was working correctly. Now suddently stopped. The problem is with searching of a specific substring position in the file using string.IndexOf method. It always gets me return value of -1 for whatever string I search for, even with only char inside. But when I search for last char of '=' it gives me proper index result. (But I want to search for "t=" substring here anyway)..

string sensorStatus = System.IO.File.ReadAllText(path);
int index = sensorStatus.IndexOf("t=") + 2;
string temperatureString = sensorStatus.Substring(index, 5);
double result = Convert.ToDouble(temperatureString) / 1000;

File content:

57 01 4b 46 7f ff 0c 10 38 : crc=38 YES
57 01 4b 46 7f ff 0c 10 38 t=21437

w1_slave.txt

The same file content (whole file) copied and run in Windows yield with proper parsed value.

Am I missing something here? What is even funnier the same code before worked but can't recall exact framework versions etc..

Activity

  1. added
    untriaged
    on Mar 24, 2020
  2. GrabYourPitchforks commented on Mar 24, 2020

    @GrabYourPitchforks
    Member

    @tarekgh The overload string.Contains(string) uses CultureInfo.CurrentCulture for comparison. Did we change how ICU works when running on RPi systems in 3.1?

  3. tarekgh commented on Mar 24, 2020

    @tarekgh
    Member

    @GrabYourPitchforks

    The overload string.Contains(string) uses CultureInfo.CurrentCulture for comparison. Did we change how ICU works when running on RPi systems in 3.1?

    did you mean string.IndexOf as the issue not reported with the Contains method? We didn't change anything in 3.1 specifics to RPi nor any other system. We have a global change that avoiding using POSIX/C locales and use Invariant culture instead.

    @digital-spinner

    It will be hard to tell what could be the problem, the only thing I can think of could be the codepage used for reading the file. could you please try after reading the whole text, print every single character:

        string sensorStatus = System.IO.File.ReadAllText(path);
        foreach (char c in sensorStatus)
        {
            Console.WriteLine($"{c} ... {(int)c:x}");
        }

    Also, could you please get the value of the environment variable LANG from the system? and let's know if the environment variable DOTNET_SYSTEM_GLOBALIZATION_INVARIANT is defined in the system.

  4. added
    question
    and removed
    untriaged
    on Mar 24, 2020
  5. added this to the 5.0 milestone on Mar 24, 2020
  6. GrabYourPitchforks commented on Mar 24, 2020

    @GrabYourPitchforks
    Member

    Yes, string.IndexOf, not string.Contains. My error. :)

  7. digital-spinner commented on Mar 24, 2020

    @digital-spinner
    Author

    OK, thank you and I'm very sorry for disturbing you all.. Everything works as intended except my SDCARD which was used in my Raspberry Pi. I have finally discovered this issue when my app started failing during web server initialization when tried to ask about System Culture etc. After I replaced that everything started to work again.

    Again sorry and thank you for your support!

  8. ghost locked as resolved and limited conversation to collaborators on Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Globalizationquestion

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions