Mutual conversion between strings and Integers

Source: Internet
Author: User

Mutual conversion between strings and Integers

1. The integer is converted to a string. You can add '0' and then sort it in reverse order.

Adding '0' will implicitly convert the number to the char type.

# Include

# Include

Int main (void)

{

Int num = 12345, j = 0, I = 0;

Char temp [7], str [7];

While (num)

{

Temp [I] = num % 10 + '0 ';

I ++;

Num = num/10;

}

Temp [I] = 0''

Printf (temp = % s, temp );

I = I-1;

// The converted string is in reverse order and must be reversed.

While (I> = 0)

{

Str [j] = temp [I];

J ++;

I --;

}

Str [j] = 0''

Return 0;

}

2. If the string is converted to an integer, You can subtract '0' and multiply it by 10. If the string is reduced to '0', It is implicitly converted to the number of int type.

# Include

# Include

 

Int main (void)

{

Int num = 12345, j = 0, I = 0, sum = 0;

Char temp [7] = {'1', '2', '3', '4', '5', ''}, str [7];

While (temp [I])

{

Sum = sum * 10 + (temp [I]-'0 ');

I ++;

}

Printf (sum = % d, sum );

Return 0;

}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.