In C# what is the default value of the bytes when creating a new byte array?
The default value is 0
For more information about default values:
http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx
Initialize a byte array to a certain value, other than the default null?
For small arrays use array initialisation syntax:
var sevenItems = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
For larger arrays use a standard for
loop. This is the most readable and efficient way to do it:
var sevenThousandItems = new byte[7000];
for (int i = 0; i < sevenThousandItems.Length; i++)
{
sevenThousandItems[i] = 0x20;
}
Of course, if you need to do this a lot then you could create a helper method to help keep your code concise:
byte[] sevenItems = CreateSpecialByteArray(7);
byte[] sevenThousandItems = CreateSpecialByteArray(7000);
// ...
public static byte[] CreateSpecialByteArray(int length)
{
var arr = new byte[length];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = 0x20;
}
return arr;
}
C# Create byte array in code from copied SQL value?
You can include your default file in project and then set value of your object with it's contents, like
MyObject.filebytes = File.ReadAllBytes("default.bin");
Using .NET to create a Base 64 byte array replicating Java code
Bytes in C# are unsigned by default, since they tend to represent raw binary data, not actual numbers. If you want to treat bytes as signed numbers, there's the sbyte
type.
In Java, signed is the default for all numbers and there's no unsigned alternative, for reasons that are unknown to me (if you have a reliable source, feel free to comment).
You can verify that your values in C# are the same as in Java by casting the byte
array to an sbyte
array:
byte[] bytes = new byte[] { 68, 163, 160, 50, 213, 109, 12, 103 };
foreach (var b in bytes)
{
Console.WriteLine((sbyte) b);
}
Prints:
68
-93
-96
50
-43
109
12
103
which is the same as your values from Java.
If your interested in how the same 8 bits are interpreted as unsigned or signed values, read about two's complement representation.
Thus, this is not the cause of your program being incorrect. Carry on debugging, and feel free to ask another question if something else comes up.
Related Topics
C# Download Big File from Server With Less Memory Consumption
How to Use a Nuget Package Within a Powershell Script
Convert Json File to C# and Stored in Database
How to Make Xmlserializer Ignore the Namespace on Deserialization
C# Windows Form Application for Employee Management
How to Print the Elements With Text Value That Contains in a List Selenium C#
Ssh.Net Sftp Get a List of Directories and Files Recursively
Why Does Integer Division in C# Return an Integer and Not a Float
Windows Service Cannot Access Network Location (Unc) Path
Error 5:Access Denied When Starting Windows Service
Convert Time-Formatted Column in Excel to C# Datetime
C# String to Hex , Hex to Byte Conversion
How to Refresh or Show Immediately in Datagridview After Inserting
How to Loop Through All Fields in an Object in C#
How to Trim All Elements in a List
How to Wait Until Task Is Finished in C#