Coder Social home page Coder Social logo

andreakarasho / reinterpret.net Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hellokitty/reinterpret.net

0.0 1.0 0.0 745 KB

.NET library in C# that offers generic extension methods for C++-style reinterpret_cast for select Types.

License: MIT License

Shell 0.03% Batchfile 0.07% C# 98.91% PowerShell 0.99%

reinterpret.net's Introduction

Reinterpret.Net

Reinterpret.Net supports >=.NETFramework2.0 and .NET Core.

Reinterpret.Net is a .NET library that allows users to take advantage of an API like the C++-style reinterpret casts in .NET. It's built using a collection of generic extension methods meaning integration is simple.

It supports casting from bytes to primitives or strings and from primitives and strings to bytes and even supports primitive arrays.

Features

  • Netstandard1.1
  • Netstandard2.0
  • .NETFramework >= 2.0
  • Conversion from byte[] to primitives (Ex. Int32, Float)
  • Conversion from primitives to byte[]
  • Conversion from byte[] to string
  • Conversion from string to byte[]
  • Conversion from byte[] to primitive arrays
  • Conversion from primitive arrays to byte[]

How to Use

Reinterpret.Net is designed for ease-of-use. It is implemented as generic extension methods for the byte[] Type, primitive arrays and strings so reinterpreting is simple.

Converting from a byte[] to an int value (4 bytes)

byte[] bytes = GetBytes();
int intValue = bytes.Reinterpret<int>();

Converting from a byte[] to a string (default unicode encoding)

byte[] bytes = GetBytes();
string result = bytes.Reinterpret();

Converting from a byte[] to a primitive array

byte[] bytes = GetBytes();
int[] result = bytes.ReinterpretToArray<int>();

The extension method API allows you to perform operations that feel like LINQ Letting you reinterpret from an int32 array to a UTF16 string if you so choose.

int32[] values = GetValues()
string result = values
    .Reinterpret() //to bytes
    .ReinterpretToString(); //to string

High Performance (UNSAFE)

This section orginally described a key feature to the library. That feature has since been removed due to Issue #1 and will not be returning anytime soon.

Performance Comparisions

The unsafe operations use to make Reinterpret.Net highly competitive in array and string conversions. These performance benchmarks have been removed due to Issue #1. They are now approximately as fast as the .NET Encoding class or BlockCopy.

Performance differs between framework versions. BitConverter in older versions of .NETFramework is slower and in the .NET Core Corefx repo it's implemented in a significantly different way. All these effect the comparisions but Reinterpret.NET is usually still faster, especially the dangerous no allocation verion, with only BlockCopy being competitive due its unmanaged implementation. However for some unknown reason which doesn't appear to be IL related, could be GC or JIT, the performance of reinterpret suffers on the .NET3.5 and .NET2.0 platforms.

.NET Core and >=.NETFramework4.0

Showcase

.NETFramework2.0 - .NETFramework3.5

Showcase

Setup

To compile or open Reinterpret.Net project you'll first need a couple of things:

  • Visual Studio 2017
  • ilasm.exe (For compiling .il files)

Builds

NuGet: Reinterpret.Net

Myget: hellokitty MyGet Build Status

Tests

Linux Debug Windows .NET Debug
master TODO Build status

reinterpret.net's People

Contributors

hellokitty avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.