How to display binary in C, how to output hexadecimal in C?

Updated on amusement 2024-03-15
15 answers
  1. Anonymous users2024-02-06

    In the standard output of the C++ language, there are hexadecimal, decimal, and octal output formats, but there is no binary output format. So if you want to output binary, you can only write your own functions for output.

    The basic idea is to take out the values on each bit by shifting the + and operations, and then output them.

    Take the binary output of 32-bit unsigned integer data as an example, ** is as follows:

    #include

    using namespace std;

    int i;

    int bit;

    int size = sizeof(v)*8;

    The size here is the number of digits of the input parameter v, and it is okay to replace it with 32, but it is more common to write it as sizeof(v)*8.

    for(i = 0; i < size; i ++

    bit = v&(1<<(size-i-1));The value of the ith digit is extracted.

    if(bit == 0) cout <<0;

    else cout <<1;

    if(i % 4 == 3)cout <<' ';To make it easier to read the output, add a space after every four digits of the output.

    Use this function to call binary print(0x1a2b3c4d);

    You can get the output.

  2. Anonymous users2024-02-05

    int aaa = 20;

    Method 1: char str[255];

    itoa(aaa, str, 2);

    Cout Method 2: Cout Method 3, 4, 5.

    Write some functions yourself.

  3. Anonymous users2024-02-04

    There is no direct way. Make it up yourself.

  4. Anonymous users2024-02-03

    The materials that need to be prepared are: computer and C language compiler.

    1. First of all, open the C language compiler and create a new initial. CPP file, for example:

    2. In the file, enter C language**: int a = 127; printf("%x", a);

    3. The compiler runs the file, and the numbers are successfully output in hexadecimal.

  5. Anonymous users2024-02-02

    C++ does not support the direct use of binary numbers, only supports the direct definition of 8, 10, and 16 bases.

    The computer stores data in binary, such as chara=97; Then the information in the computer is 01100001, printing in binary is 01100001, and printing in 10 is 97, so there is only a difference in the way of display.

    C++ natively supports the representation of three bases other than decimal system:

    1. Binary.

    Example: intx=0b1001; //x=9

    2. Octadecimal system.

    Example: inty=074; //x=60

    3. Hexadecimal.

    Example: intz=0xa3; //x=163;

  6. Anonymous users2024-02-01

    1. First open and create a new project.

    2. Add a header file.

    3. Add the main main function.

    4. Define a function binsubtracton that adds two numbers.

    5. Define int number1, number2, binsub in the main function.

    6. Use scanf to assign values to variables.

    7. Call binaddition and binsubtracton.

    8. Use printf to print the result.

  7. Anonymous users2024-01-31

    The C standard does not output binary, but it can be converted to binary with iToa().

    1. In C language programs, you can use printf() in the standard library function to output information to the screen, or use sprintf() to output information to the buffer. For integers, you can use %d, %o, %x (or %x) to output decimal, octal, and hexadecimal forms, but there seems to be a lack of binary forms.

    2. There are two ways to solve the problem, one is to use the Itoa() function provided by Windows, and the other is to design a new function by yourself: **a:

    #pragma once

    #ifndef test_h

    #define test_h

    * use itoa() to print integers in the form of binary

    param[in] n the integer to print

    return void

    void printbinarybyitoa(unsigned int n);**II:

    #include

    #include

    void printbinarybyitoa(unsigned int n)

    void printbinary(unsigned int n, unsigned char separator, unsigned char needprefixzeros)

    unsigned int i = 0;

    unsigned int mask = 0; /* the mask code to get each bit of n */

    unsigned char data[40]; /* the buffer to store the bits of n in the form of characters */

    unsigned int index = 0; /* used to access data */

    unsigned int start = 0; /* to point out where and when to store bits of n */

    data[39] = '\0';

  8. Anonymous users2024-01-30

    void b print (unsigned char b) octal binary output.

    If you want a few more cycles, you just need to change the number of cycles.

  9. Anonymous users2024-01-29

    As mentioned upstairs, binary can't be output directly, but you can programmatically output include

    void f(int n)

    int main()

    return 0;}

  10. Anonymous users2024-01-28

    No way, convert to decimal form.

  11. Anonymous users2024-01-27

    c C++ does not support binary input and output.

  12. Anonymous users2024-01-26

    int s = 0b101010

    0b is used for binary descriptions.

    0 octal.

    0x hexadecimal.

    For example, 0b10 is 2

    030 is 24

    30 is 30

    0x30 is 48

  13. Anonymous users2024-01-25

    Gives you a program written in C++, simple.

    #include

    #include

    using namespace std;

    void main()

    int a;

    cin>>a;

    cout<(a) to adjust the number of output digits.

  14. Anonymous users2024-01-24

    No, you can use the base hexadecimal.

  15. Anonymous users2024-01-23

    C++14 already directly supports binary literals, which are written by adding 0b or 0b in front of them, such as:

    int s=0b0101010;

    int s1=0b0101010;

Related questions
23 answers2024-03-15

The above statement is wrong.

How to calculate the binary of 1100 111. >>>More

8 answers2024-03-15

Convert decimal integers to binary numbers.

The divide by 2 reverse remainder method is adopted: >>>More

10 answers2024-03-15

Binary numbers. Writing is long, error-prone, and hard to remember, while the decimal system. >>>More

9 answers2024-03-15

The least squares formula is a mathematical formula, which is called curve fitting in mathematics, and the least squares method mentioned here refers specifically to the linear regression equation! The formula for least squares is b=y(average)-a*x(average).