Skip to content

Instantly share code, notes, and snippets.

using System;
using System.Linq;
var bytes = new byte[] { /* ... */ };
var hex = new string(bytes.SelectMany(x => x.ToString("x2").ToCharArray()).ToArray());
var data = Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
using System;
using System.Linq;
namespace SystemExtensions
{
public static class HexadecimalExtensions
{
/// <summary>
/// Converts a byte array to a hexadecimal string.
@smonn
smonn / mediaqueryfix.css
Last active December 14, 2015 20:09
Required CSS when working with media queries.
html { height: 100%; overflow: hidden; }
body { height: 100%; overflow: auto; }
@smonn
smonn / ImageBuilder.cs
Last active December 14, 2015 15:29
A class that resizes an image.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
public sealed class ImageBuilder : IDisposable
{
@smonn
smonn / readme.md
Last active December 14, 2015 14:18
Issue with vagrant init: "No such file or directory".

After installing VirtualBox and Vagrant I tried to follow the getting started guide, but when I tried to run vagrant init I got this error:

C:\vagrant_guide> vagrant init
C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:247:in `mkdir': No such file or directory - H: (Errno::ENOENT)
    from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:247:in `fu_mkdir'
    from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:221:in `block (2 levels) in mkdir_p'
    from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:219:in `reverse_each'
    from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:219:in `block in mkdir_p'
    from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:205:in `each'

from C:/vagrant/embedded/lib/ruby/1.9.1/fileutils.rb:205:in `mkdir_p'

@smonn
smonn / ImageHelper.cs
Created March 5, 2013 11:03
Image helper class. Basic "validation" of JPEG bytes.
/// <summary>
/// Helper methods for verifying image content types by validating the first bytes of the file.
/// You can extend this class by adding additional XxxFirstBytes constants and IsXxx methods.
/// </summary>
public static class ImageHelper
{
// These are the first three bytes of a JPEG, usually enough to determine if the file is an image/jpeg
private static readonly byte[] JpegFirstBytes = new byte[] { 0xFF, 0xD8, 0xFF };
@smonn
smonn / encrypt.cs
Last active December 14, 2015 00:59
Encrypt password with random salt, and example of slow equals.
/*
using System;
using System.Linq;
using System.Security.Cryptography;
*/
// The user's password in pure text.
var password = string.Empty;
@smonn
smonn / startdev.js
Last active December 13, 2015 18:08
This is my little play script written in Node.js. I use it to fetch the latest changes from the version control system and build the project(s) I'm working on. Consider it a Makefile written in JavaScript. Probably not entirely necessary, but I find it useful and more fun to maintain than a shell script.
/*jshint node: true */
'use strict';
var exec = require('child_process').exec,
fs = require('fs'),
cwd = 'C:\\working\\directory',
env = {
'PATH': 'C:\\Windows\\System32\\;'
},
commands = {
'command name': 'command-line --with-params'
@smonn
smonn / readme.md
Last active March 27, 2022 10:49
UUID v4 generation

Not the smallest footprint out there, but it seems to be faster when I compare against other solutions, but not when I use jsperf.com for some reason. This solution is based upon that gist by the way. Biggest difference is that this is more readable...

This will generate a version 4 UUID. The non-packed version passes JSLint. Performance comparison at jsperf.

Reduced footprint.

@smonn
smonn / xhr.js
Created May 30, 2012 19:58
My XML HTTP Request function
/*jslint browser: true, windows: true, indent: 2 */
// async: whether the request is asynchronuos or not
// cached: set to false to ignore browser cache
// method: the http method to use
// data: key value pairs, e.g. foo=1&bar=2
// url: the absolute or relative url to send the request to
// success: the success function, will receive the XHR object
// failure: the failure function, will receive the XHR object
// prepare: set to a function to set additional parameters to the xhr object, it should return the xhr object
(function (w) {