开发者

the equivalent of sscanf_s in C#?

UnlockOffset is DWORD. thisKey is a char[5]

if(EOF == sscanf_s(thisKey, "%d", &UnlockOffset))

How would the above code be done in c# ?

DWORD was converted to UInt32 and thiskey remained char array but I still dont understand the sscanf_s.

开发者_开发技巧PS: I did check MSDN but was not able to understand it very well which was why I posted it here.


sscanf_s basically reads a string and extracts stuff that matches the format string. It'll return EOF if it couldn't extract stuff to match all the format thingies.

You could do something like

string str = new string(thisKey);
if (!UInt32.TryParse(str, out UnlockOffset))

which would accomplish something similar, but it might be more or less strict. UInt32.TryParse returns true if it could convert the string and false if it couldn't, so checking for EOF would be equivalent to seeing whether TryParse is false.


Typically, you would use UInt32.Parse (or TryParse) to pull the information out of a string. It is rare that char[] is used to store string values in C#, as string is more appropriate.


Since everyone has already mentioned uint.Parse (and uint.TryParse), you can convert your char array to an integer like this:

uint UnlockOffset = 0;
foreach (char digit in thisKey)
{
    UnlockOffset *= 10;
    UnlockOffset += (uint)(digit - '0');
}


If thisKey is "123 456 739" then sscanf_s(thisKey, "%d", &UnlockOffset)) would get 123 into UnlockOffset Here's an approximate equivalent

string str = new string(thisKey);
string[] strAr = str.Split(' ');
UnlockOffset = Convert.ToUInt32(strAr!=null ? strAr[0] : str);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