I am using following approach for converting byte to short,
short shortvalue;
nTempByteArr[0] = RawDataQueue.poll();
nTempByteArr[1] = RawDataQueue.poll();
ShortValue = (short) (((nTempByteArr[1] & 0xff) << 8) | (nTempByteArr[0] & 0xff));
How to convert short value to exact same two byte nTempByteArr[0] and nTempbyteArr[1]
I have tried:
nByteArr[0] = (byte)((ShortValue & 0xff00) >> 8);
nByteArr[1] = (byte)(ShortValue & 0xff);
nByteArr[0] = (byte)( ShortValue & 0xff);
nByteArr[1] = (byte)((ShortValue & 0xff00) >> 8);
Please help me...!!!!!!!!!!!!!
We can use another approach without bit shift to convert bytes to short without shift by using java.nio.ByteBuffer
ByteBuffer bb = ByteBuffer.allocate(2);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.put(nTempByteArr[1]);
bb.put(nTempByteArr[0]);
short shortVal = bb.getShort(0);
and we can use the get function of ByteBuffer will help us back to get bytes
bb.putShort(ShortValue);
nTempByteArr[0] = bb.get(0);
nTempByteArr[1] = bb.get(1);
short s = ...
byte b1 = (byte)((s >> 8) & 0xff);
byte b2 = (byte)(s & 0xff);
try this
short to byte
(number >>> 8) & 0xFF;
(number >>> 0) & 0xFF;
byte to short
(short) ((portArray[0] << 8) | (portArray[1]) & 0xff);
My Solution is correct . There was some other problem because of that only it was not able to get correct byte
Related
reference to converting sql server rowversion to long or ulong?
i can convert SQL RowVersion to ulong by code:
static ulong BigEndianToUInt64(byte[] bigEndianBinary)
{
return ((ulong)bigEndianBinary[0] << 56) |
((ulong)bigEndianBinary[1] << 48) |
((ulong)bigEndianBinary[2] << 40) |
((ulong)bigEndianBinary[3] << 32) |
((ulong)bigEndianBinary[4] << 24) |
((ulong)bigEndianBinary[5] << 16) |
((ulong)bigEndianBinary[6] << 8) |
bigEndianBinary[7];
}
Now, I am faced with a problem, how to convert ulong to byte [8]?
I save the value of rowversion to a file, then read it and use it to make the query. the query parameter should be byte[] , not ulong . otherwise , there is an error will be raised.
i got an answer from BigEndian.GetBytes Method (Int64)
thanks all.
public static byte[] GetBytes(this ulong value)
{
return new[]
{
(byte)(value >> 56),
(byte)(value >> 48),
(byte)(value >> 40),
(byte)(value >> 32),
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)(value)
};
}
I have 4 byte data stream, I know at what bite I wanted to split them and assign them to a different variable. keeping in mind the data I receive is in hex format. let's say,
P_settings 4bytes p_timeout [6:0]
p_s_detected[7]
p_o_timeout [14:8]
p_o_timeout_set [15]
override_l_lvl [23:16]
l_b_lvl [31:24]
above P_settings is 4 bytes and I wanted to split them byte into bits like p_timeout [6:0] requires 7 bits of those 4 byte.
Currently, the implementation I have tried is..for just one byte split into bits.
var soch = ((b_data>> 0)& 0x7F ); if i want first 7 bits
how do I do it for 4 byte streams
Try code like this. You said input was a stream.
public class P_Settings
{
byte p_timeout; //[6:0]
Boolean p_s_detected; //[7]
byte p_o_timeout; // [14:8]
Boolean p_o_timeout_set; // [15]
byte override_l_lvl; //[23:16]
byte l_b_lvl; //[31:24]
public P_Settings(Stream data)
{
byte input = (byte)(data.ReadByte() & 0xff);
p_timeout = (byte)(input & 0x7F);
p_s_detected = (input & 0x80) == 0 ? false : true;
input = (byte)(data.ReadByte() & 0xff);
p_o_timeout = (byte)(input & 0x7F);
p_o_timeout_set = (input & 0x80) == 0 ? false : true;
override_l_lvl = (byte)(data.ReadByte() & 0xff);
l_b_lvl = (byte)(data.ReadByte() & 0xff);
}
}
SO it turns out it much easier that i thought..
1) separate them by single byte and put them in a buffer and & operate them individually and you will get the data. thanks for all the support.
**
byte input = (byte)( buffer[10]);//1 byte
var p_timeout = (byte)(input & 0x7F);
var p_s_detected = (input & 0x80) == 0 ? false : true;
input = (byte)( buffer[11]);//1 byte
var p_o_timeout = (byte)(input & 0x7F);
var p_o_timeout_set = (input & 0x80) == 0 ? false : true;
var override_l_lvl = (byte)(buffer[12] & 0xff);//1 byte
var l_b_lvl = (byte)(buffer[13] & 0xff); //1 byte
**
I suspect this is an easy one.
I need to get a number from the first 4 bits and another number from the last 12 bits
of 2 bytes.
So here is what I have but it doesn't seem to be right:
byte[] data = new byte[2];
//assume byte array contains data
var _4bit = data[0] >> 4;
var _12bit = data[0] >> 8 | data[1] & 0xff;
data[0]>>8 is 0. Remember that your data is defined as byte[] so it has 8bits per single item, so you are effectively cutting ALL bits off the data[0].
You want rather to take the lowest 4 bits from that byte by bitwise AND (00001111 = 0F) and then shift it leftwards as needed.
So try this:
var _4bit = data[0] >> 4;
var _12bit = ((data[0] & 0x0F) << 8) | (data[1] & 0xff);
It's also worth noting that the last & 0xFF is not needed, as the data[1] is already a byte.
On bits, step by step:
byte[2] data = { aaaabbbb, cccccccc }
var _4bit = data[0] >> 4;
= aaaabbbb >> 4
= 0000aaaa
var _12bit = ( (data[0] & 0x0F) << 8) | ( data[1] & 0xff);
= ((aaaabbbb & 0x0F) << 8) | (cccccccc & 0xff);
= ( 0000bbbb << 8) | ( cccccccc );
= ( 0000bbbb000000000 ) | ( cccccccc );
= 0000bbbbcccccccc;
BTW. also, note that results of & and | operators are typed as int, so 32bits, I've omitted the zeroes for clarity and written it as 8bit only to make it brief!
I'm trying to send the Length of a Byte array to my server, so that it knows how much data to read.
I get the length of the Byte[] message array using int messageLength = message.Length
How do I represent this integer messageLength a four-byte integer?
Use BitConvertor BitConverter.GetBytes(message.Length);
Use the BitConverter.GetBytes(int32) class
You can use
int length = message.Length;
BitConvert.GetBytes(length);
http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx
message[0] = length & 0xFF;
message[1] = (length >> 8) & 0xFF;
message[2] = (length >> 16) & 0xFF;
message[3] = (length >> 24) & 0xFF;
To recover it...
int length = message[0] | (message[1] << 8) | (message[2] << 16) | (message[3] << 24);
A byte in C# is 8 bits.. 8 bits * 4 bytes = 32 bits.. so you want to use a System.Int32.
I have this function in C# to convert a little endian byte array to an integer number:
int LE2INT(byte[] data)
{
return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
}
Now I want to convert it back to little endian..
Something like
byte[] INT2LE(int data)
{
// ...
}
Any idea?
Thanks.
The BitConverter class can be used for this, and of course, it can also be used on both little and big endian systems.
Of course, you'll have to keep track of the endianness of your data. For communications for instance, this would be defined in your protocol.
You can then use the BitConverter class to convert a data type into a byte array and vice versa, and then use the IsLittleEndian flag to see if you need to convert it on your system or not.
The IsLittleEndian flag will tell you the endianness of the system, so you can use it as follows:
This is from the MSDN page on the BitConverter class.
int value = 12345678; //your value
//Your value in bytes... in your system's endianness (let's say: little endian)
byte[] bytes = BitConverter.GetBytes(value);
//Then, if we need big endian for our protocol for instance,
//Just check if you need to convert it or not:
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes); //reverse it so we get big endian.
You can find the full article here.
Hope this helps anyone coming here :)
Just reverse it, Note that this this code (like the other) works only on a little Endian machine. (edit - that was wrong, since this code returns LE by definition)
byte[] INT2LE(int data)
{
byte[] b = new byte[4];
b[0] = (byte)data;
b[1] = (byte)(((uint)data >> 8) & 0xFF);
b[2] = (byte)(((uint)data >> 16) & 0xFF);
b[3] = (byte)(((uint)data >> 24) & 0xFF);
return b;
}
Just do it in reverse:
result[3]= (data >> 24) & 0xff;
result[2]= (data >> 16) & 0xff;
result[1]= (data >> 8) & 0xff;
result[0]= data & 0xff;
Could you use the BitConverter class? It will only work on little-endian hardware I believe, but it should handle most of the heavy lifting for you.
The following is a contrived example that illustrates the use of the class:
if (BitConverter.IsLittleEndian)
{
int someInteger = 100;
byte[] bytes = BitConverter.GetBytes(someInteger);
int convertedFromBytes = BitConverter.ToInt32(bytes, 0);
}
BitConverter.GetBytes(1000).Reverse<byte>().ToArray();
Depending on what you're actually doing, you could rely on letting the framework handle the details of endianness for you by using IPAddress.HostToNetworkOrder and the corresponding reverse function. Then just use the BitConverter class to go to and from byte arrays.
Try using BinaryPrimitives in System.Buffers.Binary, it has helper methods for reading and writing all .net primitives in both little and big endian form.
byte[] IntToLittleEndian(int data)
{
var output = new byte[sizeof(int)];
BinaryPrimitives.WriteInt32LittleEndian(output, data);
return output;
}
int LittleEndianToInt(byte[] data)
{
return BinaryPrimitives.ReadInt32LittleEndian(data);
}
public static string decimalToHexLittleEndian(int _iValue, int _iBytes)
{
string sBigEndian = String.Format("{0:x" + (2 * _iBytes).ToString() + "}", _iValue);
string sLittleEndian = "";
for (int i = _iBytes - 1; i >= 0; i--)
{
sLittleEndian += sBigEndian.Substring(i * 2, 2);
}
return sLittleEndian;
}
You can use this if you don't want to use new heap allocations:
public static void Int32ToFourBytes(Int32 number, out byte b0, out byte b1, out byte b2, out byte b3)
{
b3 = (byte)number;
b2 = (byte)(((uint)number >> 8) & 0xFF);
b1 = (byte)(((uint)number >> 16) & 0xFF);
b0 = (byte)(((uint)number >> 24) & 0xFF);
}