Skip to content

Instantly share code, notes, and snippets.

@wolf99
wolf99 / ConvertWithSign.vb
Last active April 20, 2017 12:33
VB.NET extensions to allow conversion from an unsigned type to the equivalent signed type, and vice versa, while preserving the sign bit, by using a structure with explicit layout to simulate a union.
Option Strict On
Option Explicit On
Imports System.Runtime.InteropServices
Imports System.Runtime.CompilerServices
Module ConvertWithSign
' These extensions allow conversion from an unsigned type to the equivalent
' signed type, and vice versa, while preserving the sign bit.
'
@wolf99
wolf99 / BoilerplateException.cs
Last active August 3, 2017 10:08
A boilerplate C# exception class
using System;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace FooProgram
{
// Recommended practice to derive from Exception rather than ApplicationException
// Recommended practice to make exceptions serializable
[Serializable]
public class BoilerplateException : Exception, ISerializable
@wolf99
wolf99 / DerivedClassEnumerator.cs
Created March 31, 2017 14:44
Class that lists all the types derived from a given type. Taken from http://stackoverflow.com/a/6944605/1292918
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace FooProgram
{
public static class DerivedClassEnumerator
{
// Class that list all the types derived from a given type
@wolf99
wolf99 / perfect_numbers.c
Created March 31, 2017 08:16
MISRA classify_number()
kind classify_number(int n)
{
kind class = error;
if (n > 0) {
int buf = aliquot_sum(n);
if (buf > n) {
class = abundant_number;
} else if (buf < n) {
class = deficient_number;
} else {
@wolf99
wolf99 / cmock_return_thru_ptr.c
Last active February 7, 2017 16:35
An example showing how to use cmock's ReturnThrePtr mock type
/* bar.c */
#include foo.h
int bar (void)
{
int a = 5, b = 0;
e = foo(a, &b); /* Note b is local, thus test won't know its address. */
/* ... use b ... */
return e;
}
@wolf99
wolf99 / my_isinf.c
Last active October 27, 2019 21:50
Portable alternative to isinf() in math.h
int my_inf(double x)
{
volatile double tmp = x; /* Prevent optimization by compiler. */
if ((tmp == x) && ((tmp - x) != 0.0))
return x < 0.0 ? -1 : 1;
return 0;
}
@wolf99
wolf99 / my_isnan.c
Last active October 27, 2019 21:56
Portable alternative to isnan() in math.h
int my_isnan(double x)
{
volatile double tmp = x; /* Prevent optimization by compiler. */
return tmp != x;
}
@wolf99
wolf99 / AddLogLine.vb
Created August 22, 2016 12:13
Extension to add strings to a VB.NET ListBox for output or console logging.
'Usage: ListBox1.AddLogLine("Log this!")
Imports System.Runtime.CompilerServices
Module FormExtensions
Delegate Sub UpdateLog(lb As ListBox, line As String)
<Extension()>
Public Sub AddLogLine(lb As ListBox, line As String)
If (lb.InvokeRequired) Then 'Invoke from the GUI thread if called from a different one
lb.Invoke(New UpdateLog(AddressOf AddLogLine), lb, line)
@wolf99
wolf99 / BetterComboBox.vb
Last active August 29, 2015 14:27
A VB.NET control, derived from ComboBox, that adds a SelectedIndexChanging event to allow the change to be cancelled
'This is a VB version of the C# code by Mike Bevers as of 19 Aug 2015 12:07 at
'http://www.mikebevers.be/blog/2010/02/extended-combobox-with-a-selection-changing-event/
'Usage:
'Private Sub BetterComboBox1_SelectedIndexChanging( _
' sender As System.Object, _
' e As System.ComponentModel.CancelEventArgs _
') Handles BetterComboBox1.SelectedIndexChanging
'
' If True Then
@wolf99
wolf99 / ToHexString.vb
Last active August 29, 2015 14:27
Extensions to convert VB.NET numeric type data to formatted hexadecimal strings
'Usage:
' Dim MyBytes() As Byte = {0, &HA5}
' Console.WriteLine(MyBytes.ToHexString)
' 'Prints "00 A5"
Imports System.Runtime.CompilerServices
Public Module Extensions
<Extension()>
Public Function ToHexString(a As Byte(), offset As Integer, length As Integer) As String
Dim RetVal As New System.Text.StringBuilder((length * 3) - 1)
Dim ByteStr As String