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.

how to split a number?

Status
Not open for further replies.

david43

Newbie level 3
Newbie level 3
Joined
Dec 12, 2012
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,311
hi,all
I have a integer 59.
how to split to two integer
my code not work
PHP:
			buf=recebuf;            	
	     buf = buf % 10;
           
			lcd_write_char(10,1,buf);   	
		
	 buf = buf / 10;            	
			lcd_write_char(11,1,buf);
 

Just a guess because I'm not sure which processor or compiler you are using: 'buf' appears to be a number of some type, is your lcd_write_char routine expecting a character like it's name suggests?
You might have to convert the number to it's ASCII equivalent or use 'sprintf' to turn it into a string.

Brian.
 

I think you have to convert it into character after extractingg individual digits like

Code:
digit1 = buf / 10 + 0x30;
digit2 = buf % 10 + 0x30;
or

Code:
digit1 = buf / 10 + 48;
digit2 = buf % 10 + 48;
 

Try this code:
Code:
        uc digit1, digit2, number;

	number = 59;
	digit1 = (number/10)%10;	//digit1 = 5
	digit2 = (number%10);		//digit2 = 9

	lcd_putc(digit1 + 0x30);	//0x05+0x30=0x35 -->ASCII '5'
	lcd_putc(digit2 + 0x30);	//0x09+0x30=0x39 -->ASCII '9'
 

You are modifying the value in 'buf' with your first statement 'buf = buf %10'

Instead you should use some temp variable so that buf doesn't get modified.

Also - i assume that the first argument in lcd_write_char() is the position from the left ? If so, the you need to swap the 2 position numbers 10 <--> 11.
10 is on the left (most sig digit) and 11 on the right.

Lastly - the earlier comment about converting to ascii code is valid. You need to add 48 (0x30) to convert to ascii codes, unless your lcd_write routine already does this (which it probably doesn't)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top