473,388 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Converting to/from a string without regard to culture

Hi there,

Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let's say you're dealing
with the native "System.Drawing.Size" type on an English version of Windows.
You use the "TypeConverter" for this structure to generate the string, say,
"50, 75" which you then store in a file (or perhaps a DB). Now, the same
value might later be read back in from this file on a Japenese version of
Windows (or more accurately on a thread where the culture is set to
Japanese). Let's say the comma (",") is actually a period (".") in Japanese
however though I'm just making this up for demonstration purposes. How do
you now convert the original value "50, 75" into a "Size" object given that
the system presumably won't recognize the comma anymore (only a period).
Conversely, how do you convert a "Size" object back to a culture-independent
string (in this case using a comma) so that it can be processed on the
original English machine again. Or maybe this isn't as complicated as I'm
making it out to be. I'm just not sure how to use the "TypeConverter" class
to write my string in a consistent way given that it has to be read back in
on a thread running with a different "CutlureInfo". Can anyone provide any
insight on the matter. Thanks very much.
May 26 '07 #1
10 3041
Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let's say you're dealing
with the native "System.Drawing.Size" type on an English version of
Windows. You use the "TypeConverter" for this structure to generate the
string, say, "50, 75" which you then store in a file (or perhaps a DB).
Now, the same value might later be read back in from this file on a
Japenese version of Windows (or more accurately on a thread where the
culture is set to Japanese). Let's say the comma (",") is actually a
period (".") in Japanese however though I'm just making this up for
demonstration purposes. How do you now convert the original value "50, 75"
into a "Size" object given that the system presumably won't recognize the
comma anymore (only a period). Conversely, how do you convert a "Size"
object back to a culture-independent string (in this case using a comma)
so that it can be processed on the original English machine again. Or
maybe this isn't as complicated as I'm making it out to be. I'm just not
sure how to use the "TypeConverter" class to write my string in a
consistent way given that it has to be read back in on a thread running
with a different "CutlureInfo". Can anyone provide any insight on the
matter. Thanks very much.
Ok, I'm thinking that I need to call
"TypeConverter.ConvertToInvariantString()" to write the value and
"TypeConverter.ConvertFromInvariantString()" to read it back in. However,
once read back in using the latter function, I then need to pass the value
to "TypeConverter.ConvertToString()" if I actually want to display it (since
I assume "ConvertToString()" is culture-sensitive). Conversely, if a user
enters the string in some field during data-input, I would need to convert
it using "TypeConverter.ConvertFromString()" and then pass this back to
"TypeConverter.ConvertToInvariantString()" before writing it to file again.
Is this correct? Thanks.
May 26 '07 #2
John Brown <no_spam@_nospam.comwrote:
Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let's say you're dealing
with the native "System.Drawing.Size" type on an English version of Windows.
You use the "TypeConverter" for this structure to generate the string, say,
"50, 75" which you then store in a file (or perhaps a DB). Now, the same
value might later be read back in from this file on a Japenese version of
Windows (or more accurately on a thread where the culture is set to
Japanese). Let's say the comma (",") is actually a period (".") in Japanese
however though I'm just making this up for demonstration purposes. How do
you now convert the original value "50, 75" into a "Size" object given that
the system presumably won't recognize the comma anymore (only a period).
Have you tested this assumption? I would hope that TypeConverter was
already culture-independent, but I haven't tried it.
Conversely, how do you convert a "Size" object back to a culture-independent
string (in this case using a comma) so that it can be processed on the
original English machine again. Or maybe this isn't as complicated as I'm
making it out to be. I'm just not sure how to use the "TypeConverter" class
to write my string in a consistent way given that it has to be read back in
on a thread running with a different "CutlureInfo". Can anyone provide any
insight on the matter. Thanks very much.
I'd say the first thing to do is to make sure there's actually a
problem. Try exactly the situation you fear will fail.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 26 '07 #3
pax
The comma you are talking about is actually part of the language, C#, it is
a list separator, and therefore it's got nothing to do with localization!
"John Brown" <no_spam@_nospam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let's say you're
dealing with the native "System.Drawing.Size" type on an English version
of Windows. You use the "TypeConverter" for this structure to generate
the string, say, "50, 75" which you then store in a file (or perhaps a
DB). Now, the same value might later be read back in from this file on a
Japenese version of Windows (or more accurately on a thread where the
culture is set to Japanese). Let's say the comma (",") is actually a
period (".") in Japanese however though I'm just making this up for
demonstration purposes. How do you now convert the original value "50,
75" into a "Size" object given that the system presumably won't recognize
the comma anymore (only a period). Conversely, how do you convert a
"Size" object back to a culture-independent string (in this case using a
comma) so that it can be processed on the original English machine again.
Or maybe this isn't as complicated as I'm making it out to be. I'm just
not sure how to use the "TypeConverter" class to write my string in a
consistent way given that it has to be read back in on a thread running
with a different "CutlureInfo". Can anyone provide any insight on the
matter. Thanks very much.

