Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.InteropServices;
- namespace Example {
- /// <summary>
- /// Each instance must be used in single thread
- /// </summary>
- class DecimalBitConverter {
- [StructLayout(LayoutKind.Explicit)]
- struct OverlapData {
- [FieldOffset(0)] public Decimal decimalValue;
- [FieldOffset(0)] public Int64 int64_0;
- [FieldOffset(8)] public Int64 int64_1;
- }
- OverlapData _temp = new OverlapData();
- public Decimal ToDecimal(Int64 i0, Int64 i1) {
- _temp.int64_0 = i0;
- _temp.int64_1 = i1;
- return _temp.decimalValue;
- }
- public Decimal ToDecimal(Tuple<Int64, Int64> x) {
- return ToDecimal(x.Item1, x.Item2);
- }
- public Tuple<Int64, Int64> ToLongs(Decimal x) {
- _temp.decimalValue = x;
- return Tuple.Create(_temp.int64_0, _temp.int64_1);
- }
- }
- class Test {
- private static void Main(string[] args) {
- var x = 42.0042m;
- var converter = new DecimalBitConverter();
- var t = converter.ToLongs(x);
- Console.WriteLine(t);
- var y = converter.ToDecimal(t);
- Console.WriteLine(y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement