CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Converting hex to decimal

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Fabri



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

Converting hex to decimal
PostPosted: Thu Jul 27, 2006 11:03 am     Reply with quote

Hi everybody,

I need to convert hex into decimal number 3 digit as show:
Quote:


int16 hexnumber;
char string[4];

sprintf(string,"%03Ld",hexnumber);
digit1=0x0f & string[0];
digit2=0x0f & string[1];
digit3=0x0f & string[2];


Sometime in string[0] there's an incorrect value for example 0x46 instead 0x30.

Somebody can help me ?

Regards,
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Jul 27, 2006 11:13 am     Reply with quote

How are you declaring digit1 through digit3? You do not need the leading zero(0) in the sprintf() command to get three places inserted into string. How are you finding out (another printf() command?) what has been inserted into digit1 through digit3? A little more info might let us see how you are formatting everything.

Ronald
Fabri



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

PostPosted: Fri Jul 28, 2006 2:17 am     Reply with quote

Hi rnielsen,

I declared digit1 to digit3 as integer and are only result of conversion from string to digit. In debug the variable hexnumber is ok before sprintf. No more sprintf is used in the project.

what are you meaning with:
Quote:

You do not need the leading zero(0) in the sprintf() command to get three places inserted into string


If I declare:
char string[6];
Is there any difference in result of sprintf ?

I'm using compiler PCWH V.3,235 and PIC16F690

Thanks for help,
Ttelmah
Guest







PostPosted: Fri Jul 28, 2006 2:44 am     Reply with quote

How is the value put 'into' 'hexnumber'?.
What you post will fail (nastily), if hexnumber goes >999, since then the string will overflow the storage space available. Also if the source value, is actually _signed_, and then gets transferred into the 'unsigned' hexnumber, you will get probelms.
Add a test where you load 'hexnumber'. Something like:
Code:

//assuming incoming value is called 'source'.
if (source<0>999) hexnumber=999;
else hexnumber=source;

and see if this makes things work as you expect.

Best Wishes
Fabri



Joined: 22 Aug 2005
Posts: 275

View user's profile Send private message

PostPosted: Fri Jul 28, 2006 3:08 am     Reply with quote

Hi Ttelmah,

The value into hexnumber is 0 to 400 and there's no possibility to have number excedeed 999.

For example hexnunber is 0x3A (58 decimal) and normally string is 0x30 0x35 0x38 0x00 (digit=058).
Sometimes hexnumber is 0x3A but string after convertion is 0x30 0x73 0x38 0x00.

This happend in case I declared:
Quote:

char string[6];


instead of
Quote:

char string[4];


By your side does it possible and is this the problem ?


Thanks,
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

Re: Converting hex to decimal
PostPosted: Fri Jul 28, 2006 4:50 am     Reply with quote

Fabri wrote:

I need to convert hex into decimal number 3 digit as show:


When you say hex, do you mean bcd?
Ttelmah
Guest







PostPosted: Fri Jul 28, 2006 4:51 am     Reply with quote

The simple answer I'm afraid, is that something else is causing your problem. Possibilities are:
An interrupt handler, that does not save all the registers, resulting in an incorrect array index, or value.
Another part of the code overwriting a value.
A value that is not as you expect.
A pointer being used, that points to a value that is not retained (remember if you return values 'from' a routine, using a pointer, and these are declared as 'transient' values - not global, or static, then these can be destroyed once the routine exits).
Any one of perhaps a dozen other similar faults...
Now I am pretty confident of this, for a simple reason. I generated the following 'test' program, compiled it with 3.235, loaded it into a hardware ICU, and left it running for 20 minutes. Not a single failure...
Code:

#include "C:\Program Files\PICC\amtest\testprint.h"

#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B5,bits=8,ERRORS)

char string[6];
int8 digit1,digit2,digit3;

void splitdigits(int16 val) {
   sprintf(string,"%03ld",val);
   digit1=string[0] & 0xf;
   digit2=string[1] & 0xf;
   digit3=string[2] & 0xf;
}

void main()
{
   int16 ctr;
   int8 temp;
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_oscillator(False);
   while (true) {
      for (ctr=0;ctr<400;ctr++) {
         splitdigits(ctr);
         temp=ctr/10;
         temp=temp%10;
         //Temp should equal the second digit here
         if (temp!=digit2) {
            printf("ERROR/n");
         }
      }
   }     
}


Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group