Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ realize the conversion of numbers?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report