Continue to Site

# 16 bit Hex to decimal

Status
Not open for further replies.

#### akhileshchidare

##### Member level 3
16 bit hex

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

thanx
akhilesh

ï½“ï½”ï½’tol 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

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?

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/

16-bit hex to 16-bit decimal

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

thanx
Akhilesh

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

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:

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

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.

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

FF=255

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.

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.

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.