Ok, I'm thinking that I need to call
"TypeConverter.ConvertToInvariantString()" to write the value and
"TypeConverter.ConvertFromInvariantString()" to read it back in. However,
once read back in using the latter function, I then need to pass the value
to "TypeConverter.ConvertToString()" if I actually want to display it
(since I assume "ConvertToString()" is culture-sensitive). Conversely, if
a user enters the string in some field during data-input, I would need to
convert it using "TypeConverter.ConvertFromString()" and then pass this
back to "TypeConverter.ConvertToInvariantString()" before writing it to
file again. Is this correct? Thanks.

May 26 '07 #4
pax
Sorry, my bad, it seems that the list separator IS part of the Regional
Options :)
"pax" <pa*@noname.comwrote in message
news:ub**************@TK2MSFTNGP03.phx.gbl...
The comma you are talking about is actually part of the language, C#, it
is a list separator, and therefore it's got nothing to do with
localization!
"John Brown" <no_spam@_nospam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>>Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let's say you're
dealing with the native "System.Drawing.Size" type on an English version
of Windows. You use the "TypeConverter" for this structure to generate
the string, say, "50, 75" which you then store in a file (or perhaps a
DB). Now, the same value might later be read back in from this file on a
Japenese version of Windows (or more accurately on a thread where the
culture is set to Japanese). Let's say the comma (",") is actually a
period (".") in Japanese however though I'm just making this up for
demonstration purposes. How do you now convert the original value "50,
75" into a "Size" object given that the system presumably won't
recognize the comma anymore (only a period). Conversely, how do you
convert a "Size" object back to a culture-independent string (in this
case using a comma) so that it can be processed on the original English
machine again. Or maybe this isn't as complicated as I'm making it out
to be. I'm just not sure how to use the "TypeConverter" class to write
my string in a consistent way given that it has to be read back in on a
thread running with a different "CutlureInfo". Can anyone provide any
insight on the matter. Thanks very much.

Ok, I'm thinking that I need to call
"TypeConverter.ConvertToInvariantString()" to write the value and
"TypeConverter.ConvertFromInvariantString()" to read it back in. However,
once read back in using the latter function, I then need to pass the
value to "TypeConverter.ConvertToString()" if I actually want to display
it (since I assume "ConvertToString()" is culture-sensitive). Conversely,
if a user enters the string in some field during data-input, I would need
to convert it using "TypeConverter.ConvertFromString()" and then pass
this back to "TypeConverter.ConvertToInvariantString()" before writing
it to file again. Is this correct? Thanks.


