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

IntToHex and HexToInt

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







IntToHex and HexToInt
PostPosted: Sat Jan 11, 2003 2:36 pm     Reply with quote

Hi there,
I wrote a code in borland c++ builder and i want to implement my code to ccs.Most of the project went good but in ccs there is no (i think) IntToHex and HexToInt functions.Can somebody give me idea about it? Thank you.
p.s. I only want to convert integer values to hex format and bring it back.
___________________________
This message was ported from CCS's old forum
Original Post ID: 10620
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: IntToHex and HexToInt
PostPosted: Sat Jan 11, 2003 11:36 pm     Reply with quote

:=Hi there,
:=I wrote a code in borland c++ builder and i want to implement my code to ccs.Most of the project went good but in ccs there is no (i think) IntToHex and HexToInt functions.Can somebody give me idea about it? Thank you.
:=p.s. I only want to convert integer values to hex format and bring it back.
--------------------------------------------------------
You're probably thinking of axtoi() and itoax().
http://bdn.borland.com/article/0,1410,17203,00.html
Or, see my versions below:
For some reason, I named one of them itoah instead of itoax.

I don't claim these routines are the most efficient
way to do it. I probably wrote them with a goal
of keeping the ROM size small. It's been a while.

Note: Even those these routines have pointers as
arguments, they don't operate on strings. There is
no string-terminator expected or written. These
routines were written to do a specific job, and not
for the general case.

Note that you can also use the CCS sprintf() function
to convert from integer to ascii hex.

Code:
// Convert an 8 bit number to two ascii hex chars.
// The output will always be two bytes.  Don't write
// a string terminator byte at the end.

void itoah(char value, char *output_ptr)
{
char temp;

temp = (value >> 4);      // Move hi nybble into low position
temp += '0';              // Add ascii bias
if(temp > '9')            // Is upper nybble = A to F ?
   temp += 7;             // If so, add more bias
*output_ptr++ = temp;     // Put result into output buffer

temp = value & 0x0f;      // Isolate lower nybble
temp += '0';              // Add ascii bias
if(temp > '9')            // Is lower nybble = A to F ?
   temp += 7;             // If so, add more bias
*output_ptr++ = temp;     // Put result into output buffer
}

//---------------------------------------------------------
// Convert 1 or 2 ascii hex characters to a binary value.

char axtoi(char *ptr)
{
char i, c, value;

value = 0;

for(i = 0; i < 2; i++)    // Convert a maximum of 2 digits
   {
    c = *ptr++;           // Get the char

    if(c >= 'a')          // If char is lower case,
       c &= 0x5f;         //     convert to upper case.

    if(c < '0')  // Check if the char is an ascii hex char
       break;
    if(c > 'F')
       break;
    if(c < 'A')
      {
       if(c > '9')
          break;
      }

    c -= 0x30;       // Convert the char to binary
    if(c > 9)
       c -= 7;
    value <<= 4;     // Shift existing value left 1 nybble
    value |= c;      // Then combine it with new nybble
   }

return(value);
}

___________________________
This message was ported from CCS's old forum
Original Post ID: 10622
-------
Edited to put to code in a "code block" to make it easier to read.


Last edited by PCM programmer on Sun Feb 12, 2006 2:37 pm; edited 1 time in total
No_Fear
Guest







Re: IntToHex and HexToInt
PostPosted: Mon Jan 13, 2003 2:57 pm     Reply with quote

<font face="Courier New" size=-1>@PCM Programmer :

Sorry for delay.I tried to compile your specific axtoi(..) function but my ccs compiler gave me error "Expection LVALUE such as a variable name ...." in line contains

c = *ptr++; // Get the char

but this code can be compiled easily in bcb 4.0 and works fine. Did i miss something?
Thank you.


