Hi,
I want to know how to eliminate leading zero's in a number using macros.
for example if i give XX.XX then this means that any number can occupy X which will be sent by the application dynamically. if the application sends 1234 then the number i will showing is 12.34 where as if the application sends 12 then i should show 0.12 here i should eliminate the leading zero. similarly if i get 1 then the value should be 0.01. That means i should eliminate all the leading zero's before decimal point except the units decimal point. Which should be done using macro. if the macro is defined i should eliminate zero's if not i should not eliminate. If the value doesn't contain any decimal point like XXXX or XX or XXX or X what ever then i should eliminate all the leading zero's what ever the number may be.
Note: i need pure C coding.
Loading
VulpesPosted Dec 28, 2011, 5:10 AM
Sean StewartPosted Jan 19, 2012, 10:48 PM
Sam HobbsPosted Dec 29, 2011, 2:59 PM
Karthik AgarwalPosted Dec 29, 2011, 12:55 AM
void * bit_format_ref,
HMI_CHAR concat_string[],
void * text_data_vp)
{
UINT16 Size;
UINT16 final_size = 0;
UINT16 len;
UINT8 replace_char = 0;
UINT32 *temp;
UINT32 *temp3;
UINT16 radix = 0;
HMI_CHAR static_string[MAX_TEXT_LENGTH] = {0};
HMI_CHAR DNV[9] = {0};
UINT8 *stat = (UINT8 *)bit_format_ref;
UINT8 *dynamic_string = (UINT8 *)text_data_vp;
UINT16 array_index = HMI_STRLEN(concat_string);
if(bit_format == 0x02)
{
temp3 = (UINT32 *)&bit_format_ref;
Size = *temp3 & 0x0F;
final_size = Size;
temp = (UINT32 *)&text_data_vp;
for(len = 0; len < Size; len++)
{
if(array_index < (MAX_TEXT_LENGTH - 1))
{
replace_char = (HMI_CHAR) ((*temp % 10) + 48);
*temp = *temp/10;
DNV[radix++] = replace_char;
}
else
{
break;
}
}
Size = *temp3 & 0xF0;
Size = Size >> 4;
final_size = final_size + Size;
temp = (UINT32 *)&text_data_vp;
if(Size > 0)
{
if(array_index < (MAX_TEXT_LENGTH - 1))
{
DNV[radix++] = '.';
final_size++;
}
for(len = 0; len < Size; len++)
{
if(array_index < (MAX_TEXT_LENGTH - 1))
{
replace_char = (HMI_CHAR) ((*temp % 10) + 48);
*temp = *temp/10;
#ifdef LEADING_ZEROS_REQUIRED
DNV[radix++] = replace_char;
#else
if(len > 0)
{
if(replace_char != '0')
{
DNV[radix++] = replace_char;
}
}
else if(len == 0)
{
DNV[radix++] = replace_char;
}
#endif
}
else
{
break;
}
}
}
for(len = final_size; len >= 0; len--)
{
concat_string[array_index++] = DNV[len];
if(len == 0)
break;
}
}
Karthik AgarwalPosted Dec 29, 2011, 12:48 AM
Of course i am finally putting them in the character array as i require that in the text format. To avoid your confusion please see the next post what i have already done.
Sam HobbsPosted Dec 28, 2011, 10:49 PM
The C and C++ compilers have something called the preprocessor. The preprocessor processes the #include statements, the #define (macro) statements and all the statements that begin with a "#". The preprocessor replaces those statements with other code. After all the preprocessor statements are gone, then the compiler processes the source code. So all the source code beginning with "#" that the main compiler sees (after the preprocessor), including the macros, are gone. The macros do not exist in the source code that the main compiler sees. It is impossible to use a macro during execution.
So please read again what Vulpes said.
Whatever it is that you are trying to say, "Application will send a number" and "if the macro is defined" sound inconsistent with the way that macros work.
Also note that leading zeros apply only to a number after conversion to text. The binary representation of a number will always have the same number of leading zeros. That is not entirely true if we consider 32-bit and 64-bit to be different but I hope you understand the point I am trying to make. The point I am making is that Vulpes suggests "a C library function such as sprintf to format numbers" because leading zeros is relevant to formatting numbers. The sprintf function and functions such as it use a format string and you can use one of multiple format strings depending on some condition.
VulpesPosted Dec 28, 2011, 6:11 AM
Karthik AgarwalPosted Dec 28, 2011, 5:43 AM