Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

16 bit Hex to decimal

Status
Not open for further replies.

The Svin

Junior Member level 3
Joined
Mar 5, 2005
Messages
31
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,286
Activity points
1,734
strtol c18

akhileshchidare said:
hi ,
can any one send me code for converting 16 bit hex value into decimal in C?
my mail id is
akhileshchidare(at)gmail.com

thanx
akhilesh

There is contradition in your question.
bit - means BIinary Digit
hex - means base 16 position system.
So in your question you mensioned value in 2 different positioning systems and its not clear in wich one is your Value.

Then more - talking of Values - there are no base 16 computers, so I completly buffled - do you mean ASCII string that represent hexodecimal value or what?
Or you just mean actually binary value that is some of your vars?

Then again "into decimal" - do your mean ASCII string that represent the being converted number in decimal symbols or your mean BCD value?

I think that most likely your asked how to convert binary value into ASCIIZ string that is decimal symbolic representation of the value.
Am I right?
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
637
Reputation
1,272
Reaction score
88
Trophy points
1,328
Location
USA
Activity points
33,183
16 bit to decimal

Is this what you mean?
Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  char hex_string[] = "2F1C";
  int value;

  value = (int)strtol(hex_string, NULL, 16);
  /* sscanf(hex_string, "%x", &value); */
  printf("%s hex = %d decimal\n", hex_string, value);
  return 0;
}
2F1C hex = 12060 decimal

Note, sscanf() works too, but it has undefined behavior if an error occurs, so strtol() is safer.

You may also enjoy the comp.lang.c FAQ web site. It has a lot of good C programming advice:
http://c-faq.com/
 

akhileshchidare

Member level 3
Joined
Oct 4, 2005
Messages
59
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,758
16-bit hex to 16-bit decimal

sorry,
my value is in HEX i have to convert it into Decimal.

thanx
Akhilesh
 

akhileshchidare

Member level 3
Joined
Oct 4, 2005
Messages
59
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,758
16-bit hex

Code:
  value = (int)strtol(hex_string, NULL, 16);

what is strtol() ? i am using C18 compiler from microchip. this function is not supported.

Akhiesh Chidare
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
637
Reputation
1,272
Reaction score
88
Trophy points
1,328
Location
USA
Activity points
33,183
what is 43/16 as a decimal?

Please clarify what you mean by "hex". In my example, 2F1C is hex. Maybe show us your example.

The strtol() function is defined in stdlib.h. If your compiler does not support it, then your compiler is not ANSI C compliant. Did you try sscanf() instead?

Here are two pages from the 1999 ANSI C standard describing strtol(). See section 7.20.1.4:
 

akhileshchidare

Member level 3
Joined
Oct 4, 2005
Messages
59
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,758
16bit in hex ansi c

hi,
C18 compiler is ANSI 89 compatiable.
it wil not support that strtol function. i am not finding sscanf also.
can you give any other alternative?

in my project i will be getting hex values and i have to convert those hex values into decimal and have to display. i am not looking for coverting one hexvalue into decimal.

can any one help

thanx
Akhilesh Chidare
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
637
Reputation
1,272
Reaction score
88
Trophy points
1,328
Location
USA
Activity points
33,183
16 bit hex value of -356

The C language does not have hex or decimal data types. You must explain exactly what you mean by "hex value" and "decimal" or nobody can help you.

The Microchip "16-Bit Language Tools Libraries" manual describes both strtol and sscanf. However, I'm not sure if that's the correct manual for your compiler.
 

akhileshchidare

Member level 3
Joined
Oct 4, 2005
Messages
59
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,758
what is the 16-bit hex value of -356

hi,

my value is in base 16 now i want to convert it into base 10.

some thing like FF(base 16)= 256(base 10).

i have the base 16 value in a variable. i need the source code in C for this conversion.

i have checked the stdlib header file i dont have such function. i am using Microchip C18 compiler.


thanx
Akhilesh

Added after 19 minutes:

FF=255
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
637
Reputation
1,272
Reaction score
88
Trophy points
1,328
Location
USA
Activity points
33,183
convert 16 bit hex to dec

What variable types are you using for the hex value and the decimal value?
Maybe show us a few lines of C code that explains your desired inputs and outputs.

I downloaded the "MPLAB C18 C Compiler Libraries" manual. The compiler doesn't support a lot of standard C functions. That's sad.
 

jonw0224

Full Member level 4
Joined
Nov 22, 2004
Messages
211
Helped
45
Reputation
90
Reaction score
14
Trophy points
1,298
Location
USA
Activity points
2,528
dec to 16 bit hex

Do you mean convert a number like an integer to ascii to display on a LCD. If that is the case you can try this method:

Divide the number by 10 (truncating it), keep the quotient and the remainder. Add 48 to the remainder (this will give you the ascii code for the one's place).

Divide the quotient by 10 (truncating it), keep the quotient and remainder. Add 48 to the remainder (this will give you the ascii code for the ten's place).

Repeat the process for the hundreds, thousands, etc.

But surely, if you're using C, you won't have to go this fundamental.
 

Giftbox

Newbie level 4
Joined
Sep 10, 2005
Messages
7
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,281
Activity points
1,335
hex to dec ansi c

Code:
#include < pic18.h > 

unsigned int MyWord,i;
unsigned char Ascii[5]; 
void main()
{  
 
    MyWord=19985;     // value Example max 0XFFFF
    for(i=0;i < 5;i++)
   {
           Ascii[i] = ((MyWord % 10) & 0x0F) | 0x30;
           MyWord /= 10;
   }
   while(1);
}

//==============================
//After run program

//Ascii[0]='5' or 0x35
//Ascii[1]='8' or 0x38
//Ascii[2]='9' or 0x39
//Ascii[3]='9' or 0x39
//Ascii[4]='1' or 0x31
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top