Different characters on different devices

So i dont get it =)

I have a int x.ToString(); drawn on the screen with SpriteFont. If the int goes negative the minus evaluates to different char depending on the device.

(int)x.ToString().ToCharArray()[0] becomes:
45 on Desktop and the Android emulator but 8722 on my Android hardware (Galaxy s22 ultra)

(int)'-' becomes 45 on all devices.

so basically my question is… what gives?

try with .ToString(CultureInfo.InvariantCulture)

1 Like

why, thank you!

Found this to set it globaly.

1 Like

out of interest … is the problem, that the char code is not what you expect or that it is drawn wrong on the screen?

(int)x.ToString()

is different than

((int)x).ToString()

the first casts the result of “tostring()” to int (most likely not what you want or in your example you’re casting the char on index 0 to int), the second one casts x to int first and makes toString on that (which calls Int32.ToString instead of float.ToString or whatever x is)

you can also just output the float directly via x.ToString(“0”)

PS: 8722 is the unicode decimal code for the minus sign “-” so I am not really sure how the cultureinfo did change anything … maybe because android puts out “&#8722” and if you cast that to int you may end up with 8722 … as you are casting the char to int not x

I was giving you poorly writen exampels. The int cast was just part of my way of finding out why SpriteFont crash my app.

x.ToString() with 8722 in my SpriteFont file worked fine.

and x.ToString(CultureInfo.InvariantCulture) worked witout 8722 in my spritefont file.

So im thinking the CultureInfo thing did someting.

Possibly the Galaxy s22 ultra has the Language/Region set to something other that English/US.
Some cultures use formatting that has characters outside the safe ASCII range.

the char datatype represents a 16 bit unicode character - it’s not ASCII … one would first need to ASCIIEncode to make sure to get an ASCII value … which is then used for lookup by the DrawString Method (as those most likely work with ASCII values not sure if Font libraries allow for alias or full unicode referencing?)

Looks like on that Android Phone the culture forces the string “-” to be encoded in full unicode and that code is then not found in the font

1 Like

I think you both make sense… what are we trying to do here :slight_smile:

“As those most likely work with ASCII values not sure if Font libraries allow for alias or full unicode referencing?”

isnt the fact that adding 8722 to the font file, makeing the app run, evidence that it takes full unicode referencing?

My phone was set to “nordic viking” so… yes very much, not english!

Lol, I said ASCII range.

You can include character ranges in hexadecimal,

  <!-- (extended latin I)-->
  <CharacterRegion>
    <Start>&#x0100;</Start>
    <End>&#x01FF;</End>
  </CharacterRegion>

or decimal

  <CharacterRegion>
    <Start>&#32;</Start>
    <End>&#255;</End>
  </CharacterRegion>

edit
… by reading your message again I understand that you had already figure how to add 8722 in the font file . :sunglasses:

1 Like