我想将 TCHAR 转换为 int,但是当我转换它时,我将 ASCII 值转换为 int,而不是数字值。
代码:
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;
int converttoint()
{
TCHAR tchar[2];
wcscpy_s(tchar, TEXT("5"));
int i = tchar[0];
cout << i << endl;
return i;
}
int main(int argc, _TCHAR* argv[])
{
converttoint();
string in;
cin >> in;
}
Output: 53
Run Code Online (Sandbox Code Playgroud)
但我希望我能成为5岁。
如何才能做到这一点?
正如我在标题中写的那样。
如果在您的应用程序中使用 getHashCode() 不安全,为什么要使用它?(对于字符串和整数)我想用它来交叉方法,除了 Linq 模型中的方法或创建我自己的 IEqualityCompare 类。感觉就像一个机会 - 如果它不是 100% 安全?
或者我错过了什么?
正如在https://docs.microsoft.com/中的 String.GetHashCode 方法中引用的那样
重要的
如果两个字符串对象相等,则 GetHashCode 方法返回相同的值。但是,每个唯一的字符串值都没有唯一的哈希码值。不同的字符串可以返回相同的哈希码。
不保证哈希码本身是稳定的。对于单个 .NET 版本,相同字符串的哈希码可能因 .NET 实现、.NET 版本和 .NET 平台(例如 32 位和 64 位)而异。在某些情况下,它们甚至可能因应用领域而异。这意味着同一程序的两次后续运行可能会返回不同的哈希码。
因此,永远不应该在创建它们的应用程序域之外使用哈希码,永远不应该将它们用作集合中的关键字段,也不应该持久化它们。
最后,如果您需要加密强哈希,请不要使用哈希代码代替加密哈希函数返回的值。对于加密哈希,请使用从 System.Security.Cryptography.HashAlgorithm 或 System.Security.Cryptography.KeyedHashAlgorithm 类派生的类。
有关哈希码的更多信息,请参阅 Object.GetHashCode。
考虑此代码,对于c#中的标准Windows应用程序:
Program.cs中:
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
finally
{
MessageBox.Show("Bye !");
}
}
Run Code Online (Sandbox Code Playgroud)
From1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Click += new EventHandler(Form1_Click);
}
void Form1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
Run Code Online (Sandbox Code Playgroud)
是什么让这个表格作为根本参考?是因为静态主要方法,我猜它是,还是它还有什么呢?
如果我只是用X关闭表单,那么消息框将显示"再见",但如果我点击它则不会.(因为application.Exit()) - 我觉得奇怪的是考虑main中的finally方法.
那么主要的问题是,什么使形式对象,作为一个根源参考,所以垃圾收集器不会杀死它?
示例线程池:
public class Example {
public static void Main() {
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); //task 1
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); //task 2
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); //task 3
Console.WriteLine("Main thread does some work, then sleeps.");
Thread.Sleep(1000);
Console.WriteLine("Main thread exits.");
}
static void ThreadProc(Object stateInfo) {
Console.WriteLine("Hello from the thread pool.");
}
}
Run Code Online (Sandbox Code Playgroud)
示例信号量:
public class Example
{
private static Semaphore _pool;
private static int _padding;
public static void Main()
{
_pool = new Semaphore(0, 3);
// Create and start five numbered threads.
//
for(int i = …Run Code Online (Sandbox Code Playgroud) 假设您向客户发送了一个新的应用程序版本,并且需要更新本地客户数据库,比如它是一个SQL microsoft数据库2008.
现在我通过数据库中的版本表执行此操作,并运行sql脚本. - 匹配该版本,如:
if (DatabaseVersion < Common_func.ProgramDBFixVersion)
{
switch (DatabaseVersion)
{
case 0:
if (Fix0() == false) NoErrorFixFlg = false;
goto case 1;
case 1:
if (Fix1() == false) NoErrorFixFlg = false;
goto case 2;
.
.
.
private static bool Fix1()
{
try
{
var conn = new SqlConnection(Utils.ConnectionString);
conn.Open();
ExecSql(conn, "ALTER TABLE Customer ADD Is_Deleted [bit] NULL");
conn.Close();
}
catch (Exception ex)
{
retrun false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)这项工作很好,但在实体框架中是否有任何真正内置的支持.
没有任何数据丢失!
如果是这样 - 你能否给出一些具体的例子,说明如何以正确的方式做到这一点.
非常感谢!