Skip to content

Instantly share code, notes, and snippets.

View benrr101's full-sized avatar

Benjamin Russell benrr101

  • Microsoft
  • Austin, TX
View GitHub Profile
@benrr101
benrr101 / MultipartPart.cs
Created April 10, 2016 01:49
FormMultipart Parsers
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace DolomiteWcfService.MultipartParser
{
public abstract class MultiPartPart
{
public static async Task<Task> WithTimeout(this Task task, TimeSpan timeout)
{
Task delayTask = Task.Delay(TimeSpan.FromSeconds(1));
Task firstCompleted = await Task.WhenAny(task, delayTask);
if (firstCompleted == delayTask)
{
throw new Exception("Task timed out");
}
return task;
@benrr101
benrr101 / Converters-Ieee754DoubleExtended.cs
Created January 18, 2021 19:39
Converters - IEEE 754 Double/Extended Conversion
public static double ConvertIeeeExtendedToDouble(byte[] bytes)
{
int e = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
ulong h = ((ulong)bytes[2] << 24)
| ((ulong)bytes[3] << 16)
| ((ulong)bytes[4] << 8)
| bytes[5];
ulong l = ((ulong)(bytes[6]) << 24)
| ((ulong)(bytes[7]) << 16)
| ((ulong)(bytes[8]) << 8)
private static string GetFriendlyByteString(long bytes)
{
string[] units = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
double runningTotal = bytes;
foreach (var unit in units)
{
double dividedTotal = runningTotal / 1024;
if (dividedTotal < 1)
{
return $"{runningTotal:F2} {unit}";