Skip to content

Instantly share code, notes, and snippets.

View SanderSade's full-sized avatar

Sander Säde SanderSade

View GitHub Profile
@SanderSade
SanderSade / IpV6ToGuidAndBack.cs
Last active May 15, 2023 13:02
Convert IPv6 address to GUID and back using C#. Works also with IPv4
var ips = new[] { "95e6:a11d:a55b:43f3:8bd0:a45e:cb82:fa4b", "::1", "2041:0000:140F::875B:131B", "2041:0:140F::875B:131B" };
foreach (var ipString in ips)
{
var ip = IPAddress.Parse(ipString);
//NOTE: MapToIPv6() is only needed when we parsed IPv4 as a source. 127.0.0.1 will become ::ffff:127.0.0.1
var bytes = ip.MapToIPv6().GetAddressBytes();
var guid = new Guid(bytes);
var ipBack = new IPAddress(guid.ToByteArray());
//NOTE 2: Due to Microsoft using different byte order vs rest of the world, GUID will look different as string vs IPv6 as string. See https://stackoverflow.com/questions/9195551/why-does-guid-tobytearray-order-the-bytes-the-way-it-does
@SanderSade
SanderSade / SqlMetaDataHelper.cs
Created December 9, 2021 17:42
Create SqlMetaData based on type info
/// <summary>
/// SQL metadata helper. Needed mostly because SQL SqlMetaData class is rather badly designed.
/// </summary>
public static class SqlMetaDataHelper
{
/// <summary>
/// Return SqlMetaData for specific DB type.
/// For variable-length fields such as VarChar length -1 is the default (maximum supported), so specify the proper length.
/// </summary>
public static SqlMetaData Create(string name, SqlDbType sqlDbType, int length = -1)
@SanderSade
SanderSade / SqlTypeHelper.cs
Last active December 9, 2021 17:42
Mapping CLR types to SqlDbType
internal static class SqlTypeHelper
{
private static readonly Dictionary<Type, SqlDbType> SqlDbTypes;
static SqlTypeHelper()
{
//https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings
SqlDbTypes = new Dictionary<Type, SqlDbType>
{
@DukeLupus
DukeLupus / TestArchives.cmd
Last active October 21, 2018 19:39
Test archives using 7z
REM This batch file will test all of the common archive formats
REM (zip, rar, 7z, gz) and .epub books (epub is just HTML inside zip container)
REM using 7zip (free and open source, get it from https://www.7-zip.org)
REM All bad archives are moved to "c:\BadArchives" by default,
REM and the directory is created if it doesn't exist.
REM Change the directory name as wanted (in two places)
REM and path to 7z.exe if it isn't in the default folder.
REM Password-protected files are considered to be invalid as well.
MKDIR "c:\BadArchives"
@SanderSade
SanderSade / .editorconfig
Created January 15, 2018 13:27
.editorconfig for VS
# EditorConfig - http://EditorConfig.org. This file comes from Roslyn - https://github.com/dotnet/roslyn/blob/master/.editorconfig, edited
# top-most EditorConfig file
root = true
# Use tabs for indentation.
[*]
indent_style = tab
trim_trailing_whitespace = true
end_of_line = crlf
@jonlabelle
jonlabelle / async_await_best_practices_cheatsheet.md
Last active October 14, 2025 07:20
C# Asynchronous Programming Guideline Cheat Sheet

Async Await Best Practices Cheat Sheet

Summary of Asynchronous Programming Guidelines

Name Description Exceptions
Avoid async void Prefer async Task methods over async void methods Event handlers
Async all the way Don't mix blocking and async code Console main method
Configure context Use ConfigureAwait(false) when you can Methods that require con­text