May 26 '07 #5
Can CultureInfo.InvariantCulture property help you to achieve required
result?
"John Brown" <no_spam@_nospam.comwrote in message
news:uA**************@TK2MSFTNGP04.phx.gbl...
Hi there,

Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let's say you're dealing
with the native "System.Drawing.Size" type on an English version of
Windows. You use the "TypeConverter" for this structure to generate the
string, say, "50, 75" which you then store in a file (or perhaps a DB).
Now, the same value might later be read back in from this file on a
Japenese version of Windows (or more accurately on a thread where the
culture is set to Japanese). Let's say the comma (",") is actually a
period (".") in Japanese however though I'm just making this up for
demonstration purposes. How do you now convert the original value "50, 75"
into a "Size" object given that the system presumably won't recognize the
comma anymore (only a period). Conversely, how do you convert a "Size"
object back to a culture-independent string (in this case using a comma)
so that it can be processed on the original English machine again. Or
maybe this isn't as complicated as I'm making it out to be. I'm just not
sure how to use the "TypeConverter" class to write my string in a
consistent way given that it has to be read back in on a thread running
with a different "CutlureInfo". Can anyone provide any insight on the
matter. Thanks very much.

May 27 '07 #6
Thanks for the feedback...
John Brown <no_spam@_nospam.comwrote:
>Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let's say you're
dealing
with the native "System.Drawing.Size" type on an English version of
Windows.
You use the "TypeConverter" for this structure to generate the string,
say,
"50, 75" which you then store in a file (or perhaps a DB). Now, the same
value might later be read back in from this file on a Japenese version of
Windows (or more accurately on a thread where the culture is set to
Japanese). Let's say the comma (",") is actually a period (".") in
Japanese
however though I'm just making this up for demonstration purposes. How do
you now convert the original value "50, 75" into a "Size" object given
that
the system presumably won't recognize the comma anymore (only a period).

Have you tested this assumption? I would hope that TypeConverter was
already culture-independent, but I haven't tried it.
I can't find any documenation that clarifies the situation but I'm fairly
certain that it isn't (culturally-independent). How could it be for instance
when commas and other types of punctuation can differ from one culture to
another (which is readily apparent simply by looking at regional settings in
the control panel). Moreover, customized type conversions may be influenced
by locality and that's something you can never escape. Finally, a quick look
at some of the "TypeConverter()" functions clearly indicates that culture
does come into play but I can't pin down the precise rules.
>
>Conversely, how do you convert a "Size" object back to a
culture-independent
string (in this case using a comma) so that it can be processed on the
original English machine again. Or maybe this isn't as complicated as I'm
making it out to be. I'm just not sure how to use the "TypeConverter"
class
to write my string in a consistent way given that it has to be read back
in
on a thread running with a different "CutlureInfo". Can anyone provide
any
insight on the matter. Thanks very much.

I'd say the first thing to do is to make sure there's actually a
problem. Try exactly the situation you fear will fail.
You can't accurately test this however without actually installing a
localized version of Windows or possibly using the Windows MUI (Multi User
Interface) pack which really isn't a (completely) accurate test IMO. The MUI
pack isn't available through normal retail channels in any case. I'd like to
know what the rules actually say rather than just relying on ad hoc tests.
Anyway. thanks again for the feedback.
May 27 '07 #7
Can CultureInfo.InvariantCulture property help you to achieve required
result?
Maybe (probably perhaps) but I'm still looking into the matter as mentioned
in my initial follow-up post. I think
"TypeConverter.ConvertToInvariantString()" and cousins are probably the key.
May 27 '07 #8
Size size = new Size(1,2);
TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(Size));
String text = typeConverter.ConvertToInvariantString(size);
size = (Size)typeConverter.ConvertFromInvariantString(tex t);

Which is jus short-hand for:
text = typeConverter.ConvertToString(null, CultureInfo.InvariantCulture,
size);
size = (Size)typeConverter.ConvertFromString(null,
CultureInfo.InvariantCulture, text);

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"John Brown" wrote:
Hi there,

