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 CCS Technical Support

Convert integer to hex

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







Convert integer to hex
PostPosted: Fri Apr 25, 2003 1:48 am     Reply with quote

I response with new post, due the forum reject my reply.
This message is for Jarle.

You can done the task with next code:

first, define next array as global....

char hexchar[2]; //matriz cadena de cálculo de valores binarios de 2 cars. ascii hex

and after, the function atohex(int valor) gives the string you need..

void atohex(int valor){
/*
devuelve el caracter hex del byte pasado como entero de 8 bits en la cadena hexchar
*/
int temp001;

temp001 = valor >> 4;

if (temp001 >= 0 && temp001 < 10){
hexchar[0]= temp001 + '0'; //si es en rango de 0..9
break;
}
else {
hexchar[0]= temp001 + '7'; //si es en rango de A..F
break;
}

temp001 = valor & 0x0f;

if (temp001 >= 0 && temp001 < 10){
hexchar[1]= temp001 + '0'; //si es en rango de 0..9
break;
}
else {
hexchar[1]= temp001 + '7'; //si es en rango de A..F
break;
}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13987
Bruce R. Knox
Guest







Re: Convert integer to hex
PostPosted: Fri Apr 25, 2003 6:30 am     Reply with quote

:=I response with new post, due the forum reject my reply.
:=This message is for Jarle.
:=
:=You can done the task with next code:
:=
:=first, define next array as global....
:=
:=char hexchar[2]; //matriz cadena de cálculo de valores binarios de 2 cars. ascii hex
:=
:=and after, the function atohex(int valor) gives the string you need..
:=
:=void atohex(int valor){
:=/*
:=devuelve el caracter hex del byte pasado como entero de 8 bits en la cadena hexchar
:=*/
:= int temp001;
:=
:= temp001 = valor >> 4;
:=
:= if (temp001 >= 0 && temp001 < 10){
:= hexchar[0]= temp001 + '0'; //si es en rango de 0..9
:= break;
:= }
:= else {
:= hexchar[0]= temp001 + '7'; //si es en rango de A..F
:= break;
:= }
:=
:= temp001 = valor & 0x0f;
:=
:= if (temp001 >= 0 && temp001 < 10){
:= hexchar[1]= temp001 + '0'; //si es en rango de 0..9
:= break;
:= }
:= else {
:= hexchar[1]= temp001 + '7'; //si es en rango de A..F
:= break;
:= }
:=}

or, you could use

printf("\%c",value);

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







Re: Convert integer to hex
PostPosted: Fri Apr 25, 2003 1:30 pm     Reply with quote

:=I response with new post, due the forum reject my reply.
:=This message is for Jarle.
:=
:=You can done the task with next code:
:=
:=first, define next array as global....
:=
:=char hexchar[2]; //matriz cadena de cálculo de valores binarios de 2 cars. ascii hex
:=
:=and after, the function atohex(int valor) gives the string you need..
:=
:=void atohex(int valor){
:=/*
:=devuelve el caracter hex del byte pasado como entero de 8 bits en la cadena hexchar
:=*/
:= int temp001;
:=
:= temp001 = valor >> 4;
:=
:= if (temp001 >= 0 && temp001 < 10){
:= hexchar[0]= temp001 + '0'; //si es en rango de 0..9
:= break;
:= }
:= else {
:= hexchar[0]= temp001 + '7'; //si es en rango de A..F
:= break;
:= }
:=
:= temp001 = valor & 0x0f;
:=
:= if (temp001 >= 0 && temp001 < 10){
:= hexchar[1]= temp001 + '0'; //si es en rango de 0..9
:= break;
:= }
:= else {
:= hexchar[1]= temp001 + '7'; //si es en rango de A..F
:= break;
:= }
:=}
I think Jarle, needs to work out what is really wanted...
You cannot send a two character hex value, in one character. I doubt if the system at the other end actually receives hex, so it probably just wants the 'raw' character (not hex at all), in which case simply using the \%c character in the printf statement, or a simple 'putc', is what is needed.
However if the value needs to be sent as hex, then two characters will be needed for each byte, and the \%x syntax is correct (why re-invent the wheel - printf allready has a hex output conversion...).

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 13998
Miguel Martin
Guest







Re: Convert integer to hex
PostPosted: Fri Apr 25, 2003 5:00 pm     Reply with quote

:=:=I response with new post, due the forum reject my reply.
:=:=This message is for Jarle.
:=:=
:=:=You can done the task with next code......:=:=}
:=I think Jarle, needs to work out what is really wanted...
:=You cannot send a two character hex value, in one character. I doubt if the system at the other end actually receives hex, so it probably just wants the 'raw' character (not hex at all), in which case simply using the \%c character in the printf statement, or a simple 'putc', is what is needed.
:=However if the value needs to be sent as hex, then two characters will be needed for each byte, and the \%x syntax is correct (why re-invent the wheel - printf allready has a hex output conversion...).
:=
:=Best Wishes

Hi. In most cases, is necessary to send to RS232 a byte coded in hex form. Not "0xff", par example, to send a 255 value.

I use the routine shown to form a frame in hex buffer, posterior calculation of vertical parity, and send to rs 232.

In earliers version of CCS compiler, (lower than 2.734), the function sprintf() is not implemented, and I developed the routine to form "quasi equal" in buffer the result.

The continued use of the routine is my "vice".
I will try to use sprintf(). Probably, is more efective.

But my program works fine....

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







Re: Convert integer to hex
PostPosted: Sun Apr 27, 2003 4:36 am     Reply with quote

:=:=:=I response with new post, due the forum reject my reply.
:=:=:=This message is for Jarle.
:=:=:=
:=:=:=You can done the task with next code......:=:=}
:=:=I think Jarle, needs to work out what is really wanted...
:=:=You cannot send a two character hex value, in one character. I doubt if the system at the other end actually receives hex, so it probably just wants the 'raw' character (not hex at all), in which case simply using the \%c character in the printf statement, or a simple 'putc', is what is needed.
:=:=However if the value needs to be sent as hex, then two characters will be needed for each byte, and the \%x syntax is correct (why re-invent the wheel - printf allready has a hex output conversion...).
:=:=
:=:=Best Wishes
:=
:=Hi. In most cases, is necessary to send to RS232 a byte coded in hex form. Not "0xff", par example, to send a 255 value.
:=
Er. Except the original poster, is complaining that the \%x form generates two bytes. This is inherent in sending the data in hex format...

:=I use the routine shown to form a frame in hex buffer, posterior calculation of vertical parity, and send to rs 232.
:=
:=In earliers version of CCS compiler, (lower than 2.734), the function sprintf() is not implemented, and I developed the routine to form "quasi equal" in buffer the result.
:=
:=The continued use of the routine is my "vice".
:=I will try to use sprintf(). Probably, is more efective.
:=
:=But my program works fine....
It will (inherently), still have to give two characters, and given this was what the poster was complaining about, it seems likely that in fact 'hex' format is not what is required, but just 'raw' data (ie. \%c)....
Hence my comment that Jarle really needs to work out what is needed.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 14019
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