Интервал C# к байту []

Если объект реализует IQueryable<T>, запрос выполняется только при перечислении объекта. Это означает, что вы можете объединять запросы в цепочку, и выполнение будет отложено до тех пор, пока вы не вызовете, например ToList().

В вашем примере вы могли бы сделать что-то вроде:

// to select the cities
var largeCities = dbContext.Countries
                           .Include(t => t.Cities)
                           .Where(c=> c.continent == "asia" 
                                  && c.Cities.Population > 500000)
                           .Select(c => c.Cities).ToList();

// EDIT
// to select the countries that have these cities
var countries = dbContext.Countries
                           .Include(t => t.Cities)
                           .Where(c=> c.continent == "asia" 
                                  && c.Cities.Population > 500000)
                           .ToList();  // remove .Select(c => C.Cities) if you want the countries

Или

var largeCities = asianCountries
                       .Where(c => c.Cities.Population > 500000)
                       .Select(c => c.Cities)
                       .ToList();
158
задан Peter 20 February 2017 в 13:53
поделиться

6 ответов

The RFC is just trying to say that a signed integer is a normal 4-byte integer with bytes ordered in a big-endian way.

Now, you are most probably working on a little-endian machine and BitConverter.GetBytes() will give you the byte[] reversed. So you could try:

int intValue;
byte[] intBytes = BitConverter.GetBytes(intValue);
Array.Reverse(intBytes);
byte[] result = intBytes;

For the code to be most portable, however, you can do it like this:

int intValue;
byte[] intBytes = BitConverter.GetBytes(intValue);
if (BitConverter.IsLittleEndian)
    Array.Reverse(intBytes);
byte[] result = intBytes;
207
ответ дан 23 November 2019 в 21:41
поделиться
using static System.Console;

namespace IntToBits
{
    class Program
    {
        static void Main()
        {
            while (true)
            {
                string s = Console.ReadLine();
                Clear();
                uint i;
                bool b = UInt32.TryParse(s, out i);
                if (b) IntPrinter(i);
            }
        }

        static void IntPrinter(uint i)
        {
            int[] iarr = new int [32];
            Write("[");
            for (int j = 0; j < 32; j++)
            {
                uint tmp = i & (uint)Math.Pow(2, j);

                iarr[j] = (int)(tmp >> j);
            }
            for (int j = 32; j > 0; j--)
            {
                if(j%8==0 && j != 32)Write("|");
                if(j%4==0 && j%8 !=0) Write("'");
                Write(iarr[j-1]);
            }
            WriteLine("]");
        }
    }
}```
0
ответ дан 23 November 2019 в 21:41
поделиться

Вот еще один способ сделать это: как мы все знаем, 1x байт = 8x бит, а также «обычное» целое число (int32) содержит 32 бита (4 байта). Мы можем использовать оператор >> для сдвига битов вправо (оператор >> не изменяет значение.)

int intValue = 566;

byte[] bytes = new byte[4];

bytes[0] = (byte)(intValue >> 24);
bytes[1] = (byte)(intValue >> 16);
bytes[2] = (byte)(intValue >> 8);
bytes[3] = (byte)intValue;

Console.WriteLine("{0} breaks down to : {1} {2} {3} {4}",
    intValue, bytes[0], bytes[1], bytes[2], bytes[3]);
35
ответ дан 23 November 2019 в 21:41
поделиться

BitConverter.GetBytes(int) almost does what you want, except the endianness is wrong.

You can use the IPAddress.HostToNetwork method to swap the bytes within the the integer value before using BitConverter.GetBytes or use Jon Skeet's EndianBitConverter class. Both methods do the right thing(tm) regarding portability.

int value;
byte[] bytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value));
23
ответ дан 23 November 2019 в 21:41
поделиться

When I look at this description, I have a feeling, that this xdr integer is just a big-endian "standard" integer, but it's expressed in the most obfuscated way. Two's complement notation is better know as U2, and it's what we are using on today's processors. The byte order indicates that it's a big-endian notation.
So, answering your question, you should inverse elements in your array (0 <--> 3, 1 <-->2), as they are encoded in little-endian. Just to make sure, you should first check BitConverter.IsLittleEndian to see on what machine you are running.

3
ответ дан 23 November 2019 в 21:41
поделиться

Если вам нужна более общая информация о различных методах представления чисел, в том числе о дополнении до двух, посмотрите:

Дополнение до двух и Представление числа со знаком в Википедии

2
ответ дан 23 November 2019 в 21:41
поделиться
Другие вопросы по тегам:

Похожие вопросы: