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.

Base 16 to Base 10 in C

Status
Not open for further replies.

GrandAlf

Advanced Member level 2
Joined
Mar 9, 2002
Messages
520
Helped
47
Reputation
92
Reaction score
6
Trophy points
1,298
Location
UK
Activity points
4,730
bcd2hex

I feel rather foolish, but cannot seem to my head round this one. I am trying to convert base 16 numbers (no letters A-F used) to base 10 as per the following.

B16 0x47 = Dec 71. B16 0x16 = Dec 22. B16 0x30 = Dec 48. B16 0x01 = Dec 1 Etc

Anyone got a routine for doing this?. As I said none of the B16 numbers contain any hex characters, all are standard numbers. Seems so obvious, but I just have a blind spot here. Can obviously display in any format using printf, but this is not what I am after. Any advice most gratefully received.
 

binary conversion from base 16 to base 10

The question isn't clear at all. Hex or decimal are only printable representations of internal data formats, that are always binary. To get a decimal representation in a string, sprintf() or itoa() C-functions can be used. If you intended a different representation than a printable string or internal binary format, you should specify it.

There are of course formats as BCD, that represent a two digit decimal number in 8 bits, but there's nothing in your post that suggests it.

Simply tell, what you want to achieve!
 

base-10 to base-16 in c

remember
C Language can understand number in all format i.e. Hex, decimal, binary, octal. you just declare it but all mean same
for example
a= 0x04 ; Hex 4
a=4 ; decimal 4
a=04 ; octal 4
a=0b100 ; binary 4
 

how to convert no to base 16 in c++

Perhaps I did not explain myself properly. I am trying to take a numeric variable(no strings) that contains a binary number representation of a hex number, and convert this to another binary number representing a decimal number. These is always numeric data, A-F is never used.

e.g
Hex 0-9 would be the same in decimal 0-9 of course. No conversion.

If the hex input is 30 the decimal should come out as 48. Same with other examples in initial post.

I can obviously do a conversion table, but a formula would be a much better option. Effectivly Hex to Decimal without the letters. Hope this makes sense.
 

unsigned char * num=0 in c lanuage

GrandAlf,
is this the sort of thing your looking for as a general idea
Code:
int HexToDec(int nNumToConvert) // recursive fn
{
	int nConverted = 0; // Clear this for calculation
	int nQuotient;
	int nRemainder;

	nQuotient = (nNumToConvert / 16);
	nRemainder = (nNumToConvert % 16);

	// could use ternary operator '?' but this 'if ...else' is clearer.
		if ( nQuotient = 0)
		{
			nConverted = nRemainder;
		}
		else 
		{
			nConverted = (16 * HexToDec(nQuotient) + nRemainder);
		}
	return (nConverted);
}
 

13 base ten to base 16

This looks along the right lines, will have a play around with it. Many thanks for your input.
 

base 10 to base 16 in c++

If the hex input is 30 the decimal should come out as 48. Same with other examples in initial post.
Sorry, but I didn't get the point.
0x30 is represented by the bit code 00110000, decimal 48 is represented the same bit code.

P.S.: If you are talking of BCD (binary coded decimal) representation, as I asked before, that's not a "binary number representing a decimal number" rather than a four bit binary number representing each digit.

P.P.S: A binary to BCD conversion goes like this:
Code:
while(num>0) {
  digit[n]=num%base
  num=num/base;
  n++;
}
 

convert base 16 to base 10

Just don't pass a 5 digit hex number to it , or change the relevant variables to long.

DOH!!! this may be another answer, I just ran it under Lcc ..

Code:
/* Convert hex formatted input string to its Decimal 
    will accept any valid hex string */

int main()
{
	int a;

	while(1)
		{
			printf("Enter a hex number: ");
			scanf("%x",&a); // hex formatted input string
			printf("Is the same as Decimal %d\n\n",a); // dec format output
		}
return 0;
}

may be able to run a variation of this, for your purpose.
 

It was from a snippet of code that I picked up. Looking at it again, of course it is BCD. I cannot believe I missed that. Just some days I don't have my brain in gear. Many thanks to both of you for putting me on the right track.
 

Just re-read my explanation, no wonder I caused confusion. Naturally 0x30 is the same as 48 etc. What I was actually trying to achieve was changing the hex number to decimal, but keeping the same value. So as the example 0x47 (01000111) would actualy become Dec 47 (00101111). It is in fact the opposite of the following.

// Convert BCD 1 byte to HEX 1 byte
//-------------------------------
unsigned char BCD2HEX(unsigned int bcd)
{
unsigned char temp;
temp=((bcd>>8)*100)|((bcd>>4)*10)|(bcd&0x0f);
return temp;
}

Trying to do Hex to BCD instead, and not quite getting it right. Sorry, yesterday was just one of those hectic days, not thinking logically at all.

Added after 2 minutes:

The smiley face should be an 8!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top