How to convert alphanumeric value to int in c# - c#

I have alphanumeric value = 106bd87f9386b63b. I want to convert this value into integer in c# and store the converted value into database. Is there any possibilities to convert it.

Did you try to use this:
the strLong is a declared variable.
and see this for your reference: https://learn.microsoft.com/en-us/dotnet/api/system.globalization.numberstyles?view=netframework-4.7.2
long.Parse(strLong, System.Globalization.NumberStyles.HexNumber);

You can do it with multiple methods choose one which is suitable to you
Method 1:
If you want to do it with Regex Here is Example helpful in Detail
C# Regular Expression to match letters, numbers and underscore
Here is also a useful and detailed link
Find and extract a number from a string
Other Methods:
You can also use Char.IsNumber('your character here')
here is Doc link of Microsoft
https://learn.microsoft.com/en-us/dotnet/api/system.char.isnumber?view=netframework-4.7.2
For the above method you have to split your string into characters and then check if it is character or not using Char.isNumber() Method .
Instead of all these you can also split this and make a function and match ASCII values and detect weather it is a number of not .
What ever method you use just detect number or not and concatinate it into a string and then at the end convert this string into a number using Convert class provided by Microsoft . Here is the detailed link of conversion
How can I convert String to Int?

Related

C# IndexOf in special persian character

in persian/arabic character, some character used optional on top or bottom of other character like ِ َ ّ ُ.
in my example if i use this character, indexOf not found my word. consider that persian/arabic is rtl language.
for example:
منّم => م + ن + ّ + م
C#:
"منّم".IndexOf("من");
return -1
javascript:
var index= ' منّم '.indexOf('من');
console.log(index);
what happened in C#. anyone can explain this?
By passing in StringComparison.Ordinal as an argument to the overloaded String.IndexOf(), you could have also done the following:
"منّم".IndexOf("من", StringComparison.Ordinal); // returns 0
Specifying CompareOptions.Ordinal as an option should work, together with the IndexOf method of CompareInfo.
CompareInfo info = CultureInfo.CurrentCulture.CompareInfo;
string str = "منّم";
Console.WriteLine(info.IndexOf(str, "من", CompareOptions.Ordinal));
Output is 0.
DotNetFiddle if you want to try it yourself.
You should learn about the different methods that .Net uses to compare/match strings.
Best Practices for Using Strings in .NET
Some overloads with default parameters (those that search for a Char
in the string instance) perform an ordinal comparison, whereas others
(those that search for a string in the string instance) are
culture-sensitive. It is difficult to remember which method uses which
default value, and easy to confuse the overloads.
The section String Operations that Use the Invariant Culture gives a short explanation about combining characters.

replace items in a string while also confirming format

I have a string input in the format of "string#int" and I want to convert it to "string-int" for web friendliness reasons for an api i am using.
To do this I could obviously just replace the single character # with a - using string.replace, but ideally I'd like to do a check that the input (which is user provided by the way) is in the correct format (string#int) while or before converting to the web friendly version with a "-" instead. Essentially I'm wondering if there is a method in C# that I could use to check that this input is in the correct format and convert it to the required result format.
There is no built-in way obviously, since the format you request is quite specific. Also, a string can contain anything, also a hastag, #, so I guess you need to narrow that down.
You could use regular expressions to check if the string is in the correct format. This would be possible expression:
[A-Za-z ]+#[0-9]+
Which matches for:
this is a string#123
There's nothing built in, but you could do the following:
var parts = input.Split(new char[] { '#' });
if (parts.Length != 2) incorrect format
int result;
if (!int.TryParse(parts[1], out result) incorrect format
output = String.Join("-", parts);
This takes the input and splits it on the "#" character. If the result isn't two parts then the string is invalid. You then check that the second part is an integer - if the TryParse fails it's not valid. The last step is to rejoin the two parts, but this time with a - as the separator.

Format int with negative number

I have file that match this pattern:
Name-003
Name-002
Name-001
Name0000
Name0001
Name0002
And I need a way to format a string to have this pattern.
If I use
string.Format("Name{0:0000}", index)
It give me:
Name-0003
Name-0002
Name-0001
Name0000
Name0001
Name0002
I need a way to specify the number of digit including the negative sign character.
I want a solution with String.Format()
You could look at the documentation for numeric formats (specifically the ; character):
string.Format("Name{0:0000;-000}", index)
... or you could learn how to use an if statement to select between two format strings based on the value of index.

Convert string to char

I get from another class string that must be converted to char. It usually contains only one char and that's not a problem. But control chars i receive like '\\n' or '\\t'.
Is there standard methods to convert this to endline or tab char or i need to parse it myself?
edit:
Sorry, parser eat one slash. I receive '\\t'
I assume that you mean that the class that sends you the data is sending you a string like "\n". In that case you have to parse this yourself using:
Char.Parse(returnedChar)
Otherwise you can just cast it to a string like this
(string)returnedChar
New line:
string escapedNewline = #"\\n";
string cleanupNewLine = escapedNewline.Replace(#"\\n", Environment.NewLine);
OR
string cleanupNewLine = escapedNewline.Replace(#"\\n", "\n");
Tab:
string escapedTab = #"\\t";
string cleanupTab= escapedTab.Replace(#"\\t", "\t");
Note the lack of the literal string (i.e. i did not use #"\t" because that will not represent a Tab)
Alternatively you could consider Regular Expressions if you need to replace a range of different string patterns.
You should probably write a utility function to encapsulate the common behaviour above for all the possible Escape Sequences
Then you'd write some Unit Tests to cover each of the cases you can think of.
As you encounter any bugs you add more unit tests to cover those cases.
UPDATE
You could represent a tab in the XML with a special character sequence:
see this article
This article applies to SQL Server but may well be relevant to C# also?
To be absolutely sure, you could try generating a string with a tab in it and putting it into some XML (programmatically) and using XmlSerializer to serialize that to a file to see what the output is, then you can be sure that this will faithfully 'round-trip' the string with the tab still in it.
how about using string.ToCharArray()
You can then add the appropriate logic to process whatever was in the string.
char.parse(string); is used to convert string to char and you can do vice versa
char.tostring();
100% solved

Built in method to get C-style escaped sequence for a string

Is there a built method in .Net for C-style escaping of strings?
For example, I have to convert a string which contains quotes like "hello", and write it as an escaped string \"hello\".
Actually, to be more precise:
string original = "\"hello\"";
should be converted to
string what_i_need = "\\\"hello\\\"";
I could have probably done it myself while writing this question, but I don't want to reinvent hot water.
[Edit] According to the provided answer, this is actually a duplicate of: Can I convert a C# string value to a string literal. It didn't pop out since there were no tags and keywords I was looking for.
I dont think there is any built in methods. But if you have to write your own, Can I convert a C# string value to a string literal post maybe helpful

Categories

Resources