Advertisement
Guest User

Untitled

a guest
Aug 25th, 2010
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4.  
  5. namespace Timwi.Temp
  6. {
  7.     public static class Utils
  8.     {
  9.         struct SixteenBytes
  10.         {
  11.             public byte a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
  12.         }
  13.         [StructLayout(LayoutKind.Explicit)]
  14.         struct DecimalReverser
  15.         {
  16.             [FieldOffset(0)]
  17.             public decimal Decimal;
  18.             [FieldOffset(0)]
  19.             public SixteenBytes Bytes;
  20.         }
  21.  
  22.         private static DecimalReverser _reverser;
  23.         public static decimal ReverseDecimal(decimal original)
  24.         {
  25.             _reverser.Decimal = original;
  26.             SixteenBytes copy = _reverser.Bytes;
  27.             _reverser.Bytes.a = copy.p;
  28.             _reverser.Bytes.b = copy.o;
  29.             _reverser.Bytes.c = copy.n;
  30.             _reverser.Bytes.d = copy.m;
  31.             _reverser.Bytes.e = copy.l;
  32.             _reverser.Bytes.f = copy.k;
  33.             _reverser.Bytes.g = copy.j;
  34.             _reverser.Bytes.h = copy.i;
  35.             _reverser.Bytes.i = copy.h;
  36.             _reverser.Bytes.j = copy.g;
  37.             _reverser.Bytes.k = copy.f;
  38.             _reverser.Bytes.l = copy.e;
  39.             _reverser.Bytes.m = copy.d;
  40.             _reverser.Bytes.n = copy.c;
  41.             _reverser.Bytes.o = copy.b;
  42.             _reverser.Bytes.p = copy.a;
  43.             return _reverser.Decimal;
  44.         }
  45.     }
  46.  
  47.     static class Program
  48.     {
  49.         static void Main(string[] args)
  50.         {
  51.             Console.OutputEncoding = Encoding.UTF8;
  52.  
  53.             // Prints 7467534787759564412160049152
  54.             Console.WriteLine(Utils.ReverseDecimal(8472m));
  55.  
  56.             // Prints -0.40586562448147411255697604608
  57.             Console.WriteLine(Utils.ReverseDecimal((decimal) Math.PI));
  58.  
  59.             Console.ReadLine();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement