Autor Tema: Conversión de binario 32 bits a texto decimal con 10 caracteres  (Leído 10697 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Carlos

  • Moderador Global
  • ****
  • Mensajes: 294
¿Cómo convertir un número binario de 32 bits en un texto númerico decimal?
Programa para Arduino que convierte un entero largo (unsigned long int) en un texto con caracteres decimales que lo representa.

Código: (c) [Seleccionar]
#define BINARY_DIGITS   32
#define DECIMAL_DIGITS  10


void bin32_to_str(char *str, unsigned long bin) {
   char maxdig, digcnt, bitcnt;
   static char *p, carry;
 
   // Clear string
   p = str;
   digcnt = DECIMAL_DIGITS + 1;
   do { *p++ = 0; } while (--digcnt);
 
   // Transform binary to BCD
   bitcnt = BINARY_DIGITS;
   maxdig = (DECIMAL_DIGITS - 1);
   str += (DECIMAL_DIGITS - 1);
   do {
      // Shift left binary number with carry
      carry = 0;
      if (bin & (1L<<(BINARY_DIGITS-1)))
         carry |= 1;
      bin <<= 1;

      // Shift left decimal number with carry
      p = str;
      digcnt = DECIMAL_DIGITS - maxdig;
      do {
         carry = (*p<<1) + carry;
         if (carry >= 10) {
            *p-- = carry - 10;
            carry = 1;
            if (digcnt == 1) {
               maxdig--;
               digcnt++;
            }
         }
         else {
            *p-- = carry;
            carry = 0;
         }
      } while(--digcnt);
   } while(--bitcnt);

   // Transform BCD to ASCII
   digcnt = DECIMAL_DIGITS;
   do *str-- += '0'; while (--digcnt);
}


void setup() {
   Serial.begin(9600);
}


void loop() {
   unsigned long timeit;
   unsigned long bin;
   char strnum[DECIMAL_DIGITS + 1];
   strnum[DECIMAL_DIGITS] = 0; // End of char

   timeit = millis();
   for(bin=0x80000000; bin<0x80000000+1000; bin++) {
      bin32_to_str(strnum, bin);
   }
   timeit = millis() - timeit;
   Serial.print("1 Conversion in ");
   Serial.print(timeit);
   Serial.println(" microseconds");
   delay(5000);
   
   for(bin=0; bin<100000; bin++) {
      bin32_to_str(strnum, bin);
      Serial.println(strnum);
   }
}


Salida por el puerto serie:
Código: [Seleccionar]
1 Conversion in 234 microseconds
0000000000
0000000001
0000000002
0000000003
0000000004
0000000005
0000000006
0000000007
0000000008
0000000009
0000000010
0000000011
0000000012
0000000013
0000000014
0000000015
0000000016
0000000017
0000000018
0000000019
0000000020
0000000021
0000000022
0000000023
0000000024
0000000025
0000000026
0000000027
0000000028
0000000029
0000000030
0000000031
0000000032
0000000033
0000000034
0000000035
0000000036
0000000037
0000000038
0000000039
0000000040
0000000041
0000000042
0000000043
0000000044
0000000045
0000000046
0000000047
0000000048
0000000049
0000000050
0000000051
0000000052
0000000053
0000000054
0000000055
« Última modificación: 01/Oct/2021, 19:50:35 pm por Carlos »

Carlos

  • Moderador Global
  • ****
  • Mensajes: 294
Re:Conversión de binario 16 bits a texto decimal con 5 caracteres
« Respuesta #1 en: 01/Oct/2021, 19:49:50 pm »
Función de Arduino
   de: Binario 16 bits
   a: String decimal 5 caracteres

Código: (c) [Seleccionar]
#define BINARY_DIGITS   16
#define DECIMAL_DIGITS  5


void bin16_to_str(char *str, unsigned int bin) {
   char maxdig, digcnt, bitcnt;
   static char *p, carry;
 
   // Clear string
   p = str;
   digcnt = DECIMAL_DIGITS + 1;
   do { *p++ = 0; } while (--digcnt);
 
   // Transform binary to BCD
   bitcnt = BINARY_DIGITS;
   maxdig = (DECIMAL_DIGITS - 1);
   str += (DECIMAL_DIGITS - 1);
   do {
      // Shift left binary number with carry
      carry = 0;
      if (bin & (1L<<(BINARY_DIGITS-1)))
         carry |= 1;
      bin <<= 1;

      // Shift left decimal number with carry
      p = str;
      digcnt = DECIMAL_DIGITS - maxdig;
      do {
         carry = (*p<<1) + carry;
         if (carry >= 10) {
            *p-- = carry - 10;
            carry = 1;
            if (digcnt == 1) {
               maxdig--;
               digcnt++;
            }
         }
         else {
            *p-- = carry;
            carry = 0;
         }
      } while(--digcnt);
   } while(--bitcnt);

   // Transform BCD to ASCII
   digcnt = DECIMAL_DIGITS;
   do *str-- += '0'; while (--digcnt);
}


void setup() {
   Serial.begin(9600);
}


void loop() {
   unsigned long timeit;
   unsigned long bin;
   char strnum[DECIMAL_DIGITS + 1];
   strnum[DECIMAL_DIGITS] = 0; // End of char

   timeit = millis();
   for(bin=0; bin<65500; bin++) {
      bin16_to_str(strnum, bin);
   }
   timeit = millis() - timeit;
   Serial.print("1 conversion in ");
   Serial.print(timeit/65.5);
   Serial.println(" microseconds");
   delay(5000);
   
   for(bin=0; bin<50000; bin++) {
      bin16_to_str(strnum, bin);
      Serial.println(strnum);
   }
}


Salida por el puerto serie:
Código: [Seleccionar]
1 Conversion in 66.85 microseconds
00000
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
« Última modificación: 01/Oct/2021, 19:52:57 pm por Carlos »