String to number generator - c#

I m looking for a way to convert a string to a unique Id.
Ideas invited for an algorithm that comes up with a unique number for each string sent to it.
I tried to use hash code but then realized that two strings could have the same hash code.
How do I generate a unique code for each string as input and two same strings should generate me the same id at all times.

Can you have characters in your "unique ID"? If so, this should work ;-)
public string MakeUnique(string s)
{
return s;
}
All ID's will be unique to the value provided. The same string, will produce the exact same ID. That's what you wanted right?
If it's an integer result you want, how about converting each character to an int...
public int MakeUnique(string s)
{
string result = "";
foreach(var c in s)
{
result += ((int)c).ToString();
}
return Int.Parse(result);
}
WARNING: This will break if the string is too big

Simply append or prepend a guid:
string foo = "MyString";
//Simply throw it on the end
string uniqueString = foo + Guid.NewGuid();
//Prepend with underscore
string uniqueString = String.Format("{0}_{1}", foo, Guid.NewGuid());
//Append with underscore
string uniqueString = String.Format("{0}_{1}", Guid.NewGuid(), foo);
Edit (new requirement)
You have not provided enough information for me to post a great answer to this question. For example, environment (web, winforms, etc.) would be beneficial.
To point you in the right direction...
If the unique string that is returned needs to be the same when you pass in a string a second time, you could maintain a history of generated strings and check it each time generation is requested.
Truthfully, there are lots of ways to skin this cat...

If the original string is sensitive, similar to Gravatar, you could encrypt the string with MD5 encryption
As you have stated, and #Austin Salonen commented, they're not 100% unique, but the risk is low:
How are hash functions like MD5 unique?

A string can be fairly long and consists of charactes, which are 16-bit values. The number of possible strings is huge (much more than the range of integer or Guid). So you can't have a function that "just" translates a string into some guaranteed unique code, without some help.
You could use a database table: lookup your string in the table. If it's not there, insert it, generating a new (sequential) unique id. If it's there, use that id. The number of possible strings is huge, the number of strings you encounter, probably not.

Related

Read input with different datatypes and space seperation

I'm trying to figure out how to write code to let the user input three values (string, int, int) in one line with space to separate the values.
I thought of doing it with String.Split Method but that only works if all the values have the same datatype.
How can I do it with different datatypes?
For example:
The user might want to input
Hello 23 54
I'm using console application C#
Well the first problem is that you need to decide whether the text the user enters itself can contain spaces. For example, is the following allowed?
Hello World, it's me 08 15
In that case, String.Split will not really be helpful.
What I'd try is using a regular expression. The following may serve as a starting point:
Match m = Regex.Match(input, #"^(?<text>.+) (?<num1>(\+|\-)?\d+) (?<num2>(\+|\-)?\d+)$");
if (m.Success)
{
string stringValue = m.Groups["text"].Value;
int num1 = Convert.ToInt32(m.Groups["num1"].Value);
int num2 = Convert.ToInt32(m.Groups["num2"].Value);
}
BTW: The following part of your question makes me frown:
I thought of doing it with String.Split Method but that only works if all the values have the same datatype.
A string is always just a string. Whether it contains a text, your email-address or your bank account balance. It is always just a series of characters. The notion that the string contains a number is just your interpretation!
So from a program's point of view, the string you gave is a series of characters. And for splitting that it doesn't matter at all what the real semantics of the content are.
That's why the splitting part is separate from the conversion part. You need to tell your application that that the first part is a string, the second and third parts however are supposed to be numbers. That's what you need type conversions for.
You are confusing things. A string is either null, empty or contains a sequence of characters. It never contains other data types. However, it might contain parts that could be interpreted as numbers, dates, colors etc... (but they are still strings). "123" is not an int! It is a string containing a number.
In order to extract these pieces you need to do two things:
Split the string into several string parts.
Convert string parts that are supposed to represent whole numbers into a the int type (=System.Int32).
string input = "Abc 123 456"
string[] parts = input.Split(); //Whitespaces are assumed as separators by default.
if (parts.Count == 3) {
Console.WriteLine("The text is \"{0}\"", parts[0]);
int n1;
if (Int32.TryParse(parts[1], out n1)) {
Console.WriteLine("The 1st number is {0}", n1);
} else {
Console.WriteLine("The second part is supposed to be a whole number.");
}
int n2;
if (Int32.TryParse(parts[2], out n2)) {
Console.WriteLine("The 2nd number is {0}", n2);
} else {
Console.WriteLine("The third part is supposed to be a whole number.");
}
} else {
Console.WriteLine("You must enter three parts separated by a space.");
}
What you have to do is get "Hello 23 54" in a string variable. Split by " " and treat them.
string value = "Hello 23 54";
var listValues = value.Split(' ').ToList();
After that you have to parse each item from listValues to your related types.
Hope it helps. ;)

Extract first numeric chars from a string in C#

I need to extract first numeric value set from a string. Here (Link Here), I have found a RegEx way to do that. But, in my case I have a LINQ query from where I need to do the same logic.
Here is my exisiting Logic
bool Isbn = db.BibContents.Any(ad => ad.NormValue == ISBN); // I need to do the numeric split logic into the db column NormValue
Note
I cannot loop here to get values first and compare in the loop. Because, I have huge number of records in DB and NormValue Column is nvarchar(max) typed.
Any help to this will be appreciated.
Thanks
How about you parse your result to string? I don't know that much about LINQ but I would parse the result into String and use regex.
bool Isbn = db.BibContents.Any(ad => GetDigits(ad.NormValue) == ISBN);
public string GetDigits(string text) {
return string.Join("",text.AsEnumerable().Where(char.IsDigit));
}
How about that?

