Transformation of integer and character types and how decimals are converted into binary

Source: Internet
Author: User

Atoi: A function that converts a string to the corresponding number

How decimals are converted to binary.

The decimal fraction part, multiplied by 2, if the product is less than 1, the standard is 0, continues to multiply by 2;

If the product is greater than 1, the standard is 1, the difference of the product minus 1 is multiplied by 2.

So go on until the difference is 0.

For example 0.625,→1.25, write down 0.1;

1.25-1=0.25,0.25*2=0.5, write down 0.10.

0.5*2=1,

The ∴0.625 is converted to binary 0.101.

-1.5 converts to binary -1.1.

scanf encounters the end of a space and gets () does not because it has no buffer

Methods to get the characters: scanf (), gets (), GetChar ()

printf is a row buffer, encountered \ n output, or full row output

printf and scanf are using the same buffer.

Print pointer:%p print unsigned shaping:%u print hex:%x

Converts a character type to an integral type:

#include <stdio.h>

#include <string.h>

#define MAX_SIZE 100

int My_atoi (char *src)

{

int flag = 1;

int result = 0;

if (*src = = '-')

{

flag =-1;

src++;

}

while (*src!= ' ")

{

if (*src >= ' 0 ' && *src <= ' 9 ')

{

Result = result * + (*SRC-' 0 ');

}

Else

{

Break

}

src++;

}

return result * FLAG;

}

int main ()

{

int i;

int Len;

int result = 0;

Char Src[max_size] = {0};

printf ("Please input string:\n");

Gets (SRC);

result = My_atoi (SRC);

printf ("result =%d\n", result);

return 0;

}

Converts an integer to a character type:

#include <stdio.h>

#include <string.h>

#define MAX_SIZE 1024

char * INT2STR (int num)

{

char temp;

static char Result[max_size] = {0};

int i = 0;

int len = 0;

while (num!= 0)

{

Result[i] = (num% 10) + ' 0 ';

num = NUM/10;

i++;

}

Result[i] = ' the ';

Len = strlen (result);

for (i = 0; i < LEN/2; i++)

{

temp = Result[i];

Result[i] = result[len-1-i];

Result[len-1-I] = temp;

}

return result;

}

int main ()

{

int num;

printf ("Please input num:\n");

scanf ("%d", &num);

Char *result = INT2STR (num);

printf ("result =%s\n", result);

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.