Questions tagged [ushort]

ushort keyword indicates an integral data type that stores values according to the size and range shown in the following table.

ushort keyword indicates an integral data type that stores values according to the size and range shown in the following table.

You can declare and initialize a ushort variable by assigning a decimal literal, a hexadecimal literal, or (starting with C# 7) a binary literal to it. If the integer literal is outside the range of ushort (that is, if it is less than UInt16.MinValue or greater than UInt16.MaxValue), a compilation error occurs.

Docs: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ushort

49 questions
17
votes
2 answers

How do I print a short as an unsigned short in Java

I have an array of short whose values range between 0 and the maximum value of a short. I scale the data (to display it as TYPE_USHORT) so that the resulting short values range between 0 and 65535. I need to print some of the scaled values but…
Nate Lockwood
  • 3,325
  • 6
  • 28
  • 34
13
votes
2 answers

What is the difference between a short and ushort in C#?

What is the difference between a word short and ushort in C#? They are both 16 bits!
Adam
  • 131
  • 1
  • 1
  • 3
10
votes
2 answers

byte[] to ushort[]

Here is my question. Bear with me giving a little explanation: I am reading tiff image into buffer; Each pixel of my tiff is represented by a ushort (16 bits data, non-negtive). My image size is 64*64 = 4096. When my tiff is loaded into buffer,…
Nick Tsui
  • 524
  • 2
  • 9
  • 29
8
votes
2 answers

Convert a ushort value into two byte values in C#

How do I split a ushort into two byte variables in C#? I tried the following (package.FrameID is ushort): When I try to calculate this with paper&pencil I get the right result. Also, if FrameID is larger than a byte (so the second byte isn't zero),…
user2071938
  • 2,055
  • 6
  • 28
  • 60
6
votes
3 answers

C# Cannot use Linq DefaultIfEmpty in ushort list items?

I want to take max value of ushort list and when that list is empty I want set "1" for default value. For example: List takeMaxmumId = new List(); var max = takeMaxmumId.Select(x=>x).DefaultIfEmpty(1).Max(); In my Example Visual…
Ali Yousefi
  • 2,355
  • 2
  • 32
  • 47
5
votes
1 answer

Convert ushort[] into byte[] and back

I have a ushort array that needs converting into a byte array to be transferred over a network. Once it gets to its destination, I need then reconvert it back into the same ushort array it was to being with. Ushort Array Is an array that is of…
Oliver Jones
  • 1,420
  • 7
  • 27
  • 43
5
votes
6 answers

How to quickly subtract one ushort array from another in C#?

I need to quickly subtract each value in ushort arrayA from the corresponding index value in ushort arrayB which has an identical length. In addition, if the difference is negative, I need to store a zero, not the negative difference. (Length =…
nb1forxp
  • 385
  • 2
  • 14
4
votes
2 answers

Migrating C# code to Java, unsigned short and byte array conversion

I am writing a piece of code in Java (I'm fairly new to Java) that I have previously written in C#. Here's the code and the example in C#. ushort number = 0xAABB; // 43707 byte[] data = new byte[2]; EndianBitConverter.Big.CopyBytes(number, data, 0);…
Tomislav Markovski
  • 12,331
  • 7
  • 50
  • 72
4
votes
2 answers

Why can 'int' be treated as 'ushort' but not when passed as a parameter in an extension method and what's an elegant solution to it?

I have this extension method: public static bool In(this T source, params T[] list) { return list.Contains(source); } Now I need to use the above method for a ushort. When I try ushort p = 3; if (p.In(1, 2, 3, 4, 5)) return; The first…
nawfal
  • 70,104
  • 56
  • 326
  • 368
3
votes
1 answer

Python 3.4: Converting ushort to bytes

I'm trying to convert a ushort to bytes. However, when I try this: >>import struct >>val =struct.pack('
Kucosyn
  • 49
  • 6
3
votes
6 answers

C# ushort[] to string conversion; is this possible?

I have a very painful library which, at the moment, is accepting a C# string as a way to get arrays of data; apparently, this makes marshalling for pinvokes easier. So how do I make a ushort array into a string by bytes? I've tried: int i; String…
mmr
  • 14,781
  • 29
  • 95
  • 145
3
votes
1 answer

Add uchar values in ushort array with SSE or SSE3

I have an unsigned short dst[16][16] matrix and a larger unsigned char src[m][n] matrix. Now I have to access in the src matrix and add a 16x16 submatrix to dst, using SSE2 or SSE3. In an older implementation, I was sure that my summed values were…
pompolus
  • 45
  • 1
  • 6
2
votes
3 answers

Convert int[] array to ushort[] array

I need to convert an int[] array to a ushort[] array in order to use the method Array.Copy() from theses two types of array. Which method should I use ?
PLB
  • 197
  • 1
  • 10
2
votes
0 answers

Does operator fail to distinguish short[] from ushort[]?

Consider the following code: I build it with .NET framework 4.7.2. ushort[] a = new ushort[100]; if (a is short[]) { Assert.Fail("This never happens."); } object b = a; if (b is short[]) { Assert.Fail("Why?"); } The first test works as…
Surak of Vulcan
  • 348
  • 2
  • 11
2
votes
0 answers

c# ushort uint16 which type to choose

I can't understand why we can have ushort and uint16… 2 bytes for both. But a problem if I want to use Convert. If I remember well it's ushort.parse(stringtype) while convert.touint16 needs an object. I searched but can't find why… In the past I…
user8888955
1
2 3 4