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>
{
@SanderSade
SanderSade / TimespanFormat.cs
Created July 25, 2019 13:32
Faster timespan format for specific needs. Work in progress
public static string FasterFormat(TimeSpan time)
{
var charP = new char[9];
int hours = time.Hours;
int minutes = time.Minutes;
int seconds = time.Seconds;
int ms = time.Milliseconds;
if (hours > 10) charP[0] = (char)('0' + hours / 10);
if (hours > 0)
{
@SanderSade
SanderSade / FormatSize.cs
Last active October 16, 2018 07:41
Format data sizes to be human-readable, Fast and flexible, supports sizes up to exabytes
/// <summary>
/// Found from the internets, heavily modified for optimization and flexibility
/// </summary>
/// <param name="size">Size in bytes</param>
/// <param name="numberFormat">Format of the returned number. Defaults to 0.##</param>
internal static string ReadableSize(long size, string numberFormat = "0.##")
{
size = Math.Abs(size);
string suffix;
float readable;
@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