Results 1 to 9 of 9

Thread: convert int to char...

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    How is this done, like in VB its CStr(thisInt). Its easy for int to float, etc., i know, but how can I convert an integer to a char?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Use the _itoa function.
    Code:
    //Example
    #include <stdlib.h>
    #include <stdio.h>
    
    void main( void )
    {
       char buffer[20];
       int  i = 3445;
       long l = -344115L;
       unsigned long ul = 1234567890UL;
    
       _itoa( i, buffer, 10 );
       printf( "String of integer %d (radix 10): %s\n", i, buffer );
       _itoa( i, buffer, 16 );
       printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
       _itoa( i, buffer, 2  );
       printf( "String of integer %d (radix 2): %s\n", i, buffer );
    
       _ltoa( l, buffer, 16 );
       printf( "String of long int %ld (radix 16): 0x%s\n", l, 
                                                        buffer );
    
       _ultoa( ul, buffer, 16 );
       printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,
                                                        buffer );
    }

    MSDN Info:
    _itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow
    Convert an integer to a string.

    char *_itoa( int value, char *string, int radix );

    char *_i64toa( __int64 value, char *string, int radix );

    char * _ui64toa( unsigned _int64 value, char *string, int radix );

    wchar_t * _itow( int value, wchar_t *string, int radix );

    wchar_t * _i64tow( __int64 value, wchar_t *string, int radix );

    wchar_t * _ui64tow( unsigned __int64 value, wchar_t *string, int radix );

    Routine Required Header Compatibility
    _itoa <stdlib.h> Win 95, Win NT
    _i64toa <stdlib.h> Win 95, Win NT
    _ui64toa <stdlib.h> Win 95, Win NT
    _itow <stdlib.h> Win 95, Win NT
    _i64tow <stdlib.h> Win 95, Win NT
    _ui64tow <stdlib.h> Win 95, Win NT


    For additional compatibility information, see Compatibility in the Introduction.

    Libraries

    LIBC.LIB Single thread static library, retail version
    LIBCMT.LIB Multithread static library, retail version
    MSVCRT.LIB Import library for MSVCRT.DLL, retail version


    Return Value

    Each of these functions returns a pointer to string. There is no error return.

    Parameters

    value

    Number to be converted

    string

    String result

    radix

    Base of value; must be in the range 2 – 36

    Remarks

    The _itoa, _i64toa, and _ui64toa function convert the digits of the given value argument to a null-terminated character string and stores the result (up to 33 bytes) in string. If radix equals 10 and value is negative, the first character of the stored string is the minus sign ( – ). _itow, _i64tow, and _ui64tow are wide-character versions of _itoa, _i64toa, and _ui64toa respectively.

    Generic-Text Routine Mappings

    TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
    _itot _itoa _itoa _itow
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Guest
    Thats a REALLY complicated way of doing something REALLY simple.

    Code:
    char a;
    int b;
       
    b = 65;
    a = char(b);

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    That is not what he meant:
    Code:
    //if you try your code like this it will not work
    int b = 65;
    MessageBox(NULL,char(b),"OK",MB_OK);
    You have to do it like this:
    Code:
    int b = 65;
    char txt[2];
    MessageBox(NULL,itoa(b,txt,10),"OK",MB_OK);
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    you were correct in what I was looking for Vlatko, but what is the difference between the two methods, for my own reference...
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6
    Guest
    Vlatko used the correct function (itoa), whereas Zaei's method does not convert.

  7. #7

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    right, but what's the difference between Vlatko's, which works, and Zaei's, which doesn't work?? I guess what I'm saying is...what does Zaei's code do??
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8
    Guest
    It turns 65 into 'A', rather than "65".

  9. #9

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    oh, okay, I get ya...thanks again Dennis...
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width