# How to convert 2 byte data to integer?

172 views (last 30 days)
Tharindu Weerakoon on 26 Jan 2015
Commented: mohd akmal masud on 20 Feb 2018
I have a two byte data (unsigned) as array.
e.g. x=[255 67]
I read the data from a sensor giving a stream of byte data (unsigned 0 to 255). From them I select corresponding two-byte of data set for necessary parameter calculation.
I want to convert this into an Integer value or a double value to do real mathematic calculations.
I tried with.
x=uint8([1 0]) y=typecast(x,'uint32') % but this gives an error.
if I use: x=uint16([255 67]) y=typecast(x,'uint32')
4391167
I don't how to check the answer is correct or not. Or the conversion syntax is correct?
Can anyone give me the code for correct 2-byte data conversion to integer..!

Guillaume on 26 Jan 2015
Edited: Guillaume on 26 Jan 2015
You were nearly there. combining two bytes (uint8) does not make a 32-bit number (uint32), but a 16-bit numbers (uint16), so:
x = [255 67];
y = typecast(uint8(x), 'uint16');
You haven't specified the endianness of your data, nor that of your machine. If the two don't match, you'll have to swapbytes afterward:
truey = swapbytes(y); %if one is big-endian and the other litte-endian
Tharindu Weerakoon on 27 Jan 2015
Hi Guillaume,
Actually I have the byte array in my C# program like: (simply, not the real commands) byte[] x; x={20, 0, 1, 255, 178, 0, 35, 255, 138}
Y1: convert the byte value 20 (lower byte from right side) to integer (int in C#) and answer should equal to 20.
Y2: two byte array [0 1] (1: lower byte) should be converted to int16 and then the answer should be equal to 1
Y3: for the two byte array [255 178] (lower byte is 178)should be converted to the int16 and then the answer divided by 32767.0f/360.0f in C# should give the answer looks like -0.93387.
Y4: for the two byte array [0 35] (lower byte is 35) should be converted to the int16 and then dividing by 32767.0f/360.0f should give the answer looks like 0.56032
Y5: for the two byte array [255 138] (lower byte is 138) should be converted to int16 and then it is divided by 32767.0f/3.0f should give the answer looks like -0.01053.
For your reference I have put my C# code here to get more clear understanding about my problem.
/// <summary>
/// Constructor
/// </summary>
/// <param name="X"></param>
public DataConversion(byte[] X)
{
//Decoding
int index = 0;
Y1= (int)X[index++];
Y2 = (Decode2byte(X[index++], X[index++]) == 1);
double bias = 0;
bias = 32767.0f / 360.0f;
Y3 = Decode2byte(X[index++], X[index++]) / bias;
Y4 = Decode2byte(X[index++], X[index++]) / bias;
bias = 32767.0f / 3.0f;
Y5= Decode2byte(X[index++], X[index++]) / bias;
}
/// <summary>
/// Two byte data conversion to int16
/// </summary>
/// <param name="data1"></param>
/// <param name="data2"></param>
/// <returns></returns>
private Int16 Decode2byte(byte data1, byte data2)
{
return (Int16)(data1 << 8 | data2);
}
Could you please tell me how can I do this in matlab

Image Analyst on 26 Jan 2015
Edited: Image Analyst on 26 Jan 2015
I don't know which element of x is the upper or lower byte, so I did it both ways. Try this:
x=[255 67]
% Display bytes in binary.
dec2bin(x(1))
dec2bin(x(2))
% If x(1) is the most significant byte:
x_uint16 = uint16(256*x(1) + x(2))
dec2bin(x_uint16)
% If x(1) is the least significant byte:
x_uint16 = uint16(256*x(2) + x(1))
dec2bin(x_uint16)
Note that x must be a double or a uint16 variable, not a char or uint8 variable or else you can't multiply by 256.
In the command window, you'll see:
x =
255 67
ans =
11111111
ans =
1000011
x_uint16 =
65347
ans =
1111111101000011
x_uint16 =
17407
ans =
100001111111111
You can replace uint16 by double if you want the data type to be double instead of uint16.
Tharindu Weerakoon on 27 Jan 2015
Edited: Tharindu Weerakoon on 27 Jan 2015
Hi,
Actually I get the data as unsigned byte data (uint8) (0 to 255 ) and I want then to convert into signed int16 (-2^15 to 2^15-1)data.
I have the byte array in my C# program like: (simply, not the real commands) byte[] x; x={20, 0, 1, 255, 178, 0, 35, 255, 138}
Y1: convert the byte value 20 (lower byte from right side) to integer (int in C#) and answer should equal to 20.
Y2: two byte array [0 1] (1: lower byte) should be converted to int16 and then the answer should be equal to 1
Y3: for the two byte array [255 178] (lower byte is 178)should be converted to the int16 and then the answer divided by 32767.0f/360.0f in C# should give the answer looks like -0.93387.
Y4: for the two byte array [0 35] (lower byte is 35) should be converted to the int16 and then dividing by 32767.0f/360.0f should give the answer looks like 0.56032
Y5: for the two byte array [255 138] (lower byte is 138) should be converted to int16 and then it is divided by 32767.0f/3.0f should give the answer looks like -0.01053.
For your reference I have put my C# code here to get more clear understanding about my problem.
/// <summary>
/// Constructor
/// </summary>
/// <param name="X"></param>
public DataConversion(byte[] X)
{
//Decoding
int index = 0;
Y1= (int)X[index++];
Y2 = (Decode2byte(X[index++], X[index++]) == 1);
double bias = 0;
bias = 32767.0f / 360.0f;
Y3 = Decode2byte(X[index++], X[index++]) / bias;
Y4 = Decode2byte(X[index++], X[index++]) / bias;
bias = 32767.0f / 3.0f;
Y5= Decode2byte(X[index++], X[index++]) / bias;
}
/// <summary>
/// Two byte data conversion to int16
/// </summary>
/// <param name="data1"></param>
/// <param name="data2"></param>
/// <returns></returns>
private Int16 Decode2byte(byte data1, byte data2)
{
return (Int16)(data1 << 8 | data2);
}
Could you please tell me how can I do this in matlab

Tharindu Weerakoon on 27 Jan 2015
Edited: Tharindu Weerakoon on 27 Jan 2015
Hello everyone,
Thanks a lot for the help. Finally I could do it.
% Two byte array [lower bit , higher bit] x=uint8[178 255]
% convert to signed int16 y=typecast(x,'int16')
% if want to use this for other mathematical calculations, it should be converted to the double. z=double(y)
% simple math operation. int16 range value is -32768 ~32767 Out=(z/32767)*250
Result will be x = 178 255
y = -78
z = -78
out = -0.595110934781945
Thanks again all
mohd akmal masud on 20 Feb 2018
hi all,
i have image dicom 16 bit. Like below my image:
>> whos P
Name Size Bytes Class Attributes
P 256x256 131072 int16
My problem is, 16 bit image can stored pixel value till 32767 only. Now i want change it to 32 bit or 64 bit so that the pixel value can stored more than that, and corresponding how much activity radionuclides i used to diagnosed patient.
can you help to convert that using matlab? or anyway to solve it?