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.

Accepting decimal value as hex value

Status
Not open for further replies.

lats

Full Member level 4
Joined
Oct 27, 2005
Messages
216
Helped
13
Reputation
26
Reaction score
6
Trophy points
1,298
Activity points
2,945
hi friends,
can someone please help me out in designing a small function in c programming to accept decimal value as hex value, i mean lets say if user inputs a decimal value say 49, the processor should read(and futher processes) it as 0x49(hex value).

Thanks .

With regards
 

As long as the bits are packed the same way, you don't need a special function, hexadecimal uses all the symbols that decimal has.

I am curious as to why to want to transfer the value without conversion. Are you using a hex display for decimal values and ignoring the other six symbols available?
 

HI,
probably the input is from a key board. But dec 49 is not the same as 0x49. the compiler will give you equ. hex from the decimal. Do you want hex 31 or hex 49 for this decimal input??. Not very clear what you want.
 

i want hex 49 for this decimal 49 input.
 

I guess you want convert hexadecimal string to hexadecimal value, this hexbyte() function will help you.

Code:
unsigned char hexbyte(char *hex){
     return 16*toint(hex[0]) + toint(hex[1]);
}
 

This function also can be used:

Code:
int DecToHex(int value)
{
  char bcd[2];
  bcd[0] = value / 10;
  bcd[1] = value % 10;
  return ((bcd[0] << 4) | (bcd[1]));
}

lats said:
hi friends,
can someone please help me out in designing a small function in c programming to accept decimal value as hex value, i mean lets say if user inputs a decimal value say 49, the processor should read(and futher processes) it as 0x49(hex value).

Thanks .

With regards

[/code]
 

emanuelcsm solution is correct !

He packs the first digit in the lower nibble and the second digit in the upper nibble - just as you want !

But I am confused as to why you want such a solution in the first place :)

But I have a question for emanuelcsm.

Do you notice your code is not type correct :)

Ok - here's the solution any way - you forgot the 'unsigned' type.

You should not shift on signed types remember ;)

Furthurmore, the array 'bcd' is not actually required at all :)
 

Well vsmGuy, here is the code rewrited.

I think now it's correct at all... If you see another mistakes please write the code to me, I'm just learnig too.

Thanks for observations..

Code:
  unsigned char DecToHex(unsigned char value)
  {
    if (value >= 99)
      value = 99;
    return (((value / 10) << 4) | (value % 10))
  }

vsmGuy said:
emanuelcsm solution is correct !

He packs the first digit in the lower nibble and the second digit in the upper nibble - just as you want !

But I am confused as to why you want such a solution in the first place :)

But I have a question for emanuelcsm.

Do you notice your code is not type correct :)

Ok - here's the solution any way - you forgot the 'unsigned' type.

You should not shift on signed types remember ;)

Furthurmore, the array 'bcd' is not actually required at all :)
 

Oh.. as a beginner you got your concepts very well !!!

Well done !

Code:
unsigned char DecToHex(unsigned char value)
  {
    return ((((value / 10) % 10) << 4) | (value % 10))
  }

I just typed this out of my head...
 

i think the code will become bigger when i'll need to convert a 5 digit value this way. actual "%" and
"/" consume lot of memory. can there still be a easier method or is this one the best.(best i mean that consumes least amount of memroy space)

Thanks to all of you.

With best regards.
 

Thank you vsmGuy for the coment, I'm doing my best in this...

I think it is not possible to do without use / and %. If you say wy do this convertion we could help you to change the high level and don't need to use / and % anymore.

Thanks...

lats said:
i think the code will become bigger when i'll need to convert a 5 digit value this way. actual "%" and
"/" consume lot of memory. can there still be a easier method or is this one the best.(best i mean that consumes least amount of memroy space)

Thanks to all of you.

With best regards.
[/code]
 

@lats :

First let us be VERY clear that division and modulo may OR may NOT take up memery space. Some cores have single instructions for them.

You have never mentioned where you want this code to run.

We assumed a general case, and this is a good solution to it.

There is no doubt a better solution to it - lot of work has been done to find fast modulo ten and quotient serquences for coers which do not have DIV built in.

But that is too much of work.

I think you need to understand your problem well and explain it more to us.

Are you sure what you are doing ?

Why do you want it to be fast ? This is typically a user input routine. Even milliseconds will not make much difference to the thoroughput.
 

Thanks once again for replying. I never mentioned i need the code to be fast, i just want to make the code size small.

i'm using keil c51.

now we talk about the code application, actually at present i'm going to use this code for 2 different things.

first, for displaying a 4 or 5 digit integer value lets say "42617" on seven segment display, what i'm doing till now is :-
Code:
i=42617;
a=i%10;
i=i/10;
b=i%10;
i=i/10;
c=i%10;
i=i/10;
d=i%10;
i=i/10;
e=i%10;

now i get a=7, b=1,c=6,d=2,e=4, which i can now display on 5-seven segment display.
but if i had taken 42617 as hex value and had shifted it 4 to right each time i would had got digits seprated easily.

Second application is when i write some value to the seconds,minutes, hour, date etc. registers of DS1307 RTC, as the registers take 10's digit and units digit in upper and lower nibble. but this application is not to important.

Thanks

With best regards.
 

lats, you can work with BCD, like your RTC, but to translate DBC to decimal you need to use decimal multiplication and division. To display a number in a display is easy using BCD.

Code:
unsigned int16 Value = 53694; //0xD1BE
unsigned int32 BCD;
unsigned in32 DecToBcd (unsigned in16 value)
{
  //Rotine DecToHex
}

unsigned in16 BcdToDec (unsigned in32 value)
{
  //Rotine HexToDec
}

void main(void)
{
  BCD =  DecToBcd(Value); //BCD now = 0x53694
}


lats said:
Thanks once again for replying. I never mentioned i need the code to be fast, i just want to make the code size small.

i'm using keil c51.

now we talk about the code application, actually at present i'm going to use this code for 2 different things.

first, for displaying a 4 or 5 digit integer value lets say "42617" on seven segment display, what i'm doing till now is :-
Code:
i=42617;
a=i%10;
i=i/10;
b=i%10;
i=i/10;
c=i%10;
i=i/10;
d=i%10;
i=i/10;
e=i%10;

now i get a=7, b=1,c=6,d=2,e=4, which i can now display on 5-seven segment display.
but if i had taken 42617 as hex value and had shifted it 4 to right each time i would had got digits seprated easily.

Second application is when i write some value to the seconds,minutes, hour, date etc. registers of DS1307 RTC, as the registers take 10's digit and units digit in upper and lower nibble. but this application is not to important.

Thanks

With best regards.
 

Just write a function to map the Dec to BCD and then have a lookup table to map the BCD to pins of the 7SEG display.

Simple.
 

Thanks a lot. I'll try out this.
 

Oh - and do get back to us with your solution !
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top