I have a database column which type is bytea. It contains floats converted as byte array (4 bytes per one float) and encoding is Escape. I’m be able to retrieve corresponding bytea string using substring function.
My question is how can I convert bytea string to float inside a SQL function. Earlier I converted to float in C# side. I used dataReader.getByte method to retrieve bytes and then Converted to float using BitConverter.ToSingle (.Net build in class) method.
Now I can’t use intermediate component as Npqsql driver. I want SQL to directly convert bytea into floats and return the corresponding number when execute a query from 3rd party application.
Thanks
Amila
For this purpose the best possible solution is to convert into bytes using IEEE754-1985 standard using SQL commands.
First it is required to check for special cases defined by IEEE754-1985 standard. Then just follow the standard algorithm to convert if it is not in any special cases. Sample code is below.
Inputs are
bytea_value bytea, is_little_endian booleanthen divide into 4 bytes as below:Then get the binary value by considering little endian or big endian
Now check for special cases:
If it does not belong to any special cases just convert it as below: