I am retrieving the OEM number from a Windows mobile smart device, and am trying to figure out how to use a returned value in an if statement.
Below is the code I use to return the value. I would like to make sure the OEM number is always 86.09.0008 and if it is not i would like it to let me know.
class oem
{
const string OEM_VERSION = "OEMVersion";
private const int SPI_GETOEMINFO = 258;
private const int MAX_OEM_NAME_LENGTH = 128;
private const int WCHAR_SIZE = 2;
[DllImport("coreDLL.dll")]
public static extern int SystemParametersInfo(int uiAction, int uiParam, string pBuf, int fWinIni);
[DllImport("CAD.dll")]
public static extern int CAD_GetOemVersionNumber(ref System.UInt16 lpwMajor, ref System.UInt16 lpwMinor);
public string getOEMVersion()
{
System.UInt16 nMajor = 0;
System.UInt16 nMinor = 0;
uint nBuild = 0;
int status = CAD_GetOemVersionNumber(ref nMajor, ref nMinor);
if (((System.Convert.ToBoolean(status))))
{
string sMajor = String.Format("{0:00}", nMajor); //in 2-digits
string sMinor = String.Format("{0:00}", nMinor); //in 2-digits
string sBuild = String.Format("{0:0000}", nBuild); //in 4-digits
return (sMajor + "." + sMinor + "." + sBuild);
}
else // failed
{
return ("00.00.0000");
}
I'm calling this from my main form like so:
label1.Text = oemver.getOEMVersion();
if statements require a bool value. In your case you should compare the value you require with the value you obtained
if(status == 86.09.0009)//...
Note the double '==' which is an operator that checks for equality. Contrast this with the single '=' which performs an assignment.
Also note that int does not allow for decimals. Considering that this number has two decimals I believe you need to get this as a string.
well, just do something like this in you "main" :
string myString = getOEMVersion();
if(myString == "86.09.0009")
{//Whatever you're willing to do
}
If I understood your question, you should do this:
oem someOem = new oem();
if (oem.getOEMVersion() == "86.09.0009") {
// ok
} else {
// fail
}
I am not sure what you mean but if I understand you question you want to use the result of the GetOEMVersion method in an ifstatement.
string OEMVersion = getOEMVersion();
if(OEMVersion == "86.09.0009")
{
// Do something
}
else
{
// fail
}
You are missing :
[DllImport("CAD.dll")]
public static extern int CAD_GetOemBuildNumber(ref uint lpdwBuild);
int build = CAD_GetOemBuildNumber(ref nBuild);
to get the build.
Related
So i have a question. I'm trying do make a function witch returns a number, but the problem is that i can't convert int to string. My functions looks like this:
static string EnemyDmg(EnemyDmg _dmg)
{
string result = "";
int EnemyDmg
= CharAttack - EnemyDefense;
if (EnemyDmg < 1)
EnemyDmg = 4;
result = EnemyDmg;
return result;
}
but it should do this
int EnemyDmg
= CharAttack - EnemyDefense;
if (EnemyDmg < 1)
EnemyDmg = 4;
Console.WriteLine(EnemyName + " takes " + EnemyDmg + " Damage");
has anyone an idea?
PS: The 4 is just a random number.
should be static int EnemyDmg(EnemyDmg _dmg). You should return an int, and convert to string outside the function iif you need that
Anyway, to convert a String s into an int i:
int i = Int32.Parse(s);
to convert an int i into a string s
string s = i.ToString();
string s = ""+i; // more "java-like"
This question is a bit ambiguous; I'm not sure why you've done it this way.
You can convert a C# integer to a string with the .ToString() method, like this:
int a = 12;
string aString = a.ToString();
Console.WriteLine(a);
https://dotnetfiddle.net/sMC3hU
static string toStr(int intInput)
{
string str = intInput.ToString();
return str;
}
}
This code will do it for you. There is no need to use if statement as there is no any specific requirement, it will make more complicated code.
or else
you can direct use ToString parameter if there is an user input just refer to the 3rd line.
How can I convert a string like "09335887170" to an integer? Here is what I have tried:
string a = "09335887170";
int myInt;
bool isValid = int.TryParse(a, out myInt); // it returns false in this case
if (isValid)
{
int plusOne = myInt + 1;
MessageBox.Show(plusOne.ToString());
}
MessageBox.Show(a);
int stringToInt = Convert.ToInt32("09335887170"); // it returns nothing with no error
MessageBox.Show((stringToInt + 1).ToString());
int test = int.Parse(a); //it has type convertion error
MessageBox.Show((test + 1).ToString());
The maximum value of a Int32 (or only int) is 2,147,483,647
you need a UInt64 or Int64 or Long
string a = "09335887170";
Int64 myInt;
bool isValid = Int64.TryParse(a, out myInt);
if (isValid)
{
int plusOne = myInt + 1;
MessageBox.Show(plusOne.ToString());
}
result = Convert.ToInt64(value);
Look here for more info.
(Found online, im not really in c#)
that number exceeds the max int value. use a long instead.
Try this
Int64 no = Convert.ToInt64( "09335887170");
My current code:
UInt32 example = Convert.ToUInt32(tb_address.Text, 16);
Problem is if I'm peeking into an address like this:
0x83e3ba3c + 0x15
toUint32 will obviously return an error and say no.
Anyway I could go about having operators handled in it?
Picture of my app to maybe further understand it
Just split the string by using the '+'. After that trim the results, parse them and add them together.
Probably your best option would be to hide the code in a static class. something like this should work:
public static class HexAdder
{
private static UInt32 num1 = 0, num2 = 0;
private static void ParseString(string stringtoadd)
{
string[] temp = {"",""};
try
{
temp = stringtoadd.Split(" +".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
num1 = Convert.ToUInt32(temp[0], 16);
num2 = Convert.ToUInt32(temp[1], 16);
}
catch(Exception e)
{
MessageBox.Show("Invalid String Format\n- Added String = " + stringtoadd + "\n- First Part = " + temp[0] + "\n- Second Part = " + temp[1]);
}
}
public static UInt32 AddedToUint32(string stringtoadd)
{
ParseString(stringtoadd);
return num1 + num2;
}
}
This has basic validation and pops up a messagebox and returns 0 if there is an error.
Using it would look like this:
string test = "0x83e3ba3c + 0x15";
UInt32 example = HexAdder.AddedToUint32(test);
This way adding other operations is a simple matter of adding the appropriate static method(s).
Please tell me how to validate GUID in .net and it is unique for always?
Guid's are unique 99.99999999999999999999999999999999% of the time.
It depends on what you mean by validate?
Code to determine that a Guid string is in fact a Guid, is as follows:
private static Regex isGuid =
new Regex(#"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);
internal static bool IsGuid(string candidate, out Guid output)
{
bool isValid = false;
output = Guid.Empty;
if(candidate != null)
{
if (isGuid.IsMatch(candidate))
{
output=new Guid(candidate);
isValid = true;
}
}
return isValid;
}
2^128 is a very, very large number. It is a billion times larger than the number of picoseconds in the life of the universe. Too large by a long shot to ever validate, the answer is doomed to be "42". Which is the point of using them: you don't have to. If you worry about getting duplicates then you worry for the wrong reason. The odds your machine will be destroyed by a meteor impact are considerably larger.
Duck!
Here's a non-Regex answer that should be pretty fast:
public static bool IsHex(this char c)
{
return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}
public static bool IsGuid(this string s)
{
// Length of a proper GUID, without any surrounding braces.
const int len_without_braces = 36;
// Delimiter for GUID data parts.
const char delim = '-';
// Delimiter positions.
const int d_0 = 8;
const int d_1 = 13;
const int d_2 = 18;
const int d_3 = 23;
// Before Delimiter positions.
const int bd_0 = 7;
const int bd_1 = 12;
const int bd_2 = 17;
const int bd_3 = 22;
if (s == null)
return false;
if (s.Length != len_without_braces)
return false;
if (s[d_0] != delim ||
s[d_1] != delim ||
s[d_2] != delim ||
s[d_3] != delim)
return false;
for (int i = 0;
i < s.Length;
i = i + (i == bd_0 ||
i == bd_1 ||
i == bd_2 ||
i == bd_3
? 2 : 1))
{
if (!IsHex(s[i])) return false;
}
return true;
}
You cannot validate GUID's uniqueness. You just hope it was generated with a tool that produces unique 16 bytes. As for validation, this simple code might work (assuming you are dealing with GUID's string representation:
bool ValidateGuid(string theGuid)
{
try { Guid aG = new Guid(theGuid); }
catch { return false; }
return true;
}
If you're looking for a way to determine if it's the format of the actual .Net Guid type, take a look at this article. A quick regex does the trick.
this question was already discussed in this post. You may find more interesting details
In .net 4, you can use this extension method
public static class GuidExtensions
{
public static bool IsGuid(this string value)
{
Guid output;
return Guid.TryParse(value, out output);
}
}
i wrote a extension for this
public static bool TryParseGuid(this Guid? guidString)
{
if (guidString != null && guidString != Guid.Empty)
{
if (Guid.TryParse(guidString.ToString(), out _))
{
return true;
}
else
return false;
}
return false;
}
How do I convert a string to an integer in C#?
If you're sure it'll parse correctly, use
int.Parse(string)
If you're not, use
int i;
bool success = int.TryParse(string, out i);
Caution! In the case below, i will equal 0, not 10 after the TryParse.
int i = 10;
bool failure = int.TryParse("asdf", out i);
This is because TryParse uses an out parameter, not a ref parameter.
int myInt = System.Convert.ToInt32(myString);
As several others have mentioned, you can also use int.Parse() and int.TryParse().
If you're certain that the string will always be an int:
int myInt = int.Parse(myString);
If you'd like to check whether string is really an int first:
int myInt;
bool isValid = int.TryParse(myString, out myInt); // the out keyword allows the method to essentially "return" a second value
if (isValid)
{
int plusOne = myInt + 1;
}
int a = int.Parse(myString);
or better yet, look into int.TryParse(string)
string varString = "15";
int i = int.Parse(varString);
or
int varI;
string varString = "15";
int.TryParse(varString, out varI);
int.TryParse is safer since if you put something else in varString (for example "fsfdsfs") you would get an exception. By using int.TryParse when string can't be converted into int it will return 0.
Do something like:
var result = Int32.Parse(str);
If you are sure that you have "real" number in your string, or you are comfortable of any exception that might arise, use this.
string s="4";
int a=int.Parse(s);
For some more control over the process, use
string s="maybe 4";
int a;
if (int.TryParse(s, out a)) {
// it's int;
}
else {
// it's no int, and there's no exception;
}
4 techniques are benchmarked here.
The fastest way turned out to be the following:
y = 0;
for (int i = 0; i < s.Length; i++)
y = y * 10 + (s[i] - '0');
"s" is your string that you want converted to an int. This code assumes you won't have any exceptions during the conversion. So if you know your string data will always be some sort of int value, the above code is the best way to go for pure speed.
At the end, "y" will have your int value.
int i;
string whatever;
//Best since no exception raised
int.TryParse(whatever, out i);
//Better use try catch on this one
i = Convert.ToInt32(whatever);
bool result = Int32.TryParse(someString, out someNumeric)
This method will try to convert someString into someNumeric, and return a result depending on whether or not the conversion is successful: true if conversion is successful and false if conversion failed. Take note that this method will not throw an exception if the conversion failed like how Int32.Parse method did and instead returns zero for someNumeric.
For more information, you can read here:
https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
&
How to convert string to integer in C#
int i;
string result = Something;
i = Convert.ToInt32(result);
You can use either,
int i = Convert.ToInt32(myString);
or
int i =int.Parse(myString);
class MyMath
{
public dynamic Sum(dynamic x, dynamic y)
{
return (x+y);
}
}
class Demo
{
static void Main(string[] args)
{
MyMath d = new MyMath();
Console.WriteLine(d.Sum(23.2, 32.2));
}
}