Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does C++ realize the conversion of numbers?

Shulou Source: shulou.com Published: 2022-05-31 14:28:32 09月12日 Update

In this article, the editor introduces in detail "how to achieve digital conversion in C++". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve digital conversion in C++" can help you solve your doubts. Let's follow the editor's ideas slowly and deeply, and let's learn new knowledge.

Algorithm:

The core of this kind of algorithm is the processing of negative numbers, that is, the conversion of complements, num = ((- num) ^ 0xffffffff) + 1.

For the conversion of different bases, the operation is carried out by taking the remainder.

For the conversion of different numbers, use a ^ b to get different values, and then clear the rightmost 1 by a & (a muri 1).

Topic 1: converting numbers to hexadecimal

Code implementation:

Func toHex (num int) string {/ / uses the array to store 16 digits ss: = [] string {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e" "f"} res: = "a: = [] string {} if num = = 0 {return" 0 "} if num > 4} / / number of digits here From left to right is high to low, so convert for iRV = len (a)-1 I > = 0 return res return res-{res + = a [I]}

Topic 2: integer conversion

Code implementation:

Func convertInteger (An int B int) int {/ / negative conversion into complement if A < 0 {A = ((- A) ^ 0xffffffff) + 1} if B < 0 {B = ((- B) ^ 0xffffffff) + 1} t: = A ^ B / / is used to calculate the different digits of An and B, 1 for t! = 0 {t & = (t color 1) / / to make the rightmost 1 on the side is set to 0 C++} return c} read here This article "how to achieve digital conversion in C++" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.

Tags: C numbers different digits articles codes content right algorithms complements negative numbers topics handling proper that is low order remainder hexadecimal ideas values numbers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology Shulou Tech Info Microsoft Huawei