Question

I want to create a Byte array in C# and the first and second bytes have to be 70 and 75 respectively:

So I did something like following:

List<byte> retval = new List<byte>();
            retval.Add(Convert.ToByte(75));
            retval.Add(Convert.ToByte(70));

I thought the function will convert the numbers into byte and if I put watch on the arrayList at runtime, then it would look a little different, but it did not change. I was expecting to see values in format of 0x00 something but it still looks like raw integers.

Am I missing something?

Was it helpful?

Solution

Right-click your Watch Window or Immediate Window and check Hexadecimal Display option. It is by default set to "unchecked" for bytes, because Visual Studio IDE assumes that you would prefer to read int values instead of bytes:

enter image description here

OTHER TIPS

A byte is an integeral value and it, along with all the other integral types, don't look like anything until you convert them into a representation. All they are are electrical charges on a silicon chip.

byte, ushort, uint and ulong are all unsigned integral types of varying lengths (1, 2, 4 and 8 octets respectively). They are stored as binary (base two) numbers, with each bit representing a power of two, with the low-order (logically, the right-most) bit representing 20 or 1 and the high-order bit representing the highest power of two possible for that size (respectively: 27, 215, , 231 and 263.

That means, the high order bit is the sign bit (0 for non-negative, 1 for negative). Non-negative values ( x >= 0) are represented as above, except the largest power of two that can be represented is, respectively 26, 214 , 230 and 262, since the high order bit is reserved for the sign.

Negative numbers are stored as the two's complement of the absolute value. That allow subtraction to be performed by addition circuits, subtraction being the addition of the negative. To get the two's-complement of a number, the following process is performed:

  • Invert the bits
  • Add 1, propagating any carries in the usual way, to obtain the negative form of the integer.

So the negative form of 1 is

binary representation of +1:  0000 0000 0000 0001
invert the bits (complement): 1111 1111 1111 1110
add 1:                        1111 1111 1111 1111

If you add the positive and negative forms, the result is zero:

0000 0000 0000 0001
1111 1111 1111 1111
-------------------
0000 0000 0000 0000

And the CPUs carry or integer overflow flag will be set.

Zero remains the same as zero really doesn't have a sign

binary zero:          0000 0000 0000 0000
inverted (complement: 1111 1111 1111 1111
add 1:                0000 0000 0000 0000

And, change the sign of the smallest negative number that can be represented

largest positive value: 1000 0000 0000 0000
inverted (complement):  0111 1111 1111 1111
add 1:                  1000 0000 0000 0000

And you get itself, since its absolute value is 1 larger than the large positive number and adding them together yields the expected zero value as the sign bit is carried out to the left, setting the carry flag).

Adding them together, they yield the expected 0 as the carries propagates out of the high order bit and sets the CPUs carry flag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top