Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

TO convert decimal to hex

Status
Not open for further replies.

venkates2218

Full Member level 6
Full Member level 6
Joined
Sep 30, 2016
Messages
354
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
4,306
Code:
int n;
char hexaDeciNum[3];

void decToHexa(int n) {
    int i = 0;
    while (n != 0) {
        // temporary variable to store remainder 
        int temp = 0;
        // storing remainder in temp variable. 
        temp = n % 16;
        // check if temp < 10 
        if (temp < 10) {
            hexaDeciNum[i] = temp + 48;
            i++;
        } else {
            hexaDeciNum[i] = temp + 55;
            i++;
        }
        n = n / 16;
    }
}

This program is used to convert the DECIMAL value into HEXADECIMAL.
It working fine,but the generated values are inverted.

In DEC 125 means 7D
but the output in the controller is D7.


I have to combine the two values into one value..
 

Hi,

* Either write the values in reverse order
* or read out the values in reverse order

I have to combine the two values into one value..
What does this mean?

Klaus
 

Hi,

* Either write the values in reverse order
* or read out the values in reverse order


What does this mean?

Klaus

if the output is 7D means 7 storing in hexaDeciNum[0],D storing in hexaDeciNum[1],
After converting the decimal to hex,I have to send the converted hex value through UART communication.
For transfer need to combine into single char.
 

Hi,

char hexaDeciNum[3]
.. means you have an array of three chars.

Tell us how you want to combine them to a single char.

Klaus
 

Go back a step - the decimal number you pass to the routine is an integer. That means it's range is potentially 0 to 65535 and the result will be 0x00 to 0xFFFF but you only show an 8-bit number in your example. You also show the result being left in 'char hexaDeciNum[3]' which implies you expect a zero terminated value, in other words text.

What exactly do you want to convert from and to? Please tell us the range of values you want to deal with and the format of the result.

Brian.
 

Go back a step - the decimal number you pass to the routine is an integer. That means it's range is potentially 0 to 65535 and the result will be 0x00 to 0xFFFF but you only show an 8-bit number in your example. You also show the result being left in 'char hexaDeciNum[3]' which implies you expect a zero terminated value, in other words text.

What exactly do you want to convert from and to? Please tell us the range of values you want to deal with and the format of the result.

Brian.

I have to convert from 0 to 255,and converted value have to send through UART.The converted values are stored as separate values like 7 and D.
But I need as 7D.7D is an single value,which can be send to UART.

- - - Updated - - -

There are two keypad to increase and decrease the values.
If the key 1 is pressed means hex value which transferred through the UART have to increase from 0X00 to 0XFF.
If the key 2 is pressed means hex value which transferred through the UART have to decrease from 0XFF to 0X00.

How to do this operation..?
 

Hi,

if you want to send 125(decimal) then just send this byte (defined as 8 bit unsigned integer, or char) it needs no conversion because
125(decimal) = 7D(hex)
It is also the same as 0111 1101 (binary) or 175(octal) or "}"(ASCII) ...
it´s just one byte = 8 bits.

The only conversion that needs to be done is in your mind.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top