aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Common/Utilities/ASCII.c
blob: 80035b588bfde20105af87ea2170ad0cd176e839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * ASCII.c
 *
 *  Created on: 31 Aug 2020
 *      Author: avi
 */
#include "include.h"
#include <stdint.h>
#include <stdbool.h>
#include <DataDef.h>

// 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;
}