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

String number to hex expression

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



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

String number to hex expression
PostPosted: Fri Dec 24, 2021 6:34 pm     Reply with quote

Hi, I need to convert any "data hex" string to expression hex but as number.

example:

one string has this "0E2010624804C11" , but i need only the 4 last data so in a variable is copied the last 4 data, for after show like hex for can convert to decimal.


Code:


int8 BCDtobin(int8 val)
{
   //take a single byte coded as two BCD digits and return the binary value
   int8 temp;
   temp=val;
   swap(temp);
   temp&=0xF;  //high nibble
   temp*=2;
   temp=temp+(temp*4); //efficient *10 *8+*2
   temp+=(val&0xF); //add the low nibble
   return temp;
}


int16 BCDtoLongbin(int16 val)
{
   //Now convert a 16bit BCD value to 16bit result;
   int16 ltemp, ltemp2;
   ltemp=BCDtobin(make8(val,0)); //convert the MSB
   //now need *100.
   ltemp*=4;
   ltemp2=ltemp*8;
   ltemp=ltemp+ltemp2+(ltemp2*2); //efficient *100 -> *64+*32+*4
   ltemp+=BCDtobin(make8(val,1)); //now the LSB
   return ltemp;
}

void main()
{
 
char fullData ="0E2010624804C11"
char string ="4c11";
int16 result =0x4c11

   int16 testval;
   testval=0x4c11;
   testval=BCDtoLongbin(testval);  // testval=19473
 
   while(TRUE) ;   
}





thanks any help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 24, 2021 8:34 pm     Reply with quote

This sample code shows how to do it.
Code:

#include <18F46K22.h>
#fuses NOWDT 
#use delay(internal=4M)
#use rs232(UART1, baud=9600, ERRORS)

#include <ctype.h>

//-------------------------------------------------
// Ascii to unsigned long -
// Converts a string of 1 to 4 ascii hex characters to an
// unsigned long (16-bit) value.

int16 axtoul(char *ptr)
{
char i, temp;
int16 result;

result = 0;

for(i = 0; i < 4; i++)           // Convert a maximum of 4 hex digits
   {
    temp = toupper(*ptr++);      // Get the char.  Convert to upper case
    if(isxdigit(temp) == FALSE)  // Is ascii char a hex digit ?
       break;         // Break if not
    temp -= '0';      // Convert the char to binary
    if(temp > 9)
       temp -= 7;
    result <<= 4;      // shift existing value left 1 nybble
    result |= temp;    // Then combine it with new nybble
   }

return(result);
}

//=================================
void main()
{
int8 buffer[] = "4c11";   
int16 result;

result = axtoul(buffer);

printf("result = %lu \r", result);

while(TRUE);
}
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Sat Dec 25, 2021 9:19 pm     Reply with quote

PCM programmer wrote:
This sample code shows how to do it.
.....


Thanks!!
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