I have one python script which i am trying to convert and stuck in one place and unable to proceed. Please check where ever i have mentioned "Stuck here" in below code. any help would be appreciated
Original Python script:
import hashlib
meid = raw_input("Enter an MEID: ").upper()
s = hashlib.sha1(meid.decode('hex'))
#decode the hex MEID (convert it to binary!)
pesn = "80" + s.hexdigest()[-6:].upper()
#put the last 6 digits of the hash after 80
print "pESN: " + pesn
My C# conversion:
UInt64 EsnDec = 2161133276;
string EsnHex=string.Format("{0:x}", EsnDec);
string m = Convert.ToString(Convert.ToUInt32(EsnHex, 16), 2);
/*---------------------------------------------
Stuck here. Now m got complete binary data
and i need to take last 6 digits as per python
script and prefix "80".
---------------------------------------------*/
Console.WriteLine(m);
Console.Read();
Use String.Substring:
// last 6 characters
string lastsix = m.Substring(m.Length - 6);
Console.WriteLine("80{0}", lastsix);
How about something like this:
static void Main(string[] args)
{
UInt64 EsnDec = 2161133276;
Console.WriteLine(EsnDec);
//Convert to String
string Esn = EsnDec.ToString();
Esn = "80" + Esn.Substring(Esn.Length - 6);
//Convert back to UInt64
EsnDec = Convert.ToUInt64(Esn);
Console.WriteLine(EsnDec);
Console.ReadKey();
}
Related
This question already has answers here:
Need to get a string after a "word" in a string in c#
(7 answers)
Closed 4 years ago.
I'm receiving from my app string of numbers, and I need to get them
but I don't know them
so in the string I have UID:
so search it in the string and then I need to take from the string 9 chars after the word "UID:" in the string
tried some and didn't word well
I just removing what I want and not extract it from the string
string id = txt.Substring(0, txt.LastIndexOf("UID:") + 9);
I know the string I need after UID: always have 9 chars
the out put I need to get
EXAMPLE:
UID: 994zxfa6q
I don't know what is it but I know its only have 9 chars.
I prefer not to have constants and length of constants hardcoded separate from each other. You need to have your starting index be the index of the searched string plus the size of the search string, and then your length should be the size of your id.
var uid = "UID: ";
string id = txt.Substring(txt.IndexOf(uid) + uid.Length, 9);
You definitely had the right idea. Almost had it.
string id = txt.Substring(txt.LastIndexOf("UID: ") + 5, 9);
string GetUID(string input)
{
const int uidLength = 9;
const string uidMarker = "UID: ";
var markerIndex = input.IndexOf(uidMarker);
if(markerIndex==-1 || markerIndex + uidMarker.Length + uidLength > input.Length)
{
throw new ArgumentException("Input does not contain UID", nameof(input));
}
return input.Substring(markerIndex + uidMarker.Length, uidLength);
}
If I understand what you want, you can use this code (or something along those lines). Sorry, may have gotten it wrong as I'm far from PC right now. This code assumes that there is only one "UID:" substring in the input string.
Also String.IndexOf and String.Substring are nicely documented.
Your code is almost correct, but you have to remember that the first parameter of string.SubString is an index. So, you need to change:
string id = txt.Substring(0, txt.LastIndexOf("UID:") + 9);
to:
string id = txt.Substring(txt.LastIndexOf("UID:") + 4, 9);
String txt = "UID: 994zxfa6q";
int pFrom = txt.IndexOf("UID:") + 4;
Console.WriteLine("pFrom = " + pFrom.ToString());
int pTo = txt.LastIndexOf("UID:") + 14;
Console.WriteLine("pTo= " + pTo.ToString());
String result = txt.Substring(pFrom, pTo - pFrom);
Console.WriteLine("result " + result);
i have a string like that
str = "4975 + 10 * (LOG(250.6)) - 321.2"
i want to compute the result of this operation.
Is there any sort way to do that?
// my operation just includes some of operators (,), +, -, *, / , ., 0-9, LOG
// '.' is used for double number
I believe this is what you are looking for.
Use this library will help you to perform math operations in string format
Add this package
Install-Package DynamicExpresso.Core
code example
public static void Main(string[] args)
{
var interpreter = new Interpreter();
var result = interpreter.Eval("4975 + 10 * (LOG(250.6)) - 321.2".Replace("LOG", "Math.Log"));
Console.WriteLine("result=> " + result);
Console.ReadKey();
}
result=> 4709.03858042462
link for lib https://github.com/davideicardi/DynamicExpresso
Also answered here:
https://stackoverflow.com/a/2859130/1043824
Try DataTable.Compute
I do not have a .net box lying around so I cannot confirm whether it can do log or not, but I have used it for long algebric expressions.
Basically this is how it goes:
DataTable dt = new DataTable();
var ans = dt.compute("5 + (7 - 9) / 3");
I am making an application which "Filewatches" a folder and when a file is created in there it will automatically be mailed to the customer.
The problem is that i haven't found any information on how to split filenames
For example i have a file called : "Q1040500005.xls"
I need the first 5 characters seperated from the last 5, so basically split it in half (without the extension ofcourse)
And my application has to recognize the "Q1040" and the "500005" as seperate strings.
Which will be recognized in the database which contains The query number (Q1040) and the customer number "500005" the email of the customer and the subject of the queryfile.
How can i do this the easiest way?
Thanks for the help!
Use SubString method http://msdn.microsoft.com/es-es/library/aka44szs(v=vs.80).aspx
int lengthFilename = filename.Length - 4; //substract the string ".xls";
int middleLength = lengthFilename/2;
String filenameA = filename.SubString(0, middleLength);
String filenameB = filename.SubString(middleLength, lengthFilename - middleLength);
Is string.Substring method what you're looking for?
Use String.SubString(int startindex, int length)
String filename = Q1040500005.xls
var queryNumber = filename.Substring(0, 5); //Q1040
var customerNumber = filename.Substring(5, 6); //500005
This assumes your strings are a constant length.
Hope this helps.
You can use string.SubString() here
string a = fileName.SubString(0, 5); // "Q1040"
string b = fileName.SubString(5, 5); // "50000" <- Are you sure you didn't mean "last 6"?
string b2 = fileName.SubString(5, 6); // "500005"
This only works, if both strings have a constant fixed length
Edit:
If on the other hand, both strings can have variable length, I'd recommend you use a separator to divide them ("Q1040-500005.xml"), then use string.Split()
string[] separatedStrings = fileName.Split(new char[] { '-', '.' });
string a = separated[0]; // "Q1040"
string b = separated[1]; // "500005"
string extension = separated[2]; // "xls"
I'm getting this error:
Index and length must refer to a location within the string.
Parameter name: length
Using this code:
string a1 = ddlweek.Text.Substring(0, 8);
string a3 = ddlweek.Text.Substring(10, 14);
What does this mean?
If the length of your string (ddlweek) is 23 characters or less, you will get this error:
string ddlweek = "12345678901234567890123";//This is NOK
string a1 = ddlweek.Substring(0, 8);
string a3 = ddlweek.Substring(10, 14);
Console.WriteLine("a1="+a1);
Console.WriteLine("a3="+a3);
Console.ReadLine();
The string should be at least 24 characters long..
You might consider adding an if to make sure everything is OK..
string ddlweek = "123456789012345678901234";//This is OK
string a1 = ddlweek.Substring(0, 8);
string a3 = ddlweek.Substring(10, 14);
Console.WriteLine("a1="+a1);
Console.WriteLine("a3="+a3);
Console.ReadLine();
It means that your ddlweek.Text string contains less number of characters than what you asked for in Substring(index, length).
Example:
if (ddlweek.Text.Length >= 8)
string a1 = ddlweek.Text.Substring(0, 8);
It just means you're asking for a substring of ddlweek that doesn't exist (ie, it's more than 24 characters in length).
this error occurs when you exceed your end limit of total character.
for example
string name = "iLovePakistan"; // Here i want to print only Pakistan
string name2 = name.Substring(5, 150); // this code will throw same error. just replace 150 with name.Length - 5
string name3 = name.Substring(5, name.Length - 5); // i skip firt 5 charchers then name.Length-5 means print rest 8 characters.
string name4 = name.Substring(5, 8); // This will do exactly as name3
Console.WriteLine(name4);
Substring(startIndex,length);
startIndex : Gets the first value you want to get. 0 Begins.
length : The size of the value you get (How many digits you will get).
string Code = "KN32KLSW";
string str = Code.Substring(0,2);
Console.WriteLine("Value : ",str);
On the Console screen : KN
string str = Code.Substring(3,4);
Console.WriteLine("Value : ",str);
On the Console screen : 2KLS
I am trying to use the following to send over an RS232 connected projector to turn it on:
commProj.Parity = "None";
commProj.StopBits = "One";
commProj.DataBits = "8";
commProj.BaudRate = "19200";
commProj.PortName = "COM6";
commProj.CurrentTransmissionType = PCComm.CommunicationManager.TransmissionType.Text; //.Hex
commProj.OpenPort();
commProj.WriteData((char)33 + (char)137 + (char)1 + (char)80 + (char)87 + (char)49 + "\n"); //turn on proj
Problem being is that it doesnt work.
I have done this with a VB6 port and it works just fine:
public static PCComm.CommunicationManager commProj = new PCComm.CommunicationManager();
MSCommProj.CommPort = 6
MSCommProj.Settings = "19200,N,8,1"
MSCommProj.PortOpen = True
MSCommProj.Output = Chr(33) & Chr(137) & Chr(1) & Chr(80) & Chr(87) & Chr(49) & Chr(10)
What am i missing?
David
CommunicationManager.cs: http://snipt.org/xmklh
Okay, the manual helps a lot. Try changing the CurrentTransmissionType to TransmissionType.Hex and sending the string 21890100000a
commProj.CurrentTransmissionType = TransmissionType.Hex;
commProj.WriteData("21890100000a");
EDIT
Sorry, that was "connection check". Use 2189015057310a for on and 2189015057300a for off.
The plus(+) operator for char's doesn't concatenate the values it adds them. So you end up passing "387\n" to write data.
You need to create a char array and then convert that to a string instead:
commProj.WriteData(new string(new char[] { (char)33, (char)37, (char)1, (char)80, (char)87, (char)49, '\n' }));
I don't know what kind of object commProj is (specifically) but my guess is that the problem comes from casting each numeric value to a char. A char is 2 bytes in size. I recommend either trying to write a Byte array with your data in it, or concatenating a string with these chars and then converting the string to ascii text.