hex_md5 implementation [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there an identical hex_md5 algorithm in C# to this: http://pajhome.org.uk/crypt/md5/md5.html
I've looked at the RFC 1321, but I wasn't sure if this was a custom implementation based on RFC 1321. I need it to be identical i.e. the value that hex_md5 would return in the JavaScript implementation will be the same in the C# implementation (if one exists).

This is a fairly typical implementation that should match the library's:
string dataToHash = "aaa";
byte[] dataToHashBytes = System.Text.Encoding.ASCII.GetBytes(dataToHash);
using (var md5 = MD5.Create())
{
var hashed = md5.ComputeHash(dataToHashBytes);
Console.WriteLine(BitConverter.ToString(hashed).Replace("-", ""));
}
The C# code produces 47BCE5C74F589F4867DBD57E9CA9F808. This is fairly trivial code, it takes a string, "aaa", converts it to a a byte representation using the ASCII encoding, then hashes it. Finally, we covert it to hexadecimal.
And here is a JSFiddle from the library producing the same thing: http://jsfiddle.net/QC57K/

Related

Writing a recursive function in C# to create a string from characters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Currently, I have something like the following:
public char functName(int n)
{
some functionality....
if(condition1)
return convertFuncToChar(variable) + functName(modifiedNumber);
else
return convertFuncToChar(variable);
}
however, I realize that doesn't give me a string (and the syntax shows that there's an error).
I know that for c++, I would most likely use char* to initialize the function, but this is C#.
I don't think it works if I initialize with String either.
Assuming I understand the question correctly, you can take advantage of two things:
1) all basic types implement .ToString(), which creates a string for you
2) string implements operator+()
The following is an example of a recursive function that will recursively concatenate characters to create a string. The output is the number in reverse as a string.
static string funcName(int n)
{
if (n<10)
return (n%10).ToString();
return (n%10).ToString() + funcName(n/10);
}
Of course, it would be more efficient to write this non-recursively.

Regex to shrink Path.Combine Relative to Absolute [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 2 file paths:
absolute:
/Content/assets2/otherfolder/another/this/
relative:
../../../../assets/img/logo.gif
when I do Path.Combine(absolute, relative) I get:
/Content/assets2/otherfolder/another/this/../../../../assets/img/logo.gif
which works fine, but what I want to get:
/Content/assets/img/logo.gif
I need a Regex or code that removes the "../" with the corresponding folder:
/Content/assets2/otherfolder/another/this/../../../../assets/img/logo.gif
/Content/assets2/otherfolder/another/../../../assets/img/logo.gif
/Content/assets2/otherfolder/../../assets/img/logo.gif
/Content/assets2/../assets/img/logo.gif
finally into:
/Content/assets/img/logo.gif
You don't need a Regex for that. Actually, I think it would be impossible to define in a reliable way in Regex. Instead, use Path.GetFullPath:
string combined = Path.Combine(path1, path2);
string prettyPath = Path.GetFullPath(combined);

c# Regular Expression for the text below? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have strings of array.
One of the strings:
myText = "[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan]"
The [Date].[Hierarchy].[Year].& part is always the same, but the rest is changing depending on the today date.
So it would be for today like [Date].[Hierarchy].[Year].&[2013].&[Q3].&[Jul], but in the array I also have other strings that like [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan].&[20].
The regex should opt those out also.
I am only interested in strings from array like [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan]", [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Feb], [Date].[Hierarchy].[Year].&[2005].&[Q1].&[Mar], ....
I hope you get the pattern.
This Regex should do it. The year is currently set to 4 digits, if you only want it to match 2005, replace the \d{4} bit.
^\[Date\]\.\[Hierarchy\]\.\[Year\]\.&\[\d{4}\]\.&\[Q1\]\.&\[[A-Z-a-z]{3}\]$
Here's the result:
[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan] // matches
[Date].[Hierarchy].[Year].&[2013].&[Q3].&[Jul] // no match
[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan].&[20] // no match
Edit to your comment: Make sure you put an # before the string declaration.
var pattern = new Regex(#"^\[Date\]\.\[Hierarchy\]\.\[Year\]\.&\[\d{4}\]\.&\[Q1\]\.&\[[A-Z-a-z]{3}\]$");
var matches = pattern.IsMatch("[Date].[Hierarchy].[Year].&[2005].&[Q1].&[Jan]");

How can I extract a string using C# Substring [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to extract the browser name and Operating system in differant strings from
this string. How can I use substring to extract them from
{ Browser/leavethis (Operating System) Leavethis/Leavethis (Leavethis, Leavethis) Leavethis/Leavethis/Leavethis }
Instead of splitting the useragent, you should use the properties already supplied the Asp.net Framework to detect the browser/os.
Use this
to get the browser name Request.Browser.Browser and browser version Request.Browser.MajorVersion
to get the Os name Request.Browser.Platform and os version Request.UserAgent
A regex such as the following should work, but I suspect #bastos.sergio's answer is the correct way to get what you need.
Regex r = new Regex(#"{\s+(?<Browser>\w+)/[^(]+\((?<OperatingSystem>[^)]+).+");
string browser;
string OS;
var match = r.Match(s);
if (match.Success)
{
browser = match.Groups["Browser"].Value;
OS = match.Groups["OperatingSystem"].Value;
}

How to get a random word from a given list of words [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
my string array has got a list of words like man, ran etc. Can somebody help me to randomly pick up a word from my string array and store it in a variable myword.
Random r = new Random();
string[] words = {"man", "rat", "cow", "chicken"};
Console.WriteLine(words[r.Next(0, words.Length)]);
or
string word = words[r.Next(0, words.Length)];
Be careful when using Random, though. This is a pseudorandom number generator so you may not get the expected results if you use it incorrectly.

Categories

Resources