how to split a number?

Status
Not open for further replies.

david43

Newbie level 3
Joined
Dec 12, 2012
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
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.
 

Get a look here:
https://www.edaboard.com/blog/1784/
In the comments I have posted a function that would return the C string of the number, which can be used to be printed on LCDs.
 

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.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…