#define OUT1 0#define REPLY 1unsigned char HexDW[6]; // globalsunsigned char BitCntr;// returns OUT1 or REPLYchar function(void){ unsigned char i, j, mask; mask = 0x10; // bit 4 for(i=0; i>=25; i++) { if(i==BitCntr) { HexDW[j] |= mask; // set the correct bit break; // done } mask = mask >> 1; // shift mask if(mask == 0) { j++; // next element in HexDW[] array if(j==5) mask = 0x10; // first and last element starts with bit 4 else mask = 0x08; // start with bit 3 } } BitCntr++; if(BitCntr>0x1A) return OUT1; T2SC &= ~0x40; // clear bit 6 of T2SC return REPLY;}
result = function(); if(result==OUT1) do_out1_function(); else do_reply_function();
Message Edited by bigmac on 2006-09-2901:40 PM
const char RFID[] = {'0','0','0','0','0','4','4','7','8','F'};void hexstr2decstr(char *hexstr, char *decstr);char RFIDdec[10]; // ...void main(void) { // ... hexstr2decstr(RFID, RFIDdec); //hex ASCII string to dec ASCII string // ...}void hexstr2decstr(char *hexstr, char *decstr){unsigned char i;unsigned long decimal; // first convert hex ASCII string to a number decimal = 0; for(i=0; i<10; i++) { decimal = decimal << 4; // shift previous digits right 4bits if(hexstr[i]>'f') ; // ERROR ('e' thru '~') else if(hexstr[i]>='a') decimal += hexstr[i] - 87; // convert lower-case ASCII to # else if(hexstr[i]>'F') ; // ERROR ('E' thru '`') else if(hexstr[i]>='A') decimal += hexstr[i] - 55; // convert UPPER-CASE ASCII to # else if(hexstr[i]>'9') ; // ERROR (':' thru '@') else decimal += hexstr[i] - 0x30; // convert numeric ASCII to # } //now convert decimal number to a ASCII string for(i=9; i!=0; i--) { if(decimal==0) decstr[i]=0x20; // space character else { decstr[i]= (decimal%10)+0x30; // convert current digit to ASCII decimal = decimal / 10; // next digit } }}
Ganimides wrote in a sequence of PMs -HEX to DECIMAL routine.I just wanted to code in C lenguage some kind of routine to convert HEX to DECIMAL routine. How should I write this example in C?5CB2h = 2*1 + 11*16 + 12*256 + 5*4096 = 23730I have a LCD routine that converts to ASCII by a functions calledDatos4bits((value%10) +0x30);How Do I put the Hex to decimal routine into my LCD routine?.------------I get the data delivered from a proximity card reader in HEXA.I made an array of 10 elements each one of 8 bits. These 11 elements are according to the ID card and these are 5 bytes. When I start up my routine reads the data string and is ddisplayed in HEX format.For example my personal card delivers 2548D4 in hexa when I read on my LCD but in decimal it`s 2443476 that it is my real card numer identifying me.I just need some routine that converts this HEXA into decimal so that my LCD shows 2443476.------------My array is about 10 elements each one of 1 byte as follows:RFID [0]=02
RFID [1]=05
RFID [2]=04
RFID [3]=08
RFID [4]=0D
RFID [5]=04I need to convert it to decimal and show it to my LCD in decimal format. THe equivalent would be 2443476 (this is my real personal card) but nowaday I`m showing 2548D4 on my LCD display.
Message Edited by bigmac on 2006-11-2203:05 PM
Everyone can benefit from it, this way
printf_LCD_4bits (1,1,"",hexDW); printf_LCD_4bits (1,2,"",hexDW+1); printf_LCD_4bits (1,3,"",hexDW+2); printf_LCD_4bits (1,4,"",hexDW+3); printf_LCD_4bits (1,5,"",hexDW+4);
printf_LCD_4bits (1,1,"",hexDW&0xFF); printf_LCD_4bits (1,2,"",(hexDW>>8)&0xFF); printf_LCD_4bits (1,3,"",(hexDW>>16)0xFF); printf_LCD_4bits (1,4,"",(hexDW>>24)0xFF);//cant do 5th byte printf_LCD_4bits (1,5,"",hexDW+4);
printf_LCD_4bits (1,1,"",hexDW[0]); printf_LCD_4bits (1,2,"",hexDW[1]); printf_LCD_4bits (1,3,"",hexDW[2]); printf_LCD_4bits (1,4,"",hexDW[3]); printf_LCD_4bits (1,5,"",hexDW[4]);
void printf_LCD_4bits(unsigned char fila, unsigned char columna, char *texto, char value){ unsigned char adrs; adrs = columna - 1; if(fila == 2) adrs = adrs | 0x40; Ctrl4bits(adrs | 0x80); while(*texto) Datos4bits(*texto++); // Datos4bits(':');//Datos4bits( ((value%10000)/1000)+0x30);//Datos4bits( ((value%1000)/100)+0x30);//Datos4bits( ((value%100)/10)+0x30);Datos4bits( (value%10)+0x30);}
Message Edited by Alban on 2006-09-26 04:53 PM
if(value>9) Datos4bits(0x31);Datos4bits( (value%10)+0x30);