convert float to hex string
floathex.h
- Committer:
- agrosmart
- Date:
- 2016-12-15
- Revision:
- 0:8638868343e7
File content as of revision 0:8638868343e7:
#include <string> string float4Hex(float value) //Deja 4 Espacios para tener en cuenta el signo, para las temperaturas { int signo=0; // para las temperaturas int ent1 =0; int dec1=0; char value2[5] = "0"; if(value<0) { signo = 1; // 1 es negativo, 0 es positivo value = value * -1; } else { signo = 0; } ent1 = value; dec1 = (value - ent1)*16; //solo queda un decimal. para que queden dos decimales hay que multiplicar decimal por 10 sprintf(value2, "%.1X%.2X%.1X", signo, ent1,dec1); //1 de signo, 2 de entero, 1 de decimal value=0; return value2; } string float3dHex(float value) //Deja 3 espacios, para 2 enteros con 1 decimal { int ent1 =0; int dec1=0; char value2[5] = "0"; ent1 = value; dec1 = (value - ent1)*16; sprintf(value2, "%.2X%.1X", ent1,dec1); //2 de entero, 1 de decimal value=0; return value2; } string float2dHex(float value) //Deja 2 espacios, para enteros con 1 decimal { int ent1 =0; int dec1=0; char value2[5] = "0"; ent1 = value; dec1 = (value - ent1)*16; sprintf(value2, "%.1X%.1X", ent1,dec1); // 1 de entero, 1 de decimal value=0; return value2; } string float3Hex(float value) //Deja 3 espacios, para 3 enteros { int ent1 =0; char value2[5] = "0"; ent1 = value; sprintf(value2, "%.3X", ent1); //3 de entero value=0; return value2; } string float2Hex(float value) //Deja 2 espacios, para 2 enteros { int ent1 =0; char value2[5] = "0"; ent1 = value; sprintf(value2, "%.2X", ent1); //2 de entero value=0; return value2; }