How to remove a string from a string

I am adding a string (a) to another string (b) before I store it in DB, when I retrieve the value from DB, I want to remove the string(b) from string (a). string (b) is a constant. How can I do it
string a= "text1";
string b="text2";
string c = a+b;
I want to remove b from c after I retrive it from db
c = c.Replace(b, "");
Would be a simple way to do so.
Rather than do any of that, create a computed column in the DB that has the extra text.
Less storage; less code.
Try String.Replace - MSDN documentation here.
As #SvenS has pointed in #Khaled Nassar answer, using String.Replace won't work "as is" in your situation.
One acceptable solution may #Mitch's one, but if you don't have that access to modify your database, maybe there's another solution in pure C#:
int indexOfB = c.LastIndexOf(b);
string cWithoutB = c;
if(indexOfB >= 0)
{
c.Substring(0, indexOfB);
}
This prevents replacing more than once the same string as b, because who knows if some user save the same text as b and logic shouldn't be removing it if it's not the one predefined by your application.

c#: Convert hex to GUID

Is there any way in C# to convert HEX to GUID?
Example:
I want to create a GUID with value equal to
0xa145ce546fe5bbcf1745491b50a4233d19b8223c0a743cad6847142df8b63821640beeabe82824b7d2bf507cb487
If you know it's a valid GUID in one of these formats:
dddddddddddddddddddddddddddddddd
dddddddd-dddd-dddd-dddd-dddddddddddd
{dddddddd-dddd-dddd-dddd-dddddddddddd}
(dddddddd-dddd-dddd-dddd-dddddddddddd)
{0xdddddddd, 0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}
Then new Guid(hexstring).
If you don't know for sure then with .NET4.0 you can use:
Guid g = default(Guid);
bool success = Guid.TryParse(hexstring, out g);
Otherwise you'll have to wrap the first in a try block, or check the format yourself first (e.g. with a Regex).
Edit:
Your edited question can't be done, you can't fit a quart into a pint-glass. There's enough bits of information in that for nearly 3 Guids.
The Guid(string) constructor can parse string with GUIDs in several formats, e.g.:
string hex = Guid.NewGuid().ToString("N");
// hex == "ca761232ed4211cebacd00aa0057b223"
Guid guid = new Guid(hex);
See also: Parse, ParseExact, TryParse, TryParseExact
There are several constructors for Guid that you could use as well as Parse and ParseExact if you have a hex string.
EDIT: Given your edit, you could use a BigInteger but without knowing why you want a Guid, it's hard to give a better answer.
//untested
var bytes = new byte[] {Oxa,1,4,5,Oxc,Oxe,5,4,6,Oxf,Oxe,5,Oxb,Oxb,Oxc,Oxf,1,7,4,5,4,9,1,Oxb,5,0,Oxa,4,2,3,3,Oxd,1,9,Oxb,8,2,2,3,Oxc,0,Oxa,7,4,3,Oxc,Oxa,Oxd,6,8,4,7,14,2,Oxd,Oxf,8,Oxb,6,3,8,2,1,6,4,0,Oxb,Oxe,Oxe,Oxa,Oxb,Oxe,8,2,8,2,4,Oxb,7,Oxd,2,Oxb,Oxf,5,0,7,Oxc,Oxb,4,8,7};
var bigInteger = new BigInteger(bytes);
I suspect the following should do the trick:
Guid g = new Guid(str); // Where str is the hex string
Of course you will need a try catch block around it in case str isn't well formed.

Retrieve string from the middle of a string

I have a coded string that I'd like to retrieve a value from. I realize that I can do some string manipulation (IndexOf, LastIndexOf, etc.) to pull out 12_35_55_219 from the below string but I was wondering if there was a cleaner way of doing so.
"AddedProject[12_35_55_219]0"
If you can be sure of the format of the string, then several possibilities exist:
My favorite is to create a very simple tokenizer:
string[] arrParts = yourString.Split( "[]".ToCharArray() );
Since there is a regular format to the string, arrParts will have three entries, and the part you're interested in would be arrParts[1].
If the string format varies, then you will have to use other techniques.
So in summary, if you have a pattern that you can apply to your string, the easiest is to use regular expressions, as per Guffa example.
On the other hand you have different tokens all the time to define the start and end of your string, then you should use the IndexOf and LastIndexOf combination and pass the tokens as a parameter, making the example from Fredrik a bit more generic:
string GetMiddleString(string input, string firsttoken, string lasttoken)
{
int pos1 = input.IndexOf(firsttoken) + 1;
int pos2 = input.IndexOf(lasttoken);
string result = input.Substring(pos1 , pos2 - pos1);
return result
}
And this is assuming that your tokens only happens one time in the string.
That depends on how much the string can vary. You can for example use a regular expression:
string input = "AddedProject[12_35_55_219]0";
string part = Regex.Match(input, #"\[[\d_]+\]").Captures[0].Value;
There are two methods which you may find useful, there is IndexOf and LastIndexOf with the square brackets as your parameters. With a little bit of research, you should be able to pull out the project number.
Here is a improvement from Wagner Silveira's GetMiddleString
string GetMiddleString(string input, string firsttoken, string lasttoken)
{
int pos1 = input.ToLower().IndexOf(firsttoken.ToLower()) + firsttoken.Length;
int pos2 = input.ToLower().IndexOf(lasttoken.ToLower());
return input.Substring(pos1 , pos2 - pos1);
}
And here how you use it
string data = "AddedProject[12_35_55_219]0";
string[] split = data.Split("[]".ToCharArray());
rtbHelp.Text += GetMiddleString(data, split[0], split[2]).Trim("[]".ToCharArray());//print it to my C# winForm RichTextBox Help

Categories

Resources