Is there a nice way to split an int into two shorts (.NET)?

前端 未结 13 1670
情深已故
情深已故 2021-01-01 11:25

I think that this is not possible because Int32 has 1 bit sign and have 31 bit of numeric information and Int16 has 1 bit sign and 15 bit of numeric information

相关标签:
13条回答
  • Why not? Lets reduce the number of bits for the sake of simplicity : let's say we have 8 bits of which the left bit is a minus bit.

    [1001 0110] // representing -22
    

    You can store it in 2 times 4 bits

    [1001] [0110] // representing   -1 and 6
    

    I don't see why it wouldn't be possible, you twice have 8 bits info

    EDIT : For the sake of simplicity, I didn't just reduce the bits, but also don't use 2-complementmethod. In my examples, the left bit denotes minus, the rest is to be interpreted as a normal positive binary number

    0 讨论(0)
  • 2021-01-01 12:04

    Int32 num = 70000;

            string str = Convert.ToString(num, 2);
        //convert INT32 to Binary string   
            Int32 strl = str.Length;
        //detect string length
    
    
            string strhi, strlo;
        //ifvalue is greater than 16 bit 
            if (strl > 16)
            {
               int lg = strl - 16;
              //dtect bits in higher word 
               strlo = str.Substring(lg, 16);
         ///move lower word string to strlo 
    
                strhi = str.Substring(0, lg);
       //mov higher word string to strhi
    
            }
            else
    //if value is less than 16 bit
            {
               strhi = "0";
    //set higher word zero
               strlo = str;
    ///move lower word string to strlo 
    
            }
    
            Int16 lowword, hiword;
            lowword = Convert.ToInt16(strlo, 2);
            hiword = Convert.ToInt16(strhi, 2);
            ////convert binary string to int16
            }
    
    0 讨论(0)
  • 2021-01-01 12:09

    If you look at the bit representation, then you are correct.

    You can do this with unsigned ints though, as they don't have the sign bit.

    0 讨论(0)
  • 2021-01-01 12:11

    Unsafe code in C#, overflow doesn't occur, detects endianness automatically:

    using System;
    class Program
    {
        static void Main(String[] args)
        {
            checked // Yes, it works without overflow!
            {
                Int32 original = Int32.MaxValue;
                Int16[] result = GetShorts(original);
                Console.WriteLine("Original int: {0:x}", original);
                Console.WriteLine("Senior Int16: {0:x}", result[1]);
                Console.WriteLine("Junior Int16: {0:x}", result[0]);
                Console.ReadKey();
            }
        }
        static unsafe Int16[] GetShorts(Int32 value)
        {
            byte[] buffer = new byte[4];
            fixed (byte* numRef = buffer)
            {
                *((Int32*)numRef) = value;
                if (BitConverter.IsLittleEndian)
                    return new Int16[] { *((Int16*)numRef), *((Int16*)numRef + 1) };
                return new Int16[] { 
                    (Int16)((numRef[0] << 8) | numRef[1]),  
                    (Int16)((numRef[2] << 8) | numRef[3])
                };
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 12:13

    Due to storage width (32bits and 16bits), converting Int32 to Int16 may imply a loss of information, if your Int32 is greater than 32767.

    0 讨论(0)
  • 2021-01-01 12:16

    You can use StructLayout to do this:

    [StructLayout(LayoutKind.Explicit)]
            struct Helper
            {
                [FieldOffset(0)]
                public int Value;
                [FieldOffset(0)]
                public short Low;
                [FieldOffset(2)]
                public short High;
            }
    

    Using this, you can get the full Value as int , and low part, hight part as short.

    something like:

    var helper = new Helper {value = 12345};
    
    0 讨论(0)
提交回复
热议问题