473,772 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String conversion

Hi,

Is there any in-built converter which will convert a string like

markrae

into

markrae

Child's play to write a function to do it, but I just wondered if it already
exists within the .NET Framework.

Any assistance gratefully received.

Mark Rae
Nov 16 '05 #1
6 1390
Hi,

No AFAIk, but as you said before it's VERY easy to program this.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Hi,

Is there any in-built converter which will convert a string like

markrae

into

markrae

Child's play to write a function to do it, but I just wondered if it already exists within the .NET Framework.

Any assistance gratefully received.

Mark Rae

Nov 16 '05 #2
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:uA******** ******@TK2MSFTN GP10.phx.gbl...
No AFAIk, but as you said before it's VERY easy to program this.


This is how I did it...

public string encodeEvent(str ing pstrEvent)
{
string strReturn = "";
for(int intPos = 0; intPos < pstrEvent.Lengt h; intPos++)
{
strReturn += "&#" +
((int)Encoding. ASCII.GetBytes( pstrEvent)[intPos]).ToString() + ";";
}
return strReturn;
}
Nov 16 '05 #3
On Wed, 28 Jul 2004 14:38:26 +0100, "Mark Rae"
<ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:uA******** ******@TK2MSFTN GP10.phx.gbl...
No AFAIk, but as you said before it's VERY easy to program this.


This is how I did it...

public string encodeEvent(str ing pstrEvent)
{
string strReturn = "";
for(int intPos = 0; intPos < pstrEvent.Lengt h; intPos++)
{
strReturn += "&#" +
((int)Encoding .ASCII.GetBytes (pstrEvent)[intPos]).ToString() + ";";
}
return strReturn;
}

A more efficient method:
string ToOrdinals(stri ng str)
{
StringBuilder sb = new StringBuilder() ;

foreach (char c in str)
{
sb.Append("&#") ;
sb.Append((int) c);
sb.Append(";");
}
return sb.ToString();
}
Nov 16 '05 #4
"Austin Ehlers" <li************ *******@ku.edu> wrote in message
news:hk******** *************** *********@4ax.c om...
A more efficient method:


In what way is it more efficient...?
Nov 16 '05 #5
Mark Rae <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote:
"Austin Ehlers" <li************ *******@ku.edu> wrote in message
news:hk******** *************** *********@4ax.c om...
A more efficient method:


In what way is it more efficient...?


In just about every possible way, to be honest.

It doesn't create extra intermediate strings for no good reason. It
also doesn't use Encoding.ASCII for no particularly good reason. (Both
will give bad results for non-ASCII data.) I've only just noticed
you're encoding the whole string for *every* iteration!

Here's a little benchmark encoding a string of 10,000 characters 100
times with each method:

using System;
using System.Text;

class Test
{
static void Main()
{
string test = new string('a', 10000);

if (Mark(test) != Austin(test))
{
throw new Exception
("Can't test - results aren't the same");
}

{
DateTime start = DateTime.Now;
for (int i=0; i < 100; i++)
{
Mark(test);
}
DateTime end = DateTime.Now;
Console.WriteLi ne ("Mark: {0}", end-start);
}

{
DateTime start = DateTime.Now;
for (int i=0; i < 100; i++)
{
Austin(test);
}
DateTime end = DateTime.Now;
Console.WriteLi ne ("Austin: {0}", end-start);
}
}

public static string Mark(string pstrEvent)
{
string strReturn = "";
for(int intPos = 0; intPos < pstrEvent.Lengt h; intPos++)
{
strReturn += "&#" +
((int)Encoding. ASCII.GetBytes( pstrEvent)[intPos]).ToString() + ";";
}
return strReturn;
}

public static string Austin(string str)
{
StringBuilder sb = new StringBuilder() ;

foreach (char c in str)
{
sb.Append("&#") ;
sb.Append((int) c);
sb.Append(";");
}
return sb.ToString();
}
}

And the results:
Mark: 00:02:34.796875 0
Austin: 00:00:00.703125 0

For this particular data, Austin's version is about 220 times faster
than yours. For longer strings, that would go up - you'd be creating
more (and longer) intermediate strings, and you'd be encoding a longer
string every iteration.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
And the results:
Mark: 00:02:34.796875 0
Austin: 00:00:00.703125 0

For this particular data, Austin's version is about 220 times faster
than yours.


Well, that's good enough for me! Thanks very much. I'm now using Austin's
version instead of mine.
Nov 16 '05 #7

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

Similar topics

10
3634
by: Marcin Kalicinski | last post by:
Why string literals are regarded as char * not as const char *? (1) void f(char *); (2) void f(const char *); f("foo") will call version (1) of function f. I understand that the exact type of literal "foo" is char, which by means of standard conversion becomes char *. Still, there's something going wrong here, because this allows modification of "foo", which in my opinion should
2
3095
by: Thomas Matthews | last post by:
Hi, I'm working with Borland C++ Builder 6.2. My project uses the std::string class. However, Borland in its infinite wisdom has its own string class, AnsiString. To make my life easier, I want to create a function, preferably a conversion operator, to convert from a std::string to an AnsiString. Please note that I don't want to change either std::string or AnsiString.
12
2217
by: ABeck | last post by:
Hello List, I have ar more or less academical question. Can there arise runtime errors in a program, if the include of <string.h> has been forgotten? If all the arguments to the functions of <string.h> are correct, is it possible that an error occurs, and what an error might this be? Regards
6
13359
by: Marco Herrn | last post by:
Hi, I need to serialize an object into a string representation to store it into a database. So the SOAPFormatter seems to be the right formatter for this purpose. Now I have the problem that this formatter writes into a stream. And I am not used enough to C# to convert this to a string. I tried the following code: MemoryStream stream= new MemoryStream() ; IFormatter formatter = new SoapFormatter();
6
2214
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
4
5547
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm converting the binary blocks to strings first, then converting the resulting string to base64. This seems highly inefficient and I'd like to just go straight from binary to a base64 string. Here is the conversion we're using from object to...
10
13642
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem like c# is breaking its own rules? This works: int b = 32; string a = "ABC " + b; Result: a = "ABC 32"
5
5973
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For compression string ->(Unicode Conversion) byte -(Compression + Unicode Conversion) string
3
9530
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++ code (ie. wrappers etc). In Managed C++ there was an automatic conversion between const char* and String^. This was useful to us for two reasons: 1. We declared our string constants as eg. const char* const c_My_Constant = "blah", and these...
10
9088
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is Unicode, UCS-2LE(BMP) in Win32, and UCS4 in Linux?). I have 2 ways to do the job:
0
9621
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
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
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...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7461
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
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
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
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.