:=:=Hi there,
:=:=I wrote a code in borland c++ builder and i want to implement my code to ccs.Most of the project went good but in ccs there is no (i think) IntToHex and HexToInt functions.Can somebody give me idea about it? Thank you.
:=:=p.s. I only want to convert integer values to hex format and bring it back.
:=--------------------------------------------------------
:=You're probably thinking of axtoi() and itoax().
:= <a href="http://bdn.borland.com/article/0,1410,17203,00.html" TARGET="_blank"> <a href="http://bdn.borland.com/article/0,1410,17203,00.html" TARGET="_blank"> <a href="http://bdn.borland.com/article/0,1410,17203,00.html" TARGET="_blank">http://bdn.borland.com/article/0,1410,17203,00.html</a></a></a>
:=
:=Or, see my versions below:
:=For some reason, I named one of them itoah instead of itoax.
:=
:=I don't claim these routines are the most efficient
:=way to do it. I probably wrote them with a goal
:=of keeping the ROM size small. It's been a while.
:=
:=Note: Even those these routines have pointers as
:=arguments, they don't operate on strings. There is
:=no string-terminator expected or written. These
:=routines were written to do a specific job, and not
:=for the general case.
:=
:=Note that you can also use the CCS sprintf() function
:=to convert from integer to ascii hex.
:=
:=<PRE>
:=// Convert an 8 bit number to two ascii hex chars.
:=// The output will always be two bytes. Don't write
:=// a string terminator byte at the end.
:=
:=void itoah(char value, char *output_ptr)
:={
:=char temp;
:=
:=temp = (value >> 4); // Move hi nybble into low position
:=temp += '0'; // Add ascii bias
:=if(temp > '9') // Is upper nybble = A to F ?
:= temp += 7; // If so, add more bias
:=*output_ptr++ = temp; // Put result into output buffer
:=
:=temp = value & 0x0f; // Isolate lower nybble
:=temp += '0'; // Add ascii bias
:=if(temp > '9') // Is lower nybble = A to F ?
:= temp += 7; // If so, add more bias
:=*output_ptr++ = temp; // Put result into output buffer
:=}
:=
:=//---------------------------------------------------------
:=// Convert 1 or 2 ascii hex characters to a binary value.
:=
:=char axtoi(char *ptr)
:={
:=char i, c, value;
:=
:=value = 0;
:=
:=for(i = 0; i < 2; i++) // Convert a maximum of 2 digits
:= {
:= c = *ptr++; // Get the char
:=
:= if(c >= 'a') // If char is lower case,
:= c &= 0x5f; // convert to upper case.
:=
:= if(c < '0') // Check if the char is an ascii hex char
:= break;
:= if(c > 'F')
:= break;
:= if(c < 'A')
:= {
:= if(c > '9')
:= break;
:= }
:=
:= c -= 0x30; // Convert the char to binary
:= if(c > 9)
:= c -= 7;
:= value <<= 4; // Shift existing value left 1 nybble
:= value |= c; // Then combine it with new nybble
:= }
:=
:=return(value);
:=}
:=
:= ___________________________
This message was ported from CCS's old forum
Original Post ID: 10661
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: IntToHex and HexToInt
PostPosted: Mon Jan 13, 2003 3:20 pm     Reply with quote

<font face="Courier New" size=-1>:=
:=Sorry for delay.I tried to compile your specific axtoi(..) function but my ccs compiler gave me error "Expection LVALUE such as a variable name ...." in line contains
:=
:=c = *ptr++; // Get the char
-------------------------------------------------------

I cut and pasted the axtoi function out of my post into MPLAB.
I added some code to call the function. I compiled it with
PCM vs. 3.131 and PCM vs. 2.734. It compiles with no errors
with both versions. I tested both hex files and they both
work OK.



#include "c:\program files\picc\devices\16F877.h"
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT, NOLVP
#use Delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

char axtoi(char *ptr);


//===============================================
void main()
{
char c;
char hex_string[2];

hex_string[0] = 'A';
hex_string[1] = 'F';

c = axtoi(hex_string);

printf("\%x", c);

while(1);
}