Does anyone know how to go about reading/writing a type to a file in a
language (culture) independent way. For instance, let's say you're dealing
with the native "System.Drawing.Size" type on an English version of Windows.
You use the "TypeConverter" for this structure to generate the string, say,
"50, 75" which you then store in a file (or perhaps a DB). Now, the same
value might later be read back in from this file on a Japenese version of
Windows (or more accurately on a thread where the culture is set to
Japanese). Let's say the comma (",") is actually a period (".") in Japanese
however though I'm just making this up for demonstration purposes. How do
you now convert the original value "50, 75" into a "Size" object given that
the system presumably won't recognize the comma anymore (only a period).
Conversely, how do you convert a "Size" object back to a culture-independent
string (in this case using a comma) so that it can be processed on the
original English machine again. Or maybe this isn't as complicated as I'm
making it out to be. I'm just not sure how to use the "TypeConverter" class
to write my string in a consistent way given that it has to be read back in
on a thread running with a different "CutlureInfo". Can anyone provide any
insight on the matter. Thanks very much.
May 27 '07 #9
Size size = new Size(1,2);
TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(Size));
String text = typeConverter.ConvertToInvariantString(size);
size = (Size)typeConverter.ConvertFromInvariantString(tex t);

Which is jus short-hand for:
text = typeConverter.ConvertToString(null, CultureInfo.InvariantCulture,
size);
size = (Size)typeConverter.ConvertFromString(null,
CultureInfo.InvariantCulture, text);
Ok, thanks. It appears that the use of "InvariantCulture" is probably the
key so at least I can go from here.
May 27 '07 #10
John Brown <no_spam@_nospam.comwrote:
Have you tested this assumption? I would hope that TypeConverter was
already culture-independent, but I haven't tried it.

I can't find any documenation that clarifies the situation but I'm fairly
certain that it isn't (culturally-independent). How could it be for instance
when commas and other types of punctuation can differ from one culture to
another (which is readily apparent simply by looking at regional settings in
the control panel). Moreover, customized type conversions may be influenced
by locality and that's something you can never escape. Finally, a quick look
at some of the "TypeConverter()" functions clearly indicates that culture
does come into play but I can't pin down the precise rules.
My mistake - I'd somehow thought you were talking about a serializer.
However, it certainly does look like the ConvertToInvariantString calls
are what you're after.
I'd say the first thing to do is to make sure there's actually a
problem. Try exactly the situation you fear will fail.

You can't accurately test this however without actually installing a
localized version of Windows or possibly using the Windows MUI (Multi User
Interface) pack which really isn't a (completely) accurate test IMO. The MUI
pack isn't available through normal retail channels in any case. I'd like to
know what the rules actually say rather than just relying on ad hoc tests.
Anyway. thanks again for the feedback.
Well, just changing the culture of the current thread and seeing
whether you can still retrieve the data accurately would be a good test
to start with, I'd say. I completely understand about wanting more than
tests though :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 27 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Daniel Patriak | last post by:
hello! Anybody knows if there is a method in C#, that can convert date given as string, using date format given as string (like DD/MM/YYYY, DD-MM-YYYY) ? Please help! I couldn't find it in...
10
by: Kim Hellan | last post by:
I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to convert to a DateTime value. I know this is a standard problem, but please don't just link to all the MSDN pages regarding...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
2
by: Fredrik Rodin | last post by:
All, I'm having problems with my resource manager in ASP.NET 2.0 after conversion from ASP.NET 1.1. Here is a background: In ASP.NET 1.1 All my user controls and aspx pages inherit from...
14
by: eric.goforth | last post by:
Hello, I'm trying to convert a string to a date time in a C# web service. I'm passing in a string parameter and I have a localization setting in my Web.config file: My app is blowing up on...
5
by: vivekaseeja | last post by:
Hi , Trying to convert a string value to a float value after reading the value from an XML file , but not sure what function to use. The following only works for integers Int32.Parse...
2
by: Curious Trigger | last post by:
Hello, if have an asp.net web page with a detailsview. This detailsview uses a sqldatasource connecting to a sql server 2005 database with a select statement simliar to this one: SELECT...
1
by: pebelund | last post by:
Hi I got an .aspx that got the following code (example) <td width="120"> <% = row %> </td> row takes a value from a SQL db, that is a string of numbers. On the webbsite I want this to show...
11
by: Mark Braswell | last post by:
Hello, I would like to hear people's thoughts and opinions on the best way for a VB.NET developer to move into C#. This is specifically from a job perspective, and specifically from a UK based...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.