Covert int array to char array in C.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepsysargus
    New Member
    • Aug 2015
    • 1

    Covert int array to char array in C.

    I wish to convert integer array to a character array.

    Eg:
    void main()
    {

    int i,temp[512];

    for(i=0;i<512;i ++)
    temp[i] = i+1;

    }
    How to convert temp array to a character array?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    A char is an integer. An int is an integer. Just move the char element to the int element.

    There's only one snag. Does your char array contain ASCII values?

    If so you need to convert from ASCII to integer. Do this by subtracting 49 from the char element value. Why 49? Look at your ASCII table.

    Post again if you need more info.

    Comment

    • kiseitai2
      New Member
      • Jul 2007
      • 93

      #3
      Also, remember that a char is a special integer in that it is 8 bits in size. Thus, if you go beyond 255, you will see integer overflows, which means your char element will be wrapped around. Example, 256 -> 0 in unsigned char because the 256 is beyond the size. In binary, 1111 1111 (255) + 0000 0001(1) = 1 0000 0000(0 w/ carry 1 or carry flag on). Just keep that in mind so you don't freak out by the results of your computation.

      Comment

      Working...