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
0
votes
1 answer

Passing a hex string in a textbox.text property to BitConverter.GetBytes as hex in C#

I am reading values from sensors and receiving two 16 bit Hex values back in string format "417D" and "8380" for example. I cannot seem to find a way in C# to parse the strings to split them up into a byte array ( 41,7D,83,80 ) preserving the hex…
0
votes
3 answers

Is there a built in function to expand a byte[] leaving zeros at the beginning?

I've got this code BitConverter.GetBytes(width).CopyTo(resultBytes, 0); If the width is 12 it returns one byte not 4, is there a built in function to resize the array leaving 0's at the beginning to output [0, 0, 0, 12] instead of [12].
trinalbadger587
  • 1,905
  • 1
  • 18
  • 36
0
votes
0 answers

C# convert hex-string to decimal

I started my project to get UID of Mifare 1k Classic Cards - this is working really good. Now I have to put my UID to a mySQL database. I converted my ByteArray to string - this works as well. private string getcardUID() …
0
votes
1 answer

Index out of range at "int msgLength = BitConverter.ToInt32(gzBuffer, 0);"

I am currently working on some game made in C# XNA. Since I need to send a huge chunk of data over net (bout 96kb), I am using some string compressor/decompressor code, which I found on the internet. The code looks like this: public static…
Mark
  • 133
  • 1
  • 2
  • 8
0
votes
1 answer

Java BitConverter equivalent

Having the following block in C# using (var nuq = new RNGCryptoServiceProvider()) { var data = new byte[4]; nuq.GetBytes(data); return BitConverter.ToUInt32(data, 0).ToString(CultureInfo.InvariantCulture); } I want to convert this in…
sarbo
  • 1,661
  • 6
  • 21
  • 26
0
votes
0 answers

Reading from port, bitconverter wrong value

I'm using proximity card reader to read card number. Using producer software, when I read I'm getting this number: BF 1C 08 04 01 But using my application this one: 3F-1C-08-04-01 Code: void sp_DataReceived(object sender,…
user13657
  • 745
  • 3
  • 17
  • 36
0
votes
1 answer

GetBytes returning different size than data byte actually is

The following code iterates through a List() however the problem is that for some reason, sometimes the lengthBuffer is a different size of the byteData. You can see in the DebugPrintInGame method that I print the BitConverter.ToInt32()…
Euthyphro
  • 700
  • 1
  • 9
  • 26
0
votes
1 answer

Convert byte array to float 32bit and 64bit?

How would you convert 4 bytes in a byte array to a 32-bit float? Then how would you do the same for 8 bytes in a byte array for a 64-bit float(double)? I noticed the BitConverter class in C# has this, however I am curious about the math behind…
user3519915
  • 129
  • 1
  • 3
  • 13
0
votes
1 answer

Whitespace Inserted When Using BitConverter

I am having the problem that whitespace of some sort is being inserted between characters when I am converting a Queue list into a string for comparison. I do not think that they are actual whitespace characters, however, because the Queue…
Rawrcasm
  • 75
  • 2
  • 9
0
votes
1 answer

How to store integers efficiently in bytes ?

I'm looking for a good and efficient way to store integers in bytes. The situation is the following: I have two integers, Value 1 is "1857" (11bit) and Value 2 is "14" (4bit) and 2 bytes (16bit). What I'm looking for, is to store the 2 integers in…
user297225
0
votes
1 answer

Is there a better way to detect endianness in .NET than BitConverter.IsLittleEndian?

It would be nice if the .NET framework just gave functions/methods from the BitConverter class that just explicitly returned an array of bytes in the proper requested endianness. I've done some functions like this in other code, but is there a…
0
votes
2 answers

C# Byte[] to long reverse not working

Why is this program not working? I convert a byte array to long. Then from the long I convert back to a byte array. The resulting byte array is not the same as original. class Program { static void Main(string[] args) { byte[]…
Jonathan
  • 93
  • 1
  • 2
  • 9
0
votes
1 answer

C# convert one's complement bits to a two's complement long?

In C#, how can I convert a 64 bit ones complement number (represented as a long or ulong) to a signed two's complement long? For reference, I'm trying to implement ULP-based double comparison using BitConverter.DoubleToInt64Bits().
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
0
votes
4 answers

Why does BitConverter shrink my already allocated array? (I'm trying to prevent a two's complement issue)

I am allocating an array that is intentionally bigger than the result of BitConverter.GetBytes. My goal is to leave the last byte empty so that I can prevent this number from being seen as the two's compliment and have tempPosBytes2[ When I run…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
0
votes
5 answers

Writing a 2-byte integer to a stream?

I'm trying to interact with an application over the network which uses a simple protocol. I have to send a header that looks like this: 2 bytes = Data Length (including Request Type) 1 byte = Request Type I'm taking both parameters as…
David Brown
  • 35,411
  • 11
  • 83
  • 132