//===============================================

// Convert 1 or 2 ascii hex characters to a binary value.

char axtoi(char *ptr)
{
char i, c, value;

value = 0;

for(i = 0; i < 2; i++) // Convert a maximum of 2 digits
{
c = *ptr++; // Get the char
if(c >= 'a') // If char is lower case,
c &= 0x5f; // convert to upper case.
if(c < '0') // Check if the char is an ascii hex char
break;
if(c > 'F')
break;
if(c < 'A')
{
if(c > '9')
break;
}
c -= 0x30; // Convert the char to binary
if(c > 9)
c -= 7;
value <<= 4; // Shift existing value left 1 nybble
value |= c; // Then combine it with new nybble
}
return(value);
}




-- ___________________________
This message was ported from CCS's old forum
Original Post ID: 10662
No_Fear
Guest







Re: IntToHex and HexToInt
PostPosted: Tue Jan 14, 2003 2:12 am     Reply with quote

I can't understand the problem.I'm using pcm v3.082 and it does not compile? Duh.

:=<font face="Courier New" size=-1>:=
:=:=Sorry for delay.I tried to compile your specific axtoi(..) function but my ccs compiler gave me error "Expection LVALUE such as a variable name ...." in line contains
:=:=
:=:=c = *ptr++; // Get the char
:=-------------------------------------------------------
:=
:=I cut and pasted the axtoi function out of my post into MPLAB.
:=I added some code to call the function. I compiled it with
:=PCM vs. 3.131 and PCM vs. 2.734. It compiles with no errors
:=with both versions. I tested both hex files and they both
:=work OK.
:=
:=
:=
:=#include "c:\program files\picc\devices\16F877.h"
:=#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT, NOLVP
:=#use Delay(clock=8000000)
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
:=
:=char axtoi(char *ptr);
:=
:=
:=//===============================================
:=void main()
:={
:=char c;
:=char hex_string[2];
:=
:=hex_string[0] = 'A';
:=hex_string[1] = 'F';
:=
:=c = axtoi(hex_string);
:=
:=printf("\%x", c);
:=
:=while(1);
:=}
:=
:=//===============================================
:=
:=// Convert 1 or 2 ascii hex characters to a binary value.
:=
:=char axtoi(char *ptr)
:={
:=char i, c, value;
:=
:=value = 0;
:=
:=for(i = 0; i < 2; i++) // Convert a maximum of 2 digits
:= {
:= c = *ptr++; // Get the char
:= if(c >= 'a') // If char is lower case,
:= c &= 0x5f; // convert to upper case.
:= if(c < '0') // Check if the char is an ascii hex char
:= break;
:= if(c > 'F')
:= break;
:= if(c < 'A')
:= {
:= if(c > '9')
:= break;
:= }
:= c -= 0x30; // Convert the char to binary
:= if(c > 9)
:= c -= 7;
:= value <<= 4; // Shift existing value left 1 nybble
:= value |= c; // Then combine it with new nybble
:= }
:=return(value);
:=}
:=
:=
:=
:=
:=-- ___________________________
This message was ported from CCS's old forum
Original Post ID: 10676
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: IntToHex and HexToInt
PostPosted: Tue Jan 14, 2003 12:36 pm     Reply with quote

:=I can't understand the problem.I'm using pcm v3.082 and it does not compile? Duh.
:=
------------------------------------------------------
I tried it with 3.082, and it compiled fine.
Just to be really sure, I cut and pasted the code
from my previous post into MPLAB. It compiles.


CCS PCM C Compiler, Version 3.082,

Filename: C:\PICC\TEST\TEST.LST

ROM used: 125 (2\%)
Largest free fragment is 2048
RAM used: 9 (5\%) at main() level
13 (7\%) worst case
Stack: 1 locations
___________________________
This message was ported from CCS's old forum
Original Post ID: 10691
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