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
4
votes
2 answers

BitConverter VS ToString for Hex

Just wondering if someone could explain why the two following lines of code return "different" results? What causes the reversed values? Is this something to do with endianness? int.MaxValue.ToString("X") //Result:…
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
4
votes
5 answers

Converting raw byte data to float[]

I have this code for converting a byte[] to float[]. public float[] ConvertByteToFloat(byte[] array) { float[] floatArr = new float[array.Length / sizeof(float)]; int index = 0; for (int i = 0; i < floatArr.Length; i++) { …
user488792
  • 1,943
  • 7
  • 31
  • 38
4
votes
2 answers

Generic Method Using BitConverter.GetBytes

I'm trying to get the bytes for different objects of different types using BitConverter.GetBytes. In order to do this, I'd like to write a generic extension method, rather than write individual extension methods for every type I want to work with…
Aaron Thomas
  • 5,054
  • 8
  • 43
  • 89
4
votes
5 answers

Is there a less painful way to GetBytes for a buffer not starting at 0?

I am having to deal with raw bytes in a project and I need to basically do something like this byte[] ToBytes(){ byte[] buffer=new byte[somelength]; byte[] tmp; tmp=BitConverter.GetBytes(SomeShort); buffer[0]=tmp[0]; buffer[1]=tmp[1]; …
Earlz
  • 62,085
  • 98
  • 303
  • 499
4
votes
2 answers

How to reverse a RectangleF to a Picasa face hash

Here are the details for what Picasa stores as a hash. It stores them like this: faces=rect64(54391dc9b6a76c2b),4cd643f64b715489 [DSC_2289.jpg] faces=rect64(1680000a5c26c82),76bc8d8d518750bc Info on the web says this: The number encased in rect64()…
esac
  • 24,099
  • 38
  • 122
  • 179
4
votes
1 answer

Security and stability issues around BitConverter.Int64BitsToDouble in C#

I'm writing a custom deserialisation system for packetising data over the network, and am serialising doubles in the following way: private static string EncodeDouble(double raw) { long value = BitConverter.DoubleToInt64Bits(raw); return…
Polynomial
  • 27,674
  • 12
  • 80
  • 107
3
votes
2 answers

C# BitConverter problems

Normaly if you want for example represent 5 in byte array it will be smth like {0x00,0x00,0x00,0x05} but BitConverter gives me reversed array({0x05,0x00,0x00,0x00}) Why it is so and where I'm wrong?
Ruslan F.
  • 5,498
  • 3
  • 23
  • 42
3
votes
1 answer

How can I convert from a Byte Array to Generic Array?

Disclaimer - Since I have a working solution, this question perhaps crosses the line to code review, however I'm convinced I'm reinventing the wheel and a better solution exists. Context I am working with a low level communication protocol whereby I…
George Kerwood
  • 1,248
  • 8
  • 19
3
votes
2 answers

Converting Int32 to 24-bit signed integer

I have a need to convert an Int32 value to a 3-byte (24-bit) integer. Endianness remains the same (little), but I cannot figure out how to move the sign appropriately. The values are already constrained to the proper range, I just can't figure out…
drharris
  • 11,194
  • 5
  • 43
  • 56
3
votes
3 answers

xxHash convert resulting in hash too long

I'm using xxHash for C# to hash a value for consistency. ComputeHash returns a byte[], but I need to store the results in a long. I'm able to convert the results into an int32 using the BitConverter. Here is what I've tried: var xxHash = new…
Desmond Nzuza
  • 130
  • 1
  • 11
3
votes
1 answer

Convert byte[] array to a short[] array with half the length

I have a byte[200] that is read from a file, representing a short[100] in little-endian format. This is how I read it: using (FileStream fs = new FileStream(_path, FileMode.Open, FileAccess.Read)) { //fs.Seek(...) byte[] record = new…
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
3
votes
2 answers

Alternative to BitConverter.ToInt32

I'm using BitConverter.ToInt32 to pack 3 byte values into an int, like so: byte R = 0; byte G = 0; byte B = 0; int i = BitConverter.ToInt32(new byte[] { R, G, B, 0 }, 0); Is there a faster way to do this that doesn't involve the creation of a new…
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
3
votes
0 answers

Is there a equivalent of python's struct.pack in C#?

Is there a more or less equivalent of Pythons' struct pack in C# ? What i basically need to do is 'rewriting' a part of a python-script in C#. The following functions are required to prepare some text, so that it can be send via a socket... def…
ThexBasic
  • 715
  • 1
  • 6
  • 24
3
votes
5 answers

C#, BitConverter.ToUInt32, incorrect value

I try to convert an Ip Address to a long value : byte[] Ip = new byte[4] { 192, 168, 1, 0 }; UInt32 Ret1 = (((UInt32)Ip[0]) << 24) | (((UInt32)Ip[1]) << 16) | (((UInt32)Ip[2]) << 8) | …
user2705397
  • 61
  • 1
  • 6
3
votes
2 answers

Converting byte[] to array issue

Please check this code float f = BitConverter.ToSingle(new byte[] { 0xBF, 0x04, 0x8E, 0xFF }, 0); byte[] b = BitConverter.GetBytes(f); this yeilds a strange result. b will be { 0xBF, 0x04, 0xCE, 0xFF } I guess it is because the value of f is…
TheDude
  • 257
  • 1
  • 9
1
2
3
9 10