Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

C++ : How to print ASCII value of a character...

YuvarajaYuvaraja Member Posts: 14
[b][red]This message was edited by Yuvaraja at 2004-9-20 0:59:1[/red][/b][hr]
In C, using format string specified we can easily print the ASCII values of character, thats its value. But in C++, we need to typecast it for having its value.

If we decided to do so, we need to cast it only to 'int' or to 'unsigned int'. Why I ask this question is that I faced while studying C++ templates :

I had written an template, which will print the no.of bits, Max and Min range of given data type.

template
void TypeSize ( TYPE tyTempVar, char *pcTypeName )
{
TYPE ukMaxValue;
TYPE ukMinValue;
unsigned char ui8NoOfBits;
bool bSingStatus;
TYPE MyTest = 0;

ui8NoOfBits = 1;

/* Find no. of bits in given data type */
ui8NoOfBits = GetNoOfBits ( MyTest );

/* Check the sign type and find its max, min */
if ( ( ( TYPE ) ( 0x1L << ( ui8NoOfBits - 1 ) ) ) > 0 )
{
/* It is Unsigned */
ukMinValue = 0;
ukMaxValue = ( TYPE ) ukMinValue - 1;
bSingStatus = 0;
}
else
{
/* It is Signed */
ukMinValue = ( TYPE ) ( 0x1L << ( ui8NoOfBits - 1 ) );
ukMaxValue = ukMinValue - 1;
bSingStatus = 1;
}

/* Print the found values */
if ( bSingStatus )
{
cout << pcTypeName << unsigned int ( ui8NoOfBits ) << ' ' << signed int ( ukMinValue ) << ' ' << signed int ( ukMaxValue ) << endl;
}
else
{
cout << pcTypeName << unsigned int ( ui8NoOfBits ) << ' ' << unsigned int ( ukMinValue ) ' ' << unsigned int ( ukMaxValue ) << endl;
}
}

In this, i had typecasted to signed or unsigned int for printing the ranges. Do u have any other way to do this?

Thank you very much for your reply in advance...

Again I faced another problem, while trying to print the max range of a bit field :

struct MyStruct
{
unsigned char a : 1 | 0;
char b : 1 | 7;
};

In the above, I want to know the max and min range of bit field 'b'.

Comments

  • HK_MP5KPDWHK_MP5KPDW Member Posts: 770 ✭✭✭
    : [b][red]This message was edited by Yuvaraja at 2004-9-20 0:59:1[/red][/b][hr]
    : In C, using format string specified we can easily print the ASCII values of character, thats its value. But in C++, we need to typecast it for having its value.
    :
    : If we decided to do so, we need to cast it only to 'int' or to 'unsigned int'. Why I ask this question is that I faced while studying C++ templates :
    :
    : I had written an template, which will print the no.of bits, Max and Min range of given data type.
    :
    : template
    : void TypeSize ( TYPE tyTempVar, char *pcTypeName )
    : {
    : TYPE ukMaxValue;
    : TYPE ukMinValue;
    : unsigned char ui8NoOfBits;
    : bool bSingStatus;
    : TYPE MyTest = 0;
    :
    : ui8NoOfBits = 1;
    :
    : /* Find no. of bits in given data type */
    : ui8NoOfBits = GetNoOfBits ( MyTest );
    :
    : /* Check the sign type and find its max, min */
    : if ( ( ( TYPE ) ( 0x1L << ( ui8NoOfBits - 1 ) ) ) > 0 )
    : {
    : /* It is Unsigned */
    : ukMinValue = 0;
    : ukMaxValue = ( TYPE ) ukMinValue - 1;
    : bSingStatus = 0;
    : }
    : else
    : {
    : /* It is Signed */
    : ukMinValue = ( TYPE ) ( 0x1L << ( ui8NoOfBits - 1 ) );
    : ukMaxValue = ukMinValue - 1;
    : bSingStatus = 1;
    : }
    :
    : /* Print the found values */
    : if ( bSingStatus )
    : {
    : cout << pcTypeName << unsigned int ( ui8NoOfBits ) << ' ' << signed int ( ukMinValue ) << ' ' << signed int ( ukMaxValue ) << endl;
    : }
    : else
    : {
    : cout << pcTypeName << unsigned int ( ui8NoOfBits ) << ' ' << unsigned int ( ukMinValue ) ' ' << unsigned int ( ukMaxValue ) << endl;
    : }
    : }
    :
    : In this, i had typecasted to signed or unsigned int for printing the ranges. Do u have any other way to do this?
    :
    : Thank you very much for your reply in advance...
    :
    : Again I faced another problem, while trying to print the max range of a bit field :
    :
    : struct MyStruct
    : {
    : unsigned char a : 1 | 0;
    : char b : 1 | 7;
    : };
    :
    : In the above, I want to know the max and min range of bit field 'b'.
    :

    [blue]Things like the max and min value and number of bits (digits) of a given type can be determined using the [b]<limits>[/b] header as such:

    [code]
    #include
    #include

    int main()
    {
    std::cout << std::numeric_limits<unsigned int>::min() << std::endl
    << std::numeric_limits<unsigned int>::max() << std::endl;
    std::cout << std::numeric_limits<int>::min() << std::endl
    << std::numeric_limits<int>::max() << std::endl;
    std::cout << std::numeric_limits<unsigned char>::digits << std::endl;
    return 0;
    }[/code]
    Which on my machine outputs:
    [code]0
    4294967295
    -2147483648
    2147483647
    8[/code]I don't know if this can be done for bit fields. They are user defined in size and the system would not necessarily know the min/max values.[/blue]
  • YuvarajaYuvaraja Member Posts: 14
    Thanks for your reply and I know it can be found in limits.h. Actually my requirement is how to print ASCII values of character. The code snippet I pased in previous forum is as an example, on what context I am using. Please help me in this aspect. Once again Thanks .

    :
    : [blue]Things like the max and min value and number of bits (digits) of a given type can be determined using the [b][/b] header as such:
    :
    : [code]
    : #include
    : #include
    :
    : int main()
    : {
    : std::cout << std::numeric_limits<unsigned int>::min() << std::endl
    : << std::numeric_limits<unsigned int>::max() << std::endl;
    : std::cout << std::numeric_limits<int>::min() << std::endl
    : << std::numeric_limits<int>::max() << std::endl;
    : std::cout << std::numeric_limits<unsigned char>::digits << std::endl;
    : return 0;
    : }[/code]
    : Which on my machine outputs:
    : [code]0
    : 4294967295
    : -2147483648
    : 2147483647
    : 8[/code]I don't know if this can be done for bit fields. They are user defined in size and the system would not necessarily know the min/max values.[/blue]
    :

    [green]Green text[/green]

Sign In or Register to comment.