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
68
votes
5 answers

Byte[] to ASCII

I received the contents of a text file returned in binary values: Byte[] buf = new Byte[size]; stream = File.InputStream; stream.Read(buf, 0, size); How can I convert this to ASCII?
The Mask
  • 17,007
  • 37
  • 111
  • 185
44
votes
8 answers

Converting from hex to string

I need to check for a string located inside a packet that I receive as byte array. If I use BitConverter.ToString(), I get the bytes as string with dashes (f.e.: 00-50-25-40-A5-FF). I tried most functions I found after a quick googling, but most of…
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248
25
votes
5 answers

Convert binary string to binary or decimal value

Is there any function to convert binary string into binary or decimal value? If I have a binary string 000101, what should I do to convert it into 5?
LifeWorks
  • 396
  • 1
  • 4
  • 12
18
votes
9 answers

C# generics: cast generic type to value type

I have a generic class which saves value for the specified type T. The value can be an int, uint, double or float. Now I want to get the bytes of the value to encode it into an specific protocol. Therefore I want to use the method…
rittergig
  • 715
  • 1
  • 5
  • 16
16
votes
5 answers

Fast casting in C# using BitConverter, can it be any faster?

In our application, we have a very large byte-array and we have to convert these bytes into different types. Currently, we use BitConverter.ToXXXX() for this purpose. Our heavy hitters are, ToInt16 and ToUInt64. For UInt64, our problem is that the…
SomethingBetter
  • 1,294
  • 3
  • 16
  • 32
13
votes
1 answer

How does the GetBytes function work?

I wrote my own class which converts C# standard primitives into byte arrays. Later on, I took a look at the BitConverter class source, to see how pros did it. My code example: public static byte[] getBytes(short value) { byte[] bytes = new…
Scavs
  • 673
  • 1
  • 5
  • 16
11
votes
11 answers

How to convert unsigned integer to signed integer without OverflowException

I would like to be able to convert a high-valued unsigned-integer (a value that uses the highest-order bit) to a signed-integer. In this case, I don't care that the value is higher than the maximum value of the signed integer type. I just want it…
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
9
votes
7 answers

What are the use-cases for IsLittleEndian in BitConverter class?

I was so happy when I discovered IsLittleEndian field in BitConverter. I thought of course it should be there and I should be able to specify whatever endian I like. Well, my happiness didn’t last long. Spent some time till found out that there is…
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
8
votes
2 answers

Converting hex to string in C?

Hello I am using digi dynamic c. I am trying to convert this in to string char readingreg[4]; readingreg[0] = 4a; readingreg[1] = aa; readingreg[2] = aa; readingreg[3] = a0; Currently when I do printf statements it has to be like this: printf("This…
Dale Reed
  • 93
  • 1
  • 2
  • 4
6
votes
6 answers

About the "GetBytes" implementation in BitConverter

I've found that the implementation of the GetBytes function in .net framework is something like: public unsafe static byte[] GetBytes(int value) { byte[] bytes = new byte[4]; fixed(byte* b = bytes) *((int*)b) = value; return…
derekhh
  • 5,272
  • 11
  • 40
  • 60
6
votes
2 answers

Improve performance of Bitconverter.ToInt16

I am collecting data from a USB device and this data has to go to an audio output component. At the moment I am not delivering the data fast enough to avoid clicks in the output signal. So every millisecond counts. At the moment I am collecting the…
Tom
  • 527
  • 1
  • 8
  • 28
6
votes
2 answers

.NET Portable library missing BitConverter.DoubleToInt64Bits, replacement very slow

I am developing a portable class library in C# and I want to bit convert a double to a long. The most straightforward solution to this issue would be to use the BitConverter.DoubleToInt64Bits method, but unfortunately this method is not available in…
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
5
votes
3 answers

Byte array to float

I am trying to cast a float into a byte array of length 4, and then back again. But I it doesn's seems to work. Here's what I've done: byte[] b = BitConverter.GetBytes(90); float fb = BitConverter.ToSingle(b, 0); I expected fb = 90, but it's…
random
  • 487
  • 1
  • 9
  • 19
5
votes
1 answer

Byte array to int C#

I am triyng to convert byte array into an int value however I am getting an exception: "Destination array is not long enough to copy all the items in the collection. Check array index and length." the exception is on line: int length =…
Barak Rosenfeld
  • 394
  • 5
  • 18
5
votes
8 answers

How to convert last 4 bytes in an array to an integer?

If I have an Uint8Array array in JavaScript, how would I get the last four bytes and then convert that to an int? Using C# I would do something like this: int count = BitConverter.ToInt32(array, array.Length - 4); Is there an inequivalent way to do…
Joey Morani
  • 25,431
  • 32
  • 84
  • 131
1
2 3
9 10