473,624 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "TypeConver ter" 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 "TypeConver ter" 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 "CutlureInf o". Can anyone provide any
insight on the matter. Thanks very much.
May 26 '07 #1
10 3065
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 "TypeConver ter" 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 "TypeConver ter" 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 "CutlureInf o". Can anyone provide any insight on the
matter. Thanks very much.
Ok, I'm thinking that I need to call
"TypeConverter. ConvertToInvari antString()" to write the value and
"TypeConverter. ConvertFromInva riantString()" 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 "ConvertToStrin g()" is culture-sensitive). Conversely, if a user
enters the string in some field during data-input, I would need to convert
it using "TypeConverter. ConvertFromStri ng()" and then pass this back to
"TypeConverter. ConvertToInvari antString()" before writing it to file again.
Is this correct? Thanks.
May 26 '07 #2
John Brown <no_spam@_nospa m.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 "TypeConver ter" 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 "TypeConver ter" 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 "CutlureInf o". 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.co m>
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@_nospa m.comwrote in message
news:%2******** ********@TK2MSF TNGP05.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 "TypeConver ter" 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
demonstratio n 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 "TypeConver ter" 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 "CutlureInf o". Can anyone provide any insight on the
matter. Thanks very much.

Ok, I'm thinking that I need to call
"TypeConverter. ConvertToInvari antString()" to write the value and
"TypeConverter. ConvertFromInva riantString()" 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 "ConvertToStrin g()" is culture-sensitive). Conversely, if
a user enters the string in some field during data-input, I would need to
convert it using "TypeConverter. ConvertFromStri ng()" and then pass this
back to "TypeConverter. ConvertToInvari antString()" 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.com wrote in message
news:ub******** ******@TK2MSFTN GP03.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@_nospa m.comwrote in message
news:%2******** ********@TK2MSF TNGP05.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 "TypeConver ter" 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
demonstrati on 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 "TypeConver ter" 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 "CutlureInf o". Can anyone provide any
insight on the matter. Thanks very much.

Ok, I'm thinking that I need to call
"TypeConverter .ConvertToInvar iantString()" to write the value and
"TypeConverter .ConvertFromInv ariantString()" 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 "ConvertToStrin g()" is culture-sensitive). Conversely,
if a user enters the string in some field during data-input, I would need
to convert it using "TypeConverter. ConvertFromStri ng()" and then pass
this back to "TypeConverter. ConvertToInvari antString()" before writing
it to file again. Is this correct? Thanks.


May 26 '07 #5
Can CultureInfo.Inv ariantCulture property help you to achieve required
result?
"John Brown" <no_spam@_nospa m.comwrote in message
news:uA******** ******@TK2MSFTN GP04.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 "TypeConver ter" 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 "TypeConver ter" 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 "CutlureInf o". Can anyone provide any insight on the
matter. Thanks very much.

May 27 '07 #6
Thanks for the feedback...
John Brown <no_spam@_nospa m.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 "TypeConver ter" 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 "TypeConver ter"
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 "CutlureInf o". 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.Inv ariantCulture 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. ConvertToInvari antString()" and cousins are probably the key.
May 27 '07 #8
Size size = new Size(1,2);
TypeConverter typeConverter = TypeDescriptor. GetConverter(ty peof(Size));
String text = typeConverter.C onvertToInvaria ntString(size);
size = (Size)typeConve rter.ConvertFro mInvariantStrin g(text);

Which is jus short-hand for:
text = typeConverter.C onvertToString( null, CultureInfo.Inv ariantCulture,
size);
size = (Size)typeConve rter.ConvertFro mString(null,
CultureInfo.Inv ariantCulture, 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 "TypeConver ter" 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 "TypeConver ter" 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 "CutlureInf o". 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(ty peof(Size));
String text = typeConverter.C onvertToInvaria ntString(size);
size = (Size)typeConve rter.ConvertFro mInvariantStrin g(text);

Which is jus short-hand for:
text = typeConverter.C onvertToString( null, CultureInfo.Inv ariantCulture,
size);
size = (Size)typeConve rter.ConvertFro mString(null,
CultureInfo.Inv ariantCulture, text);
Ok, thanks. It appears that the use of "InvariantCultu re" is probably the
key so at least I can go from here.
May 27 '07 #10

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

Similar topics

5
6238
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 MSDN! Thanks Daniel
10
15538
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 Parse() and ParseExact(). I've been there and read all the information about using IFormatProviders, DateTimeStyles, CultureInfo....... Bottom line is that I can't get it working, so could someone please just post 5-10 lines of working code...
9
2585
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 explain it. I have some questions about the instructions for creating a mixed mode DLL in the MSDN topic "Converting Managed Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode" in the "Managed Extensions for C++ Reference"....
2
5520
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 base classes. A base class includes this property (among others...;-)):
14
3395
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 the first line here, with a"Value does not fall within the expected range" error.
5
7863
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 (readXml.Value) ; Any suggestions for an alternate function ..
2
3012
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 warning_threshold, critical_threshold, weight FROM thresholds UPDATE thresholds SET warning_threshold=@warning_threshold, critical_threshold=@critical_threshold, weight =@weight I also added insert and delete statements to this datasource.
1
2084
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 as value formatted in thousands (example: I get the value 12345678, and I want it to show like 12 345
11
1547
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 job perspective. I am a VB.NET developer, and have been developing in VB.NET (also ASP.NET) for approx. 3.5 years. Prior to that, I developed in VB6/VB5/VB4 and classic ASP for approx. 10 years.
0
8246
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8490
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.