EDN Admin
Well-known member
Hi,
Im searching for a method in the framework to convert escape sequences (like "n") into their binary counterpart (like 0x0a). To be clear, heres a function in C++ that does the job:
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
char* ConvertEscapeChars(char* str)
{
char* result = new char[strlen(str)];
sprintf(result,str);
return result;
}
Of course this question is quite theoretical. I am aware that
- One could do it with a series of replacements
- One could do it with Interop
- .NET has a different way of formatting strings
Anyway Id like to know if there is a way to do this directly with some method in the .NET framework? There must be some, since the C# compiler does just this conversion when parsing strings in the source code. Besides, the opposite conversion goes like this:
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
string ConvertToEscapedString(string str)
{
StringWriter sw = new StringWriter();
CodeExpression expr = new CodePrimitiveExpression(str);
new CSharpCodeProvider().GenerateCodeFromExpression(expr, sw, new CodeGeneratorOptions());
return sw.ToString();
}
Thanks,
Stefan
View the full article
Im searching for a method in the framework to convert escape sequences (like "n") into their binary counterpart (like 0x0a). To be clear, heres a function in C++ that does the job:
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
char* ConvertEscapeChars(char* str)
{
char* result = new char[strlen(str)];
sprintf(result,str);
return result;
}
Of course this question is quite theoretical. I am aware that
- One could do it with a series of replacements
- One could do it with Interop
- .NET has a different way of formatting strings
Anyway Id like to know if there is a way to do this directly with some method in the .NET framework? There must be some, since the C# compiler does just this conversion when parsing strings in the source code. Besides, the opposite conversion goes like this:
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
string ConvertToEscapedString(string str)
{
StringWriter sw = new StringWriter();
CodeExpression expr = new CodePrimitiveExpression(str);
new CSharpCodeProvider().GenerateCodeFromExpression(expr, sw, new CodeGeneratorOptions());
return sw.ToString();
}
Thanks,
Stefan
View the full article