/* * ASCII.c * * Created on: 31 Aug 2020 * Author: avi */ #include "include.h" #include #include #include // Convert ASCII HEX to decimal int ASCII_Hex_To_Decimal(char hexVal[], int len) { int i = 0; // Initializing base value to 1, i.e 16^0 int base = 1; int dec_val = 0; // Extracting characters as digits from last character for (i = 0; i < len - 1; i++) { // if character lies in '0'-'9', converting // it to integral 0-9 by subtracting 48 from // ASCII value. if (hexVal[i] >= '0' && hexVal[i] <= '9') { dec_val += (hexVal[i] - 48)*base; // incrementing base by power base = base * 16; } // if character lies in 'A'-'F' , converting // it to integral 10 - 15 by subtracting 55 // from ASCII value else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') { dec_val += (hexVal[i] - 55)*base; // incrementing base by power base = base * 16; } } return dec_val; } // function to convert decimal to ASCII hexadecimal uint32_t Decimal_To_ASCII_Hex(int n) { Word_to_Bytes Word2Bytes; // counter for hexadecimal number array int i = 0; while (n != 0) { // temporary variable to store remainder int temp = 0; // storing remainder in temp variable. temp = n % 16; Word2Bytes.Byte[i] = temp + 0x30; i++; n = n / 16; } for( ;i<4 ;i++) Word2Bytes.Byte[i] = 0x30; return Word2Bytes.Word; }