View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
how to convert hexadecimal into decimal string |
Posted: Wed Aug 05, 2009 3:30 pm |
|
|
Hi someone can do I tell how I can convert hexadecimal into decimal string? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 05, 2009 4:31 pm |
|
|
What's the format of your input hex value ? Is it a string of ASCII hex
digits ? Or is it a integer variable (int8 or int16) ? |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Wed Aug 05, 2009 9:34 pm |
|
|
Hi PCM programmer,
I am sorry, the format of my input hex value is integer variable (int8) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Aug 06, 2009 8:29 am |
|
|
Thanks... |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Aug 06, 2009 8:40 am |
|
|
Sorry again but now I have another problem.
I want to compare three data format string, but I can not get the desired result, here's my code, could you give me a suggestion?
Code: | char Buffer1[3];
char Buffer2[3];
char Buffer3[3];
char Buffer_EEprom1[3];
char Buffer_EEprom2[3];
char Buffer_EEprom3[3];
sprintf(buffer1,"%u",sec);
sprintf(buffer2,"%u",min);
sprintf(buffer3,"%u",hrs);
for (i = 0; i< 2; i++) Buffer_EEprom1[i] = read_eeprom(210+i);
for (i = 0; i< 2; i++) Buffer_EEprom2[i] = read_eeprom(212+i);
for (i = 0; i< 2; i++) Buffer_EEprom3[i] = read_eeprom(214+i);
if ((!strcmp(Buffer_EEprom1,Buffer1))& (!strcmp(Buffer_EEprom2,Buffer2)) &(!strcmp(Buffer_EEprom3,Buffer3)))
Alarma = 1;
else
Alarma = 0; |
sec,min and hrs are values that reading of an RTC and the values with whom I want to compare are stored in the eerprom from the address 210 |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Aug 06, 2009 8:55 am |
|
|
Your strings need to be null terminated:
Code: |
for (i = 0; i< 2; i++) Buffer_EEprom1[i] = read_eeprom(210+i);
Buffer_EEprom1[i] = 0;
for (i = 0; i< 2; i++) Buffer_EEprom2[i] = read_eeprom(212+i);
Buffer_EEprom2[i] = 0;
for (i = 0; i< 2; i++) Buffer_EEprom3[i] = read_eeprom(214+i);
Buffer_EEprom3[i] = 0;
|
Also sec, min and hrs may only be 1 char.so if you store 1 sec as 01 in the eeprom then it wont match "1" != "01"
If you store it as 1 in the eeprom then you must make sure the second address contains the null terminator.
In this situation I think you would be better off just using the values rather than string equivilants.
Why do you want to use strings ? |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Aug 06, 2009 9:10 am |
|
|
Hi Wayne, the string have a null terminated, when I use the sprintf the null terminated is added, the same way when I read the contents of the EEPROM memory.
When I make the comparison of a single data eg sec, it works very well but I want to compare the three values. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Aug 06, 2009 9:22 am |
|
|
You do not null terminate the string when reading from the eeprom.
you read 2 values (chars)
for (i = 0; i< 2; i++) Buffer_EEprom1[i] = read_eeprom(210+i);
only reads
Buffer_EEprom1[0] = read_eeprom(210);
Buffer_EEprom1[1] = read_eeprom(211);
If this is a 2 digit value 56 sec for instance then where is the null terminator ?
You are assuming that when you create the array
char Buffer_EEprom3[3];
That it is filled with zero's (0) which would null terminate your string. (bad assumption)
sprintf does null terminate your string but you are only performing that on buffer1, buffer2 and buffer3. You still use strcmp with Buffer_EEprom1, Buffer_EEprom2 and Buffer_EEprom3! |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Aug 06, 2009 9:24 am |
|
|
LOL, I think you will find you want to be using && not & in your if statement.
& does a binary AND of the values. && does a logical AND. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Aug 06, 2009 9:34 am |
|
|
I can not replicate your comments that do know very little of the CCS, I told you that adds null terminal, for it can see in my debugger, in any case as I could make the comparison of these data? |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Aug 06, 2009 9:40 am |
|
|
Try posting what results you are getting!
How are you storing the values in eeprom ?
what values do
Buffer1, Buffer2, Buffer3 and
Buffer_EEprom1, Buffer_EEprom2 and Buffer_EEprom3
have when you do the comparison when it works and when it doesn't work ?
How often does it work ?
does it work at all ?
Show code that works
try
Code: |
sprintf(Buffer1,"%u",11); // puts "11" into buffer1
sprintf(Buffer2,"%u",22); // puts "22" into buffer2
sprintf(Buffer3,"%u",33); // puts "33" into buffer3
for (i = 0; i< 2; i++) Buffer_EEprom1[i] = '1'; // puts "11" into Buffer_EEprom1
Buffer_EEprom1[i] = 0;
for (i = 0; i< 2; i++) Buffer_EEprom2[i] = '2'; // puts "22" into Buffer_EEprom1
Buffer_EEprom2[i] = 0;
for (i = 0; i< 2; i++) Buffer_EEprom3[i] = '3';// puts "33" into Buffer_EEprom1
Buffer_EEprom3[i] = 0;
if ((!strcmp(Buffer_EEprom1,buffer1)) && (!strcmp(Buffer_EEprom2,buffer2)) && (!strcmp(Buffer_EEprom3,buffer3)))
Alarma = 1;
else
Alarma = 0;
|
|
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Aug 06, 2009 9:45 am |
|
|
You mix buffer and Buffer uppercase 'B' throughout your code.
Although CCS is NOT case sensitive you would be wise to be a bit more carefull. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Aug 06, 2009 9:57 am |
|
|
Wayne, whe I use Code: | if ((!strcmp(Buffer_EEprom1,buffer1)) && (!strcmp(Buffer_EEprom2,buffer2)) && (!strcmp(Buffer_EEprom3,buffer3))) |
the CPU doen not execute the comparison and jump to the next instruction, but when I do a comprarcion single value all are OK, what could be my error? |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Aug 06, 2009 10:03 am |
|
|
After the if statement does Alarm = 1 or 0 ?
it mostlikely IS doing the comparison but one of the terms is false:-
!strcmp(Buffer_EEprom1,buffer1)
!strcmp(Buffer_EEprom2,buffer2)
!strcmp(Buffer_EEprom3,buffer3)
So in each case Buffer_EEprom has to == buffer for the condition to be true.
1 or more of them must be false!
Do you have a serial port connected ?
Try printf("%s == %s\r\n%s == %s\r\n%s == %s\r\n", Buffer_EEprom1, buffer1, Buffer_EEprom2, buffer2, Buffer_EEprom3, buffer3);
And see what you get.
Also, you are using the debugger.
What does the debugger say is in the arrays ? and I mean upto the first 0.
e.g.
Buffer_EEprom1 = 0x31 0x00
Buffer_EEprom2 = 0x31 0x33 0x00
etc |
|
|
|