Questions tagged [bitconverter]

Class in C# and Java that converts base data types to an array of bytes, and an array of bytes to base data types.

Class in C# and Java that converts base data types to an array of bytes, and an array of bytes to base data types.

138 questions
3
votes
1 answer

byte[8] to bits[64] parsing

I'm reading a binary file using c#. The specification for the file type says there 6 things packed into a byte[8]. However, it says first variable are the 0-19 bits. Second variable 20-39, third 40-59, fourth 60-62 and the 63 bit is is boolean. How…
3
votes
2 answers

Converting int value into 3 byte array (and vice versa)

I am working on a C# WinForms application that reads/writes data to/from a hardware device. My application has a multiselect listbox which contains the numbers 1 - 100000 and the user may select up to 10 numbers. When they're done selecting each…
bmt22033
  • 6,880
  • 14
  • 69
  • 98
2
votes
2 answers

C# performance - pointer to span in a hot loop

I'm looking for a faster alternative to BitConverter: But! Inside a "hot loop": //i_k_size = 8 bytes while (fs.Read(ba_buf, 0, ba_buf.Length) > 0 && dcm_buf_read_ctr < i_buf_reads) { Span sp_data = ba_buf.AsSpan(); for (int i = 0; i <…
2
votes
1 answer

Why does BitConverter seemingly return incorrect results when converting floats and bytes?

I'm working in C# and attempting to pack four bytes into a float (the context is game development, where an RGBA color is packed into a single value). To do this, I'm using BitConverter, but certain conversions seem to result in incorrect bytes.…
Grimelios
  • 351
  • 2
  • 11
2
votes
6 answers

C# - Read a double value

Suppose there is only one single double value written into a file in binary format. How can I read that value using C# or Java? If I have to find a double value from a huge binary file, what techniques should I use to find that?
Barun
  • 1,885
  • 3
  • 27
  • 47
2
votes
2 answers

Marshal.Sizeof and BitConverter.GetBytes behave differently on booleans

Let's consider this piece of code : static void Main(string[] args) { Console.WriteLine(Marshal.SizeOf(typeof(bool))); Console.WriteLine(String.Join(", ", BitConverter.GetBytes(true))); } if bool is 1 byte, I'd expect it to output 1 …
Regis Portalez
  • 4,675
  • 1
  • 29
  • 41
2
votes
1 answer

Float to Byte array in other format

I'm using Bitconverter to convert from a float to a Byte array. byte[] ValueByteArray = BitConverter.GetBytes(Value); Now I'm evaluating my application with another application, and the goal is of course that my output is exactly the same. Problem…
Belekz
  • 480
  • 1
  • 7
  • 18
2
votes
3 answers

Conversion an Int to two Shorts returns padded data / negative values

I have a simple piece of code to convert an Int to two shorts: public static short[] IntToTwoShorts(int a) { byte[] bytes = BitConverter.GetBytes(a); return new short[] { BitConverter.ToInt16(bytes, 0), BitConverter.ToInt16(bytes, 2)…
AntGamle
  • 21
  • 1
2
votes
1 answer

Easily use all overloads that exist on BitConverter.GetBytes for my own method

In my program I have a PacketFactory that builds buffers ready for sending over a NetworkStream. As you can see below this method takes a short as parameter. This short is passed to BitConverter.GetBytes. internal static class PacketFactory { …
Florian Harwoeck
  • 395
  • 2
  • 11
2
votes
2 answers

How to get ushort data in C#, A909 for 41104?

I'm trying to convert an int value to a 16-bit unsigned char type (USHORT). In an example, 41104 is A909 in ushort, but in C# I tried with code sample as (with help from MSDN article BitConverter.GetBytes Yöntem (UInt16)): byte[] bytes =…
dankyy1
  • 1,094
  • 2
  • 16
  • 32
2
votes
0 answers

Does BitArray endianess always match BitConverter endianess?

If my class uses the BitArray and BitConverter classes, will the endianess of both always match? The documentation for the BitConverter constructor explicitly mentions that it depends on the architecture it is being used on (so nearly always…
Toby
  • 9,696
  • 16
  • 68
  • 132
2
votes
1 answer

BitConverter Exception, the destination array is too small

This is pretty simple to create. I have a simple byte array with proof that it has data in it on runtime : Then I simply do var bytedata = BitConverter.ToUInt32(byte_array,0); It compiles, but I get an Argument Exception on runtime that says…
ESD
  • 675
  • 2
  • 12
  • 35
2
votes
1 answer

How do I convert less than 8 bytes to a ulong in C#?

So I am implementing a cryptography algorithm now. And I need to convert data to bytes and then split it in 64 bits. I do it by using BitConverter. But sometimes I don't have 8 bytes in the end of a message and I wonder how to convert less than 8…
2
votes
3 answers

C++ equivalent of BitConverter

I'm trying to read the PE headers of a file to get some information. For .NET and C#, I'm using BitConverter to convert the Byte array obtained after having read the file to an integer equivalent. I wish to do the same with C++, but am not sure of…
user1173240
  • 1,455
  • 2
  • 23
  • 50
2
votes
2 answers

What byte order when BitConverter.IsLittleEndian = false

I'm storing numbers in their byte equivalent format, using the least number of bytes possible. With the range 65535 through 16777215, BitConverter gives me a 4 byte array, but I want to only store 3 bytes. For the code below, my array is [0]254,…
winnt93
  • 43
  • 1
  • 6
1 2
3
9 10