byte array to double conversion in c# - c#

I have an array of three bytes, I want to convert array into double using c#. Kindly guide me.

Well, that depends on what you want the conversion to do.
You can convert 8 bytes (in the right format) into a double using BitConverter.ToDouble - but with only three bytes it's a bit odd - after all, a double has 64 bits of information, normally. How do those three bytes represent a number? What's the format, basically? When you've figured that out, the rest may well be easy.

Well a double is an array of 8 bytes, so with 3 bytes you won't have all the possible values.
To do what you want:
var myBytes[] = {0,0,0,0,0,1,1,2}; //assume you pad your array with enough zeros to make it 8 bytes.
var myDouble = BitConverter.ToDouble(myBytes,0);

Depends on what exactly is stored in the bytes, but you might be able to just pad the array with 5 bytes all containing 0 and then use BitConverter.ToDouble.

Related

Converting an entire byte array into a single digit hex string

I have a byte array with 9 elements. I need to convert the entire byte array into a single hex string.
Above is fairly straightforward to do however, I need to drop leading 0 if an element contains a decimal value less than 16 i.e. F or less. See example below.
byte[] myByteArr = {10,11,12,13,14,15,16,17,18,19};
"Regular" method to convert the above to hex should give me 0x0A0B0C0D0E0F10111213
What I actually need is 0xABCDEF10111213
Is there a quick method to just drop the upper nibble and take the lower nibble if none of the bits in the upper nibble are set ?
Thanks in advance!

BitConverter.GetBytes vs Convert.ToByte

Can anyone shine some light of the differences between these two? I don't know which one to use and can't really find any information on the differences of the two.
BitConverter.GetBytes Converts the specified data to an array of bytes. Whereas: Convert.ToByte Converts a specified value to an 8-bit unsigned integer.
You want to simply convert a number or string representation into an 8-bit unsigned integer? Then Convert.ToByte is what you need. If you want to get an array of bytes, maybe the number you're trying to represent can't be stored in 8-bits, then BitConverter.GetBytes is the method for you!
Source:
https://msdn.microsoft.com/en-us/library/system.bitconverter.getbytes(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.convert.tobyte(v=vs.110).aspx

Generate random int using random byte generator

I m using random generator what takes the length of random bytes as an input and returns byte array. What i need now is to convert that byte array to 8 digit integer and from that to a string.
byte[] randomData = this.GetRandomArray(4);
SecretCode = Math.Abs(BitConverter.ToInt32(randomData, 0)).ToString().Substring(0, 7);
But some occasions the int is shorter than 8 digits and my method fails. How can i make sure that the byte array generated can be converted to 8 digi int number?
One more option:
myString = BitConverter.ToUInt32(randomData, 0).toString("D8");
Note - using ToUInt32 is a more sensible approach than converting to signed integer and taking the absolute value (it also doubles the number of values you can generate since -123 and 123 will result in a different string output, which they won't if you use Math.Abs.); and the format "D8" should convert to eight digits including leading zeros.
See https://stackoverflow.com/a/5418425/1967396
You could just use <stringCode>.PadLeft(8, "0")
Are you sure that your method is failing on the Substring? As far as I can see there's a number of issues:
It'll fail if you don't get 4 bytes back (ArgumentException on BitConverter.ToInt32)
It'll fail if the string isn't long enough (your problem from above)
It'll truncate at seven chars, not eight, as you want.
You can use the PadLeft function to pad with zeros. If want eight then code should look like:
var s = Math.Abs(
BitConverter.ToInt32(randomData, 0))
.ToString()
.PadLeft(8, '0')
.Substring(0, 8);
For seven, replace the 8 with a 7.
You need to concatenate eight zeros before trying to take the Substring(), then take the last 8 characters.
StringBuffer s = new StringBuffer("00000000").append(Math.Abs(BitConverter.ToInt32(randomData, 0)).ToString());
SecretCode = s.substring(s.length()-7);
Your other option is to use a formatter to ensure the stringification of the bits returns leading zeros.

Converting Uint64 to 5 bytes and vice versa in c#

I have an application that expects 5 bytes that is derived of a number. Right now I am using Uint32 which is 4 bytes. I been told to make a class that uses a byte and a Uint32. However, im not sure how to combine them since they are numbers.
I figured the best way may be to use a Uint64 and convert it down to 5 bytes. However, I am not sure how that can be done. I need to be able to convert it to 5 bytes and a separate function in the class to convert it back to a Uint64.
Does anyone have any ideas on the best way to do this?
Thank you
Use BitConverter.GetBytes and then just remove the three bytes you don't need.
To convert it back use BitConverter.ToUInt64 remembering that you'll have to first pad your byte array with three extra (zero) bytes.
Just watch out for endianness. You may need to reverse your array if the application you are sending these bytes to expects the opposite endianess. Also the endianess will dictate whether you need to add/remove bytes from the start or end of the array. (check BitConverter.IsLittleEndian)

Showing a +255 byte in a byte[]

I really hope someone can help me.
I have a single byte[] that has to show the amount of bytes in die byte[] to follow. Now my value is above 255. Is there a way to display/enter a large number?
A byte holds a value from 0 to 255. To represent 299, you either have to use 2 bytes, or use a scheme (which the receiver will have to use as well) where the value in the byte is interpreted as more than its nominal value in order to expand the possible range of values. For instance, the value could be the length / 2. This would allow lengths of 0 - 510, but would allow only even lengths (odd length arrays would need a pad byte).
You can use two (or more) bytes to represent a number larger than 255. Is that what you want ?
short value = 2451;
byte[] data = BitConverter.GetBytes(value);
If this is needed in order to exchange data with some external system, remember to read about Endianness.
That depends on what you consider a good approach. You can perform some form of encoding to allow you store larger than 2 bytes worth of data. I.e. perhaps setting the first byte as 0xFF means you will consider the next byte as part of its data.
[0x01,0x0A,0xFF,0x0A]
Would be interpreted as 3 values of [1,10,265]

Categories

Resources