i have a URL like http%3A%2F%2Fwww%2Ewikimoveis%2Ecom%2Ebr%2Ffotos%2F1292%2FKO2551%2FDSC01366%2EJPG
I need to get it decode correctly. I believe that is Hexadecimal. Can't find any C# code that would do that.
thank you
From C# URLDecode turns %5C into \\\\ instead of \
using System;
using System.Web;
string url = "http%3A2F%2Fwww%2Ewikimoveis%2Ecom%2Ebr%2Ffotos%2F1292%2FKO2551%2FDSC01366%2EJPG"
string decoded = HttpUtility.UrlDecode(url);
Outside of a web application, this is a way:
using System;
using System.Net;
namespace urldecoder
{
internal class Program
{
static void Main(string[] args)
{
String EncodedString = "http%3A%2F%2Fwww%2Ewikimoveis%2Ecom%2Ebr%2Ffotos%2F1292%2FKO2551%2FDSC01366%2EJPG";
String DecodedString = WebUtility.UrlDecode(EncodedString);
Console.WriteLine(DecodedString);
Console.ReadLine();
}
}
}
See: system-net-webutility-urldecode(system-string)
Related
Given the following string:
a:3:{s:6:"status";i:1;s:3:"msg";s:39:"Transaction has been already Completed.";s:6:"result";s:1620:"eyJtaWhwYXlpZCI6NDAzOTkzNzE1NTExODQwODY3LCJ tb2RlIjoiREJRUiIsInN0YXR1cyI6ImZhaWx1cmUiLCJrZXkiOiI3a2h6VEciLCJ0eG5pZCI6IjEy MzQ1IiwiYW1vdW50IjoiMS4wMCIsImFkZGVkb24iOiIyMDE5LTA1LTE3IDE3OjEwOjEzIiwic HJvZHVjdGluZm8iOiJPZmZsaW5lIER5bmFtaWMgUVIiLCJmaXJzdG5hbWUiOiIiLCJsYXN0 bmFtZSI6IiIsImFkZHJlc3MxIjoiIiwiYWRkcmVzczIiOiIiLCJjaXR5IjoiIiwic3RhdGUiOiIiLCJjb3V udHJ5IjoiIiwiemlwY29kZSI6IiIsImVtYWlsIjoiIiwicGhvbmUiOiIiLCJ1ZGYxIjoiIiwidWRmMiI6IiIsI nVkZjMiOiIiLCJ1ZGY0IjoiIiwidWRmNSI6IiIsInVkZjYiOiIiLCJ1ZGY3IjoiIiwidWRmOCI6IiIs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WNjZXNzIiwiZmllbGQ4IjoiIiwiZmllbGQ5IjoiU1VDQ0VTU3xDb21wbGV 0ZWQgVXNpbmcgQ2FsbGJhY2siLCJwYXltZW50X3NvdXJjZSI6InBheXUiLCJQR19UWVBF IjoiQlFSIiwiZXJyb3IiOiJFMDAwIiwiZXJyb3JfTWVzc2FnZSI6Ik5vIEVycm9yIiwibmV0X2Ftb3V udF9kZWJpdCI6MCwidW5tYXBwZWRzdGF0dXMiOiJkcm9wcGVkIiwiaGFzaCI6IjgxMWI0N TE2ODdhYzE5NjYwMTk4MjVjYjgzMjMzMGMyNzMyNjk5MTI0YzI4MWU5NGJlMDE1ODJh OTRiMzc5OWJkMjYyNWZlY2Y2NzViNDA4ZWUzZDk1ZTU4NzZmZTgyNWFmMDJlMGNh M2IzNGRlMDY1NzViMDJmMDgxZjg0OTllIiwiYmFua19yZWZfbm8iOiI5MTM3Mzk5NTMyMT YiLCJiYW5rX3JlZl9udW0iOiI5MTM3Mzk5NTMyMTYiLCJiYW5rY29kZSI6IlVQSUJRUiIsInN1 cmwiOm51bGwsImN1cmwiOm51bGwsImZ1cmwiOm51bGwsIm1lQ29kZSI6IntcInBnTWVyY 2hhbnRJZFwiOlwiSERGQzAwMDAwMDA2MTg5OFwifSJ9"
Using Regex :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string pattern = #"\{(?'bracket'.*)\}";
string input = "a:3:{s:6:\"status\";i:1;s:3:\"msg\";s:39:\"Transaction has been already Completed.\";s:6:\"result\";s:1620:\"eyJtaWhwYXlpZCI6NDAzOTkzNzE1NTExODQwODY3LCJ tb2RlIjoiREJRUiIsInN0YXR1cyI6ImZhaWx1cmUiLCJrZXkiOiI3a2h6VEciLCJ0eG5pZCI6IjEy MzQ1IiwiYW1vdW50IjoiMS4wMCIsImFkZGVkb24iOiIyMDE5LTA1LTE3IDE3OjEwOjEzIiwic HJvZHVjdG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wMTk4MjVjYjgzMjMzMGMyNzMyNjk5MTI0YzI4MWU5NGJlMDE1ODJh OTRiMzc5OWJkMjYyNWZlY2Y2NzViNDA4ZWUzZDk1ZTU4NzZmZTgyNWFmMDJlMGNh M2IzNGRlMDY1NzViMDJmMDgxZjg0OTllIiwiYmFua19yZWZfbm8iOiI5MTM3Mzk5NTMyMT YiLCJiYW5rX3JlZl9udW0iOiI5MTM3Mzk5NTMyMTYiLCJiYW5rY29kZSI6IlVQSUJRUiIsInN1 cmwiOm51bGwsImN1cmwiOm51bGwsImZ1cmwiOm51bGwsIm1lQ29kZSI6IntcInBnTWVyY 2hhbnRJZFwiOlwiSERGQzAwMDAwMDA2MTg5OFwifSJ9\";}";
string insideBrackets = Regex.Match(input, pattern).Groups["bracket"].Value;
string[] messages = insideBrackets.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
string output = messages.Where(x => x.StartsWith("s:1620:")).First();
}
}
}
I'm trying to title case some text that may contain html escape characters. Is there any way of doing this other than with regular expressions? Here's some example code:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string input = "B&G fried pie";
string output = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(input.ToLowerInvariant());
Console.WriteLine(output); //Should be B&G Fried Pie
Console.ReadKey();
}
}
}
Another way I can think of is to replace & with &, do my title case, then replace the & with &.
You can use the System.Web.HttpUtility class to decode and encode html strings, so your code would then look something like:
private static string ToTitleCase(string input)
{
return input == null
? null
: HttpUtility.HtmlEncode(CultureInfo.InvariantCulture.TextInfo
.ToTitleCase(HttpUtility.HtmlDecode(input.ToLowerInvariant())));
}
And in use it would look something like:
Console.WriteLine(ToTitleCase("B&G fried pie"));
Sorry for the title, I had a hard time trying to summarize this.
I'd like to replace an unknown number of instances of a string with a wrapper. So I'd like to replace this:
Test with two:\t\t tab characters
With this:
Test with two:<span class="" style="white-space:pre">\t\t</span> tab characters
There could be any number of \t characters in the given string, and in multiple locations.
The reason I'm trying to do this is our software uses Chilkat to sent emails, and if HTML content contains tabs, these are not shown at the receiving end. When sent, we use \t to represent a tab, and when viewing the source of the received email, the tabs are there, but outside the source they are not:
Outside Source:
Source:
I tested with GMail, and this wraps the tabs:
I understand this maybe be a Chilkat issue, but I can't find much help on the topic, but if I can get around it as above, I'm willing to try it.
Use this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string input = "Test with two:\t\t tab characters";
string output = Regex.Replace(input, #"(\t+)", "<span class=\"\" style=\"white-space:pre\">$1</span>");
}
}
}
This is what I ended up using. The capture group was picking up each repetition individually, and then the replace was only replacing the first found instance, not the entire capture group
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string input = "Test with two:\t\t tab characters";
string output = Regex.Replace(input, #"(\t)+", "<span class=\"\" style=\"white-space:pre\">$&</span>");
}
}
}
I'm working on a Windows application where I need to use clipboard data. I am trying to copy text from clipboard by the code below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace MultiValuedClipBoard
{
class Class1
{
public String SwapClipboardHtmlText(String replacementHtmlText)
{
String returnHtmlText = "hello";
if (Clipboard.ContainsText(TextDataFormat.Html))
{
returnHtmlText = Clipboard.GetText(TextDataFormat.Html);
Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
}
return returnHtmlText;
}
}
}
Calling the above function by:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace MultiValuedClipBoard
{
class Program
{
static void Main(string[] args)
{
Class1 aas = new Class1();
string a = aas.SwapClipboardHtmlText("chetan");
Console.WriteLine(a);
Console.ReadLine();
}
}
}
When running this code it gives the output "Hello" which is the default value, not clipboard data.
Your code will not work because of two reasons:
[1] When you say:
if (Clipboard.ContainsText(TextDataFormat.Html))
Here you are basically assuming that the clipboard already contains a text and that too in HTML format, but depending on the values you are setting in the clipboard it doesn't look like you are intending to use the pre-existing clipboard value anywhere in your program. So, this if condition should not be there.
[2] Secondly, you are further trying to set the string "chetan" to the clipboard which is definitely not in HTML format. So,
Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
becomes
Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);
Hence, effectively, your new code becomes something like this:
String returnHtmlText = "hello";
//if (Clipboard.ContainsText(TextDataFormat.Html))
//{
returnHtmlText = Clipboard.GetText(TextDataFormat.Text);
Clipboard.SetText(replacementHtmlText, TextDataFormat.Text);
//}
return returnHtmlText;
Clearly Clipboard.ContainsText(TextDataFormat.Html) evaluates to false. Which means that the clipboard in fact does not contain text in the format you specify.
I changed your program to prove the point:
[STAThread]
static void Main(string[] args)
{
Clipboard.SetText("boo yah!", TextDataFormat.Html);
Class1 aas = new Class1();
string a = aas.SwapClipboardHtmlText("chetan");
Console.WriteLine(a);
Console.WriteLine(Clipboard.GetText(TextDataFormat.Html));
Console.ReadLine();
}
Output:
boo yah!
chetan
I'm trying to make a program packer but i always fail because when i concat three strings(one contains prefix of source, one contains executable content, other contains suffix of source) content overflows into suffix. Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.IO;
using System.IO.Compression;
namespace ProgramPacker
{
public partial class Form1 : Form
{
static string prefix = #"using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ProgramPacker
{
class Program
{
static string inside = #" + "\"";
static string suffix = "\";\n" + #"static void Main(string[] args)
{
string temp = Path.GetRandomFileName() +" + "\"" + #".exe" + "\"" + #";
BinaryWriter sw = new BinaryWriter(new FileStream(temp, FileMode.Create));
sw.Write(inside);
sw.Flush();
sw.Close();
System.Diagnostics.Process.Start(temp);
}
}
}";
public string code = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
BinaryReader br = new BinaryReader(new FileStream(openFileDialog1.FileName, FileMode.Open));
byte[] data = new byte[br.BaseStream.Length];
br.Read(data, 0, (int)br.BaseStream.Length);
br.Close();
string inside = Encoding.UTF7.GetString(data);
code = string.Concat(prefix, string.Concat(inside, suffix));
}
private void button2_Click(object sender, EventArgs e)
{
Console.Write(code);
CSharpCodeProvider cs = new CSharpCodeProvider();
ICodeCompiler compile = cs.CreateCompiler();
CompilerParameters param = new CompilerParameters();
param.GenerateInMemory = false;
param.ReferencedAssemblies.Add("mscorlib.dll");
param.ReferencedAssemblies.Add("System.dll");
param.ReferencedAssemblies.Add("System.Core.dll");
param.GenerateExecutable = true;
param.OutputAssembly = Environment.CurrentDirectory + "/a.exe";
param.WarningLevel = 4;
CompilerResults comp = compile.CompileAssemblyFromSource(param, code);
foreach (CompilerError error in comp.Errors)
{
MessageBox.Show(error.Line + " " + error.Column + " " + error.ErrorText);
}
MessageBox.Show(comp.PathToAssembly);
MessageBox.Show("Finish!");
}
}
}
It outputs:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ProgramPacker
{
class Program
{
static string inside = #"?ãÎoüoy±ôãøÿ?ÿQÔh¦Rt!?^ÿyóë=G:Fÿ»??¿Ç/}òKÿ?úõßû3õMù?c·?Äûòëoª?À_Û>LÖïá^öÿ·õ©üüòcÊ?ª??÷Ô?î:^ȯÏäG¶?ù ?ñË?oñëy:ôã7??ò#ÚyN?w¿=?ëÆ÷ëÛßTëºÓ»yßüø]áå{ö/àïªêL©Oÿ;ú5rù% ä¸?1)Ï?¥?y®ÿ]0QL8û×ÖGòów±?øÿèü[?ª~éá¿ÿó£üüJ~ܹ
Êw¡h??/?úçeñÈ_?¿?ü½L^ywü7?,ùÅû¿ß£ó÷w?É~Ç????ê?ÿQÌyç¿??¹Cö×vZ__>?? ûïx?_ü"õ!ì; ;ùõåÇ?ú3*¿ #ÿV?èÿ¿Ë??/½P¶y?â¡??ùñÿÑæä/A¶_ò?v??ÿ?ÿQtÄx÷w?O'N?u$ÿk÷U«yÆò?£.Y?ùw(?ßÔ6ÿ]2ÿeUÆò¿?ضö·ßO~ü#*y£Jæ·Ä w yóYëï´
õ}y¡ä¨ù÷YPdú?Z©Óé¼R?æg?ÀSyà쬽ÉÿûÁ?ym>ut?ÿA??>?¿[ôçF ê´)9ß9xÿ£¿?
åùïyí ÁTlù
-ùñ?Òÿ·!·
E?£üO5÷]çºy?7Áè¿?Zô?(å§
#ü¥úõ®?o«âû]0Èßä7¿à??¿õ?d?ѵ;Jü_K?úAMìT>Àü1mR0sâ¿ê¨õ{äö×ó??¿.úÿõ½Eä?ÔïÿÎÿ±³v'ÿûìw ?»oÃø1±WtB
É¡wì&øuå£Îÿ?Éw?|úWÿ=ö÷_ÿ·?y®YWöwû¿ßß?Öe'û?%?yß/×Àî-yÿ.?8åóù?ûÇÚ6ÿÂßøï?Áàï?{8Zy?¿Å5yø ÇpÏ9=ó»üÑ>Õ?Èÿ?Y¿,?ëÿ?Êçÿì_}E?÷Ûú|î$ò÷ø}Jÿ^Êtò¿?#á|ò{Ø`<ªÿQ?QLæ½õo)¿ü&¿áÂÿ¼ó¿_÷7yMå8)¿Ùo'¿õ±üü=~#LÚ¿ó§¢Ë÷üO?Æ?å÷WJÍ$¾¸øW?A?Òÿ(09Áÿà³_ëÿù3l?SWYµßô÷¤~?ÀG¿ñ
??Às%ÍyÑ;Ò¸!ÿ?_Iÿ¼¼í/Ôrö
?T´ôÑùyǹßE~üV?ñ¿C~VßùuÿG X8|._¼Å_:Æü-?îü:?øì?|W ¤ÃÓW01?$å×?Û#ùIÿû·1:4û½ü._y^òÙNó?º1?ßIÍ?ÿ¿ß_~|ÖùX?#?në_B?¡µ~_×ãÿhãèïØÏßì¡yïôk_y;HÿÛyÿàâ/sÀ??? $ìw0Jüßü«#?ûo?¿îËX
´úÏ?Æ?Y>øçÿFpyÅÿèËöyK¡²Ì`}ù½Ò¿íÿÀñn"?Í?ëOëi»ò¹Î~çÿ?_òüîÿh£ôáêDøÏÿ¶?ç·fy ÄúçÿÖÿ?yÿ?¿?ã0ÿ¿Ë÷åå?ÿQ|ü/ü9¿?÷×Hè×ÉÏ#õ??æ?ÿâ?x?ß?5µ}~Øy¾ß^øè{¿
y{íü;Òÿ¿óú7¤¡î?» yû?ÀÓøiP?ÿÀ_ôK?Gëªíá#??ú%íwS×ñ¿¨#Û¦ÿÿÛªy\]¼÷Z~yR~ü¶ÿ£hû???ÇãÿQVèÿ©?÷{O?Gë¬ ??¡ø\«L` æcëí¿ ½?»?ü?oë¼Ätÿ>ò÷£_¯ÖiÄ?¯ÿÓ¿÷Ï?(c¦äàéûèó?ü£´çÕè4üy_
r?qÆ¿ø'?KÿÀ_ó×xùkÔ¿FõkÌ~õ¯1y5Ú_ã'??n~âר~å¯ñkü»¿Æø×رÿÿ5~?_ã7ø5~Í_ã?Z4ÔrñkL~ò׸?5Òßûÿ??ñïñnQ¦?yYÕò³vÇ;¥ùrZÍ?åÅg}õæÙöÁGiÓfËYVVËü³®óæ£ßãè7NgM?/&åuJrustInfo>
</assembly>
;
}
}
}
Any help?
EDIT: Why is everyone down-voting? I just asked a question.
Don't use bare UTF7 or UTF8 strings, use Base64 encoding instead.
// given: byte[] data = new byte[...]
string inside = System.Convert.ToBase64String(data);
code = string.Concat(prefix, string.Concat(inside, suffix));
// in your target code
sw.Write(System.Convert.FromBase64String(inside));
You've run into a problem of representation, one that you're not likely to fix with your code as-is.
However, you can choose to follow a similar approach with just minor modifications (which don't actually compress your program much at all).
Remove the # from the definition of the string inside. This is one of the causes of your problems.
You can't just put a BELL character or NUL character in-line in your string, instead write out their unicode escape sequences:
string inside = String.Concat(
data.Select(b => String.Format(#"\u{0:X4}", b)));
Now, in your suffix code, reinterpret your inside string as characters which you cast to bytes:
sw.Write(inside.Select(c => (byte)c).ToArray()); // hardly efficient
I was able to use these modifications and successfully "pack" and execute the following:
C:\temp>type hello.cs
using System;
class M {
static void Main(string[] args) {
System.IO.File.Create("hello.world");
}
}
C:\temp>csc hello.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.
C:\temp>pack.exe hello.exe
3584 bytes
C:\temp>a.exe
C:\temp>dir *.world
Volume in drive C is OSDisk
Volume Serial Number is AABD-D663
Directory of C:\temp
03/22/2012 16:36 0 hello.world
1 File(s) 0 bytes
0 Dir(s) 279,351,762,944 bytes free
Better use CodeCompileUnit to generate C# code from a C# program:
http://msdn.microsoft.com/en-us/library/system.codedom.codecompileunit.aspx
You can also use this to compile the generated ATS into an assembley.
Or parse your template parts from files. That would make the code much more readable.
You can't just express executable binary code as a C# string without escaping it. At least you need to replace any occurrences of the double-quote character ('"') with a sequence of two double-quote characters. I would be very surprised if that's the only problem you encounter, however.
Note that the string might contain control characters that cause the screen to display the string in a garbled way, but that wouldn't necessarily cause the code containing that string to compile improperly. For example, if you have a verbatim string containing a backspace ("stac{backspace}koverflow", say), the character after the backspace would overwrite the character before the backspace, so viewing the string on the screen would give an inaccurate representation of its contents ("stakoverflow"). The compiler would presumably see the full 14-character string including the backspace.