C++ to C# code conversion [closed] - c#

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am trying to emulate some C++ code in C#. I am not familiar with the intricate workings of C++ and don't quite understand how to implement this code in C#.
Could someone please explain what the functions are doing and what their output would be in ASCII? In particular, I do not understand what the "memcpy" method is doing the way this code is written.
//example values
str = "<Request Type="Query" Version="1.0"></Request>"
uintcrc = getCrc(str, strlen(str));
//code i don't understand
//create a byte array with a null terminator?
memset(strQueryBuffer, '\0', sizeof(str));
//print the values into the byte array
sprintf(strQueryBuffer, "%c%s%c", COMM_STX, str, COMM_ETX);
//append the uintcrc to the end of the byte array?
memcpy(strQueryBuffer + strlen(strQueryBuffer), &uintcrc, sizeof(uintcrc));

it does nothing else than
strQueryBuffer = COMM_STX + "<Request Type='Query' Version="1.0"></Request>" + COMM_ETX + Encoding.Ascii.GetString(BitConverter.GetBytes(uintcrc));
if you have a binary system and want to send the complete information binary, you can also write
var str = "<Request Type='Query' Version="1.0"></Request>";
byte[] Data = (new [] { COMM_STX }).Concat(Encoding.Ascii.GetBytes(str)).Concat(new [] { COMM_ETX }).Concat(BitConverter.GetBytes(uintcrc)).ToArray();

strQueryBuffer + strlen(strQueryBuffer)
is same as
&strQueryBuffer[strlen(strQueryBuffer)]
so it append binary value of crc to the end of strQueryBuffer

Related

What are the library that use C# 4.0 dynamic as an essential part of it? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
What are the library that use C# 4.0 dynamic as an essential part of it?
The most powerful uses of dynamic are evil incarnate and should never be used near production code.
For example:
var sql = SqlBuilder.MyDatabase
* "SELECT * FROM MyTable WHERE UserName = " + userName;
User result = sql;
dynamic could be used to turn userName into a parameter and turn the last line into an ExecuteReader() call.
Inspired by Jon Skeet
Another example:
var _ = RestClient.Builder;
var endpoint = _.https/_.api.stackexchange.com/2.1/_.answers
var results = endpoint(sort: "activity", order: "desc", site: "stackoverflow");
// Returns https://api.stackexchange.com/2.1/answers?order=desc&sort=activity&site=stackoverflow as dynamic JSON
The _ is needed to allow arbitrary identifiers.
You could get rid of the _ using expression trees:
var endpoint = RestClient.Build((https, api, answers) =>
https/api.stackexchange.com/2.1/answers
);
This uses parameter names to allow arbitrary identifiers.

C# remove text after comma [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
How can I remove specific text from a string?
for example I have this string:
string file = "43 , 2000-12-12 003203";
I need to remove the text after the comma, including comma, so I can have as a final result:
string file = "43";
thank you,
string file = "43 , 2000-12-12 003203";
string number = file.Split(',')[0].Trim();
You can do this:
string output = file.Substring(0, file.IndexOf(',')).Trim();
However, that might fail if the string doesn't contain a comma. To be safer:
int index = file.IndexOf(',');
string output = index > 0 ? file.Substring(0, index).Trim() : file;
You can also use Split as others have suggested, but this overload would provide better performance, since it stops evaluating the string after the first comma is found:
string output = file.Split(new[] { ',' }, 2)[0].Trim();
Possibly by using Split?
file.Split(',')[0].Trim();

not insert Turkish character Window 8 app to Sqlite [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I have been working with the Sqlite Database on the Windows 8 App.(Sqlite for Windows Runtime)
Turkish character problem when I insert to database.
How can I fix this problem. can you help me?
Thank you very much.
Easy come.
using (var db = new SQLite.SQLiteConnection(App.DbPath))
db.Execute("Insert Into Stock (Name) Values('ŞşİıĞğ')");
i try this but result -> ÞþÝýÐð
string a = "ŞşİıĞğ";
string b = string.Empty;
byte[] utf8Bytes = Encoding.UTF8.GetBytes(a);
b= Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length);
using (var db = new SQLite.SQLiteConnection(App.DbPath))
db.Execute("Insert Into Stock (Name) Values('"+ b +"')");
Most likely the problem is that your column cannot handle UTF-16 (unicode) properly (could be client API). Safest bet is to convert to/from utf-8 on the client side then read/write to database.
System.Encoding.UTF8 has all the magic.

C# read a text file and search for string [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
In C# how can I open the text file , search for string "mystring" and if string is there then set variable Vara = 1 else Vara= 0 .
Thanks in advance
Quick & dirty using System.IO.File.ReadAllText:
int Vara = File.ReadAllText(path).Contains("mystring") ? 1 : 0;
won't do all your exercise to leave you some fun, but here a way to start:
to get all text of a file into a string variable try this:
using System.IO;
string fileContent = File.ReadAllText(#"C:\file.txt");
then here to check if your string is inside:
bool present = fileContent.IndexOf("mystring") >= 0;

.NET String Assignment Removes Characters [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Never seen anything like this before. I'm doing a very simple DataReader value assignment.
story.Byline = _r["Byline"].ToString();
But the values of _r["Byline"].ToString() and story.Byline are different after the assignment. Here's the output from the Immediate window:
story.Byline
"Associated Press - FIRST LASTAssociated Press - FIRST LAST"
_r["Byline"].ToString()
"Associated Press - FIRST LAST<br />Associated Press - FIRST LAST"
Why is <br /> being removed?
Calling reader["x"].ToString() you actually calls x' type possibly overridden method ToString().
If you're sure that it's string, use reader.GetString(reader.GetOrdinal("x"))
Well, this is a bit embarrassing:
public string Byline
{
get { return !_elements.ContainsKey("Byline") ? "" : (string)_elements["Byline"]; }
set
{
string _buf = Functions.StripTags(value);
_elements["Byline"] = _buf;
}
}
Incorrect assumptions FTL. Can this question be deleted?

Categories