How to decode one Int64 back to two Int32?

The previous question was to encode two Int32into one Int64[ C # - introducing one Int64 from two Int32s

Question : how to decode one Int64back to two Int32?

+3
source share
2 answers

Here's a (cheap) solution that will work for both conversions.

[StructLayout(LayoutKind.Explicit)]
public struct UnionInt64Int32 {
    public UnionInt64Int32(Int64 value) {
        Value32H = 0; Value32L = 0;
        Value64 = value;
    }
    public UnionInt64Int32(Int32 value1, Int32 value2) {
        Value64 = 0;
        Value32H = value1; Value32L = value2;
    }
    [FieldOffset(0)] public Int64 Value64;
    [FieldOffset(0)] public Int32 Value32H;
    [FieldOffset(4)] public Int32 Value32L;
}

The obvious flaw of this, though, is it is incapable. Values ​​Value32H and value32L will change on different end platforms.

+3
source

Something like that:

long x = ...;

int a = (int) (x & 0xffffffffL);
int b = (int) (x >> 32);

It’s just possible that masking in the first form is not necessary ... I can never remember the details of narrowing conversions and signed values, so I turned it on :)

+8

Source: https://habr.com/ru/post/1762603/


All Articles