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

Conversione HEX -> DEC

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







Conversione HEX -> DEC
PostPosted: Mon Nov 25, 2002 10:54 am     Reply with quote

Dear all,

As I can convert a HEX number in DEC number with CCS:
example:
//*****************************************
int16 risultato; // HEX

int8 dec0; //DEC 0 //LSB
int8 dec1; //DEC 1
int8 dec2; //DEC 2
int8 dec3; //DEC 3
int8 dec4; //DEC 4 //MSB

risultato = 0x36F8 // example! HEX = 0X36F8

/*********** routine??? convert HEX (risultato) -> DEC (decx)

void convertHEX_DEC(int16 risultato)
{
??????
????? //HOW FUNCTION CONVERT HEX risultato in DEC?????
?????

dec0 = 2 //LSB
dec1 = 7
dec2 = 0
dec3 = 4
dec4 = 1 //MSB

}



HOW?

THANKS!
___________________________
This message was ported from CCS's old forum
Original Post ID: 9395
R.J.Hamlett
Guest







Re: Conversione HEX -> DEC
PostPosted: Mon Nov 25, 2002 11:29 am     Reply with quote

:=Dear all,
:=
:=As I can convert a HEX number in DEC number with CCS:
:=example:
:=//*****************************************
:=int16 risultato; // HEX
:=
:=int8 dec0; //DEC 0 //LSB
:=int8 dec1; //DEC 1
:=int8 dec2; //DEC 2
:=int8 dec3; //DEC 3
:=int8 dec4; //DEC 4 //MSB
:=
:=risultato = 0x36F8 // example! HEX = 0X36F8
:=
:=/*********** routine??? convert HEX (risultato) -> DEC (decx)
:=
:=void convertHEX_DEC(int16 risultato)
:={
:=??????
:=????? //HOW FUNCTION CONVERT HEX risultato in DEC?????
:=?????
:=
:=dec0 = 2 //LSB
:=dec1 = 7
:=dec2 = 0
:=dec3 = 4
:=dec4 = 1 //MSB
:=
:=}
:=
:=
:=
:=HOW?
It allready is....
Numbers are stored in binary internally (not hex or decimal). when you use the form:
risultato = 0x36F8;

The variable contains the number in binary, not hex. If you want the individual digits (as you show in your text), then:
byte out_string[6];
int count=0; //only needed for integer return

sprintf(out_string,"\%lu",risultato);

will result in out_string[0] containing '1' (in ASCII), out_string[1] containing '4', and so on. If you want the numeric values (as opposed to the string values), then you can use:

while (out_string[count]!=0) {
out_string[count]=out_string[count]-0x30;
count++;
}

Remember however at this point, you can no longer use the test for '0' to find the end of the string, but have to use the 'count' value to know how many digits there are.

Alternatively, if you want a 'DIY' version, and want to ensure that DEC4 allways contains the MSB digit for the five digit value, then:

DEC4=risultato/10000;
risultato=risultato \% 10000;
DEC3=risultato/1000;
risultato=risultato \% 1000;
DEC2=risultato/100;
risultato=risultato \% 100;
DEC1=risultato/10;
DEC1=risultato \% 10;

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 9398
micman
Guest







Re: Conversione HEX -> DEC
PostPosted: Mon Nov 25, 2002 12:02 pm     Reply with quote

Thanks!!!


:=:=Dear all,
:=:=
:=:=As I can convert a HEX number in DEC number with CCS:
:=:=example:
:=:=//*****************************************
:=:=int16 risultato; // HEX
:=:=
:=:=int8 dec0; //DEC 0 //LSB
:=:=int8 dec1; //DEC 1
:=:=int8 dec2; //DEC 2
:=:=int8 dec3; //DEC 3
:=:=int8 dec4; //DEC 4 //MSB
:=:=
:=:=risultato = 0x36F8 // example! HEX = 0X36F8
:=:=
:=:=/*********** routine??? convert HEX (risultato) -> DEC (decx)
:=:=
:=:=void convertHEX_DEC(int16 risultato)
:=:={
:=:=??????
:=:=????? //HOW FUNCTION CONVERT HEX risultato in DEC?????
:=:=?????
:=:=
:=:=dec0 = 2 //LSB
:=:=dec1 = 7
:=:=dec2 = 0
:=:=dec3 = 4
:=:=dec4 = 1 //MSB
:=:=
:=:=}
:=:=
:=:=
:=:=
:=:=HOW?
:=It allready is....
:=Numbers are stored in binary internally (not hex or decimal). when you use the form:
:=risultato = 0x36F8;
:=
:=The variable contains the number in binary, not hex. If you want the individual digits (as you show in your text), then:
:=byte out_string[6];
:=int count=0; //only needed for integer return
:=
:=sprintf(out_string,"\%lu",risultato);
:=
:=will result in out_string[0] containing '1' (in ASCII), out_string[1] containing '4', and so on. If you want the numeric values (as opposed to the string values), then you can use:
:=
:=while (out_string[count]!=0) {
:= out_string[count]=out_string[count]-0x30;
:= count++;
:=}
:=
:=Remember however at this point, you can no longer use the test for '0' to find the end of the string, but have to use the 'count' value to know how many digits there are.
:=
:=Alternatively, if you want a 'DIY' version, and want to ensure that DEC4 allways contains the MSB digit for the five digit value, then:
:=
:=DEC4=risultato/10000;
:=risultato=risultato \% 10000;
:=DEC3=risultato/1000;
:=risultato=risultato \% 1000;
:=DEC2=risultato/100;
:=risultato=risultato \% 100;
:=DEC1=risultato/10;
:=DEC1=risultato \% 10;
:=
:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 9402
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