Skip to main content
gamedev.net
🔒 Locked

[.net] Reading From File Encoding Problem

Started by ZZajoman 11 replies 1.7k views
Original Post
ZZajoman
ZZajoman
Hi there, I'm reading data from a file in XNA. More precisely a string that I convert in run-time to a float. The problem is that it cannot convert the string because of wrong format. The string is "4.0". When I change it to "4,0", it works. It has something to do with my local settings, I believe. I want it "4.0" as it is standard in English. How do I set this to work in the program no matter what settings a user has?
SiCrane
SiCrane
Well, how are you doing it now? If you're using Single.Parse(), then you can use an IFormatProvider or a NumberSyles to control how the string gets parsed.
ZZajoman
ZZajoman
Well, I'm doing it like this
float f = Convert.ToSingle("4.0");
SiCrane
SiCrane
You can use CultureInfo.InvariantCulture, to get a culture-insensitive IFormatProvider that you can use with ToSingle().
ZZajoman
ZZajoman
Well, I have no idea how to use the interface.
SiCrane
SiCrane
      CultureInfo ci = CultureInfo.InvariantCulture;      float f = Convert.ToSingle("4.0", ci);
ZZajoman
ZZajoman
That works, thank you very much, SiCrane. Now, how do you know it's the implementation of the interface?
SiCrane
SiCrane
I have no idea what you just asked.
ZZajoman
ZZajoman
Now I'm wondering how to set this globally, so I don't have to use the culture info everytime.
SiCrane
SiCrane
If you have the ControlThread SecurityPermission, you can set the Thread's CurrentCulture to any CultureInfo you like.
ZZajoman
ZZajoman
I have absolutely no idea about that. I'll do it differently. Thank you.
SiCrane
SiCrane
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

Again, this is on a per thread basis, and only works if you have the ControlThread SecurityPermission (which is generally true if the code is in your own application; may not be true if you're writing a library function that someone else calls).
ZZajoman
ZZajoman
This does not work for me. And the code is in my application.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.