View previous topic :: View next topic |
Author |
Message |
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
String number to hex expression |
Posted: Fri Dec 24, 2021 6:34 pm |
|
|
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
|
|
Posted: Fri Dec 24, 2021 8:34 pm |
|
|
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
|
|
Posted: Sat Dec 25, 2021 9:19 pm |
|
|
PCM programmer wrote: | This sample code shows how to do it.
.....
|
Thanks!! |
|
|
|