Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, 3 February 2017

C# Ticks to human-readable date/time

Problem:

I'd like to get a human-readable date/time for a number of Ticks in C#.

Tool:

Try the tool below :)

Ticks:
DateTime(UTC):
DateTime(local):

(Please note that this might not work in all browsers due to limitations in number size that a browser can handle. Also, please let me know if I goofed up the calculation. Thanks!)

Notes on calculation:

  • ticks to microtime = ticks / 10000
  • microtime to Unix time = microtime - 62135596800000
  • then convert Unix time to human-readable using Javascript Date()

References:

Saturday, 10 December 2016

Convert C# bytes to human-readable strings

Problem:

How do I convert a C# byte into a string so that I can read all 8 digits?

Solution:

Imagine that you have a C# byte named myByte. To get a string containing all 8 binary digits, you can use the following code:

  Convert.ToString(myByte, 2).PadLeft(8, '0');

Notes:

Also remember that the Convert.ToString method is in the System namespace.

References:

https://msdn.microsoft.com/en-us/library/system.convert.tostring(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.string.padleft(v=vs.110).aspx http://stackoverflow.com/questions/4829366/byte-to-binary-string-c-sharp-display-all-8-digits

C# selectively disable warnings

Problem:

I would like to selectively disable C# warnings.

Solution:

The syntax to selectively disable warnings is:

#pragma warning disable <warning number or warning list>
   <code block where warning is to be ignored>
#pragma warning restore <warning number or warning list>

Example:

#pragma warning disable 0618
  MyNecessaryObsoleteFunctionCall();
#pragma warning restore 0618

BadObsoleteCallThatShouldProduceWarning();

Notes:

C# compiler warnings are generally there for good reason, so it's better practice to resolve them rather than hide them. However, in some situations willfully ignoring/acknowledging a warning might be necessary (e.g. if warnings block your team's build, but the code is temporarily required for some important reason).

References:

https://msdn.microsoft.com/en-ca/library/441722ys.aspx http://stackoverflow.com/questions/968293/c-sharp-selectively-suppress-custom-obsolete-warnings