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.

Stuck on difficult string conversion (C inside) to integer problem

Status
Not open for further replies.

Pigi_102

Member level 1
Joined
Nov 14, 2007
Messages
39
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,643
Stucked on studid string conversion ( C inside )

Hi all,
I'm facing a stupid problem on converting from string to integer.


I have a string ( like "123" ) and need to interpreter his value, char by char reading backword and looking up his display value in an array ( to lit a 7 segment display by using a 4094 driver.

I already have a code working in C on my linux machine, and a working code for a really old Pic C compiler, but translating in CCS it does not work.

Here some snippet:
Code:
char digit[10]=
{
0b01111110,   // 0
0b00010010,   // 1
0b10111100,   // 2
0b10110110,   // 3
0b11010010,   // 4
0b11100110,   // 5
0b11001110,   // 6
0b00110010,   // 7
0b11111110,   // 8
0b11110010    // 9
} ;

void display(char * content, int howmany){

char Bytem;
char p[2];

    while ( (howmany+1) > 0 ) {                         // for every byte in buffer
           p[0] = content[howmany--];                    // read from last char in string backword 
           p[1] = '\0';
           Bytem = digit[atoi(p)];  
/* here the 4094 routine */
   }

void main () {
   while ( 1 )
     display ( "123", 3);
     display ( "456", 3);
// and so on
   } 
}

In the previous code I had:
Code:
int i=0;

    while ( (howmany+1) > 0 ) {                         
           i = (char) content[howmany--] - 0x80;
           Bytem = digit[i];  
/* here the 4094 routine */
   }

and was working, but in CCS it does not.

In linux the code is almost the same :
Code:
    while ( (howmany+1) > 0 ) {                         
           i = (char) content[howmany--] - '0';
           Bytem = digit[i];


Can you help me understand what I'm doing wrong ?

Thanks

Pigi
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,877
Helped
4,862
Reputation
9,742
Reaction score
4,643
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
126,635
Re: Stucked on studid string conversion ( C inside )

I can't see why it shouldn't work but it isn't the most efficient way to program it.

Try changing removing 'howmany' from the declaration and function call and use "howmany = sizeof(content)" instead, it will let you use any length of string without specifying it explicitly.
Also, if the characters are 0-9, change "bytem = digit[atoi(p)]" to "bytem = digit[content[howmany] - '0'];"

Brian.
 

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
Re: Stucked on studid string conversion ( C inside )

Try this:
Code:
[COLOR="#FF0000"]unsigned char [/COLOR]digit[10]=
{
0b01111110,   // 0
0b00010010,   // 1
0b10111100,   // 2
0b10110110,   // 3
0b11010010,   // 4
0b11100110,   // 5
0b11001110,   // 6
0b00110010,   // 7
0b11111110,   // 8
0b11110010    // 9
} ;

void display(char * content, int howmany){
[COLOR="#FF0000"]unsigned char Bytem;[/COLOR]
    while ([COLOR="#FF0000"]howmany[/COLOR]) {                         // for every byte in buffer
           [COLOR="#FF0000"]Bytem = digit[content[[SIZE=3]--howmany[/SIZE]] - '0']; [/COLOR] 
           /* here the 4094 routine */
   }
}

void main () {
[COLOR="#FF0000"]   char x[4] = "123";
[/COLOR]   while ( 1 ){
     [COLOR="#FF0000"]display ( x, 3);//or display(x,strlen(x)) or display(x,sizeof(x))[/COLOR]
// and so on
    } 
}

You read the '\0' (NUL) character initialy because you have used post decrement.
So in stead of accessing 3 digits, you access 4 (and the first is an invalid one).
Maybe the code that follows do the trick.

Initially you could replace:
Code:
content[howmany--]
with
Code:
content[--howmany]
and check if this alone do the trick.
The other red indications are speed optimizations or compiler compatibility hints.
 

Pigi_102

Member level 1
Joined
Nov 14, 2007
Messages
39
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,643
Re: Stucked on studid string conversion ( C inside )

I have tried the suggestion but they ( they are basically the same ) don't work too.

Now my code is:
Code:
while ( howmany  ) {           // per ogni byte del buffer

   Bytem=digit[content[--howmanyi] - '0'];

but still no display....

Before you ask, the display code works, as if I do:

Code:
while ( howmany ){

   Bytem=digit[howmany];

I get a nice "0" "1" "2" on display.

I'm starting to suspect that the problem could be in some differences in variable dimension or even endianess.

Pigi
 

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
Re: Stucked on studid string conversion ( C inside )

What is the mC?

Why don't you debug the code and see what values are used?
I noticed that you rewrite fragment of code here. Maybe some detail that you omit do the problem.

Test the above and see if it works displaying the first digit 3 times

Code:
while ( howmany  ) {           // per ogni byte del buffer
   Bytem = digit[content[0]];
   howmany--;
   //
   display segments here
}
 

Pigi_102

Member level 1
Joined
Nov 14, 2007
Messages
39
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,643
Re: Stucked on studid string conversion ( C inside )

The compiler is CCS. The micro is a pic 16F84 ( which unfortunally doesn't has icd capabilities ).

If I'm not wrong, being content a char* , if for example content is "123", content[0] is "1" ( in decimal should be 49 ).
Byetm = digit[49] would give unpredictable results ( or a core dump if possible ).

Am I right ?

Pigi
 

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
Re: Stucked on studid string conversion ( C inside )

Yes, subtract '0', sorry.

I suspect that because of limited capabilities the "123" you have problem or to the "123" (may be rom array that pointer cannot handle, or the double array usage digits[content[]].
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
Re: Stucked on studid string conversion ( C inside )

Please zip and post the complete CCS C project with latest code. I will test it in my hardware and reply.
 

Pigi_102

Member level 1
Joined
Nov 14, 2007
Messages
39
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,643
Re: Stucked on studid string conversion ( C inside )

I' m sure, at the moment, that it was a compiler bug.
I have upgraded from 4 to 5 and the same code is now working.

Thanks everyone for the help.

Pigi
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top