C ++十进制到十六进制转换问题 [英] C++ decimal to hexadecimal conversion issue

查看:120
本文介绍了C ++十进制到十六进制转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建一个程序,将十进制转换为十六进制字符串而不使用std :: hex。转换发生时,它返回十六进制的正确答案的翻转版本



例如:100(十进制)转换为46而不是64(正确的十六进制)。



无法想出这个问题的解决方案。



Trying to create a program that would convert a decimal into a hexadecimal string without using std::hex. When conversion occurs, it returns a flipped version of the correct answer for the Hexadecimal

For e.g : 100(decimal) converted to 46 instead of 64(correct hexadecimal).

Am unable to think of a solution for this problem.

int i;
int temp;
string hex;
long totalDecimal;
cin >> totalDecimal;

for (i = 0; totalDecimal != 0; i++)
{
    temp = totalDecimal % 16;

    if (temp > 10)
    {
        hex += temp + 55;
    }
    else
    {
        hex += temp + 48;
    }
    totalDecimal = totalDecimal / 16;
}

cout << hex;
return 0;





我的尝试:



使十六进制存储一个数组,例如hex [100]并使用for循环为数组中的每个元素赋值,但它只会让它更加混乱和复杂



What I have tried:

making hex store an array e.g hex[100] and using for loops to assign values to each element in the array, but it only made it much more confusing and complicated

推荐答案

你有两个选择:

1)存储十六进制数字并稍后反向显示。

2)首先从最高位开始工作。



Option2并不像听起来那么糟糕,不是真的。

1)设置一个名为leadingZero的布尔值并设置它确实

2)设置一个移位计数,设置为28(32位数字,60位64位)

3)循环所有数字。

3.1)按移位计数位移位输入,使用>>运算符并将其掩盖为数字:

You have two options:
1) Store the hex digits and display them in reverse later.
2) Work from the most significant digit first.

Option2 isn't as bad as it sounds, not realy.
1) Set up a boolean called "leadingZero" and set it to true
2) Set up a shift count, set it to 28 (for 32 bit numbers, 60 for 64 bit)
3) Loop though all digits.
3.1) Shift the input by shift count bits, using the >> operator and mask it to a digit:
digit = (input >> shiftCount) & 0xF;



3.2)如果数字不为零或者leadignZero为假,则打印数字。设置前导零为假

3.3)将shiftCount减少4并循环回3.1


3.2) If the digit not zero or leadignZero is false print the digit. Set leading Zero to false
3.3) Reduce shiftCount by 4 and loop back to 3.1


让我们看看会发生什么:



迭代1:

totalDecimal = 100

totalDecimal%16 = 4

hex =4



迭代2:

totaldecimal = 6(100/16)

totalDecimal%16 = 6

hex =46



所以每次迭代,你除以16(向右走左),但你添加到你的字符串的末尾(从左到右)。

您可以通过从右到左添加到您的字符串来修复它:



Let's look at what happens:

Iteration 1:
totalDecimal = 100
totalDecimal%16 = 4
hex = "4"

Iteration 2:
totaldecimal = 6 (100/16)
totalDecimal%16 = 6
hex = "46"

So every iteration, you divide by 16 (going right to left), but you add to the end of your string (left to right).
You could fix it by adding to your string from right to left:

string hex = "";
...
    if (temp > 10)
    {
        hex.insert(0, 1, temp + 55);
    }
    else
    {
        hex.insert(0, 1, temp + 48);
    }


假设您要打印出十进制数字的十六进制字符串,我默认使用sprintf()来填充一个char数组使用%x说明符的十六进制数。



这是旧的方式,可能不是你上司之后的事情,但它会得到你想要的东西 - 我想。
Assuming you want to print out a hex string of the decimal number I default to using sprintf() to stuff a char array with the hex number using the %x specifier.

This is the old way and probably not what you superiors are after out of you but it will get you what you want - I think.


这篇关于C ++十进制到十六进制转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