convert float to hex string
floathex.h@0:8638868343e7, 2016-12-15 (annotated)
- Committer:
- agrosmart
- Date:
- Thu Dec 15 11:57:02 2016 +0000
- Revision:
- 0:8638868343e7
library float2hex
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
agrosmart | 0:8638868343e7 | 1 | #include <string> |
agrosmart | 0:8638868343e7 | 2 | |
agrosmart | 0:8638868343e7 | 3 | string float4Hex(float value) //Deja 4 Espacios para tener en cuenta el signo, para las temperaturas |
agrosmart | 0:8638868343e7 | 4 | { |
agrosmart | 0:8638868343e7 | 5 | int signo=0; // para las temperaturas |
agrosmart | 0:8638868343e7 | 6 | int ent1 =0; |
agrosmart | 0:8638868343e7 | 7 | int dec1=0; |
agrosmart | 0:8638868343e7 | 8 | char value2[5] = "0"; |
agrosmart | 0:8638868343e7 | 9 | |
agrosmart | 0:8638868343e7 | 10 | if(value<0) { |
agrosmart | 0:8638868343e7 | 11 | signo = 1; // 1 es negativo, 0 es positivo |
agrosmart | 0:8638868343e7 | 12 | value = value * -1; |
agrosmart | 0:8638868343e7 | 13 | } else { |
agrosmart | 0:8638868343e7 | 14 | signo = 0; |
agrosmart | 0:8638868343e7 | 15 | } |
agrosmart | 0:8638868343e7 | 16 | ent1 = value; |
agrosmart | 0:8638868343e7 | 17 | dec1 = (value - ent1)*16; //solo queda un decimal. para que queden dos decimales hay que multiplicar decimal por 10 |
agrosmart | 0:8638868343e7 | 18 | sprintf(value2, "%.1X%.2X%.1X", signo, ent1,dec1); //1 de signo, 2 de entero, 1 de decimal |
agrosmart | 0:8638868343e7 | 19 | value=0; |
agrosmart | 0:8638868343e7 | 20 | return value2; |
agrosmart | 0:8638868343e7 | 21 | } |
agrosmart | 0:8638868343e7 | 22 | string float3dHex(float value) //Deja 3 espacios, para 2 enteros con 1 decimal |
agrosmart | 0:8638868343e7 | 23 | { |
agrosmart | 0:8638868343e7 | 24 | int ent1 =0; |
agrosmart | 0:8638868343e7 | 25 | int dec1=0; |
agrosmart | 0:8638868343e7 | 26 | |
agrosmart | 0:8638868343e7 | 27 | char value2[5] = "0"; |
agrosmart | 0:8638868343e7 | 28 | |
agrosmart | 0:8638868343e7 | 29 | ent1 = value; |
agrosmart | 0:8638868343e7 | 30 | dec1 = (value - ent1)*16; |
agrosmart | 0:8638868343e7 | 31 | sprintf(value2, "%.2X%.1X", ent1,dec1); //2 de entero, 1 de decimal |
agrosmart | 0:8638868343e7 | 32 | value=0; |
agrosmart | 0:8638868343e7 | 33 | return value2; |
agrosmart | 0:8638868343e7 | 34 | } |
agrosmart | 0:8638868343e7 | 35 | string float2dHex(float value) //Deja 2 espacios, para enteros con 1 decimal |
agrosmart | 0:8638868343e7 | 36 | { |
agrosmart | 0:8638868343e7 | 37 | int ent1 =0; |
agrosmart | 0:8638868343e7 | 38 | int dec1=0; |
agrosmart | 0:8638868343e7 | 39 | |
agrosmart | 0:8638868343e7 | 40 | char value2[5] = "0"; |
agrosmart | 0:8638868343e7 | 41 | |
agrosmart | 0:8638868343e7 | 42 | ent1 = value; |
agrosmart | 0:8638868343e7 | 43 | dec1 = (value - ent1)*16; |
agrosmart | 0:8638868343e7 | 44 | sprintf(value2, "%.1X%.1X", ent1,dec1); // 1 de entero, 1 de decimal |
agrosmart | 0:8638868343e7 | 45 | value=0; |
agrosmart | 0:8638868343e7 | 46 | return value2; |
agrosmart | 0:8638868343e7 | 47 | } |
agrosmart | 0:8638868343e7 | 48 | string float3Hex(float value) //Deja 3 espacios, para 3 enteros |
agrosmart | 0:8638868343e7 | 49 | { |
agrosmart | 0:8638868343e7 | 50 | int ent1 =0; |
agrosmart | 0:8638868343e7 | 51 | char value2[5] = "0"; |
agrosmart | 0:8638868343e7 | 52 | |
agrosmart | 0:8638868343e7 | 53 | ent1 = value; |
agrosmart | 0:8638868343e7 | 54 | sprintf(value2, "%.3X", ent1); //3 de entero |
agrosmart | 0:8638868343e7 | 55 | value=0; |
agrosmart | 0:8638868343e7 | 56 | return value2; |
agrosmart | 0:8638868343e7 | 57 | } |
agrosmart | 0:8638868343e7 | 58 | string float2Hex(float value) //Deja 2 espacios, para 2 enteros |
agrosmart | 0:8638868343e7 | 59 | { |
agrosmart | 0:8638868343e7 | 60 | int ent1 =0; |
agrosmart | 0:8638868343e7 | 61 | char value2[5] = "0"; |
agrosmart | 0:8638868343e7 | 62 | |
agrosmart | 0:8638868343e7 | 63 | ent1 = value; |
agrosmart | 0:8638868343e7 | 64 | sprintf(value2, "%.2X", ent1); //2 de entero |
agrosmart | 0:8638868343e7 | 65 | value=0; |
agrosmart | 0:8638868343e7 | 66 | return value2; |
agrosmart | 0:8638868343e7 | 67 | } |