logo
CSharp_Prog_Guide

Результат

Specified cast is not valid. Error: Incorrect unboxing.

При изменении оператора

int j = (short) o;

на

int j = (int) o;

будет выполнено преобразование со следующим результатом.

Unboxing OK.

How to: Convert a byte Array to an int

This example shows you how to use the BitConverter class to convert an array of bytes to an int and back to an array of bytes. You may have to convert from bytes to a built-in data type after you read bytes off the network, for example. In addition to the ToInt32(array<Byte>[]()[], Int32) method in the example, the following table lists methods in the BitConverter class that convert bytes (from an array of bytes) to other built-in types.

Type returned

Method

bool

ToBoolean(array<Byte>[]()[], Int32)

char

ToChar(array<Byte>[]()[], Int32)

double

ToDouble(array<Byte>[]()[], Int32)

short

ToInt16(array<Byte>[]()[], Int32)

int

ToInt32(array<Byte>[]()[], Int32)

long

ToInt64(array<Byte>[]()[], Int32)

float

ToSingle(array<Byte>[]()[], Int32)

ushort

ToUInt16(array<Byte>[]()[], Int32)

uint

ToUInt32(array<Byte>[]()[], Int32)

ulong

ToUInt64(array<Byte>[]()[], Int32)