C++将长十六进制字符串转换为二进制
C++ converting long hex string to binary
我在这里找到了类似的解决方案:十六进制2bin C++缺少标点符号并使用该答案:
std::string hex2bin(std::string const& s) {
std::string sOut;
sOut.reserve(s.length()/2);
std::string extract;
for (std::string::const_iterator pos = s.begin(); pos<s.end(); pos += 2)
{
extract.assign(pos, pos+2);
sOut.push_back(std::stoi(extract, nullptr, 16));
}
return sOut;
}
int main()
{
printf("DECODED: %sn", hex2bin("5468697320697320612031323320746573742e").c_str());
}
这是打印,如示例所示:
DECODED: This is a 123 test.
但它似乎不适用于较长的十六进制字符串,例如:
printf("DECODED: %sn", hex2bin("0200000081cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122bc7f5d74df2b9441a42a14695").c_str());
结果是:
DECODED:
那么如何用C++来完成它。
我更熟悉php,并且有函数hex2bin可以完成所有工作。
关于这一点的小更新:我正在使用该功能:
std::string hex2bin(std::string s) {
std::string rc;
int nLen = s.length();
int tmp;
for (int i(0); i + 1 < nLen; i += 2) {
if (std::istringstream(s.substr(i, 2)) >> std::hex >> tmp) {
rc.push_back(tmp);
}
}
return rc;
}
该函数逐个字符返回字符:C++
index 0: 2
index 1: 0
index 2: 0
index 3: 0
index 4: -127
index 5: -51
index 6: 2
index 7: -85
index 8: 126
index 9: 86
index 10: -98
index 11: -117
index 12: -51
index 13: -109
index 14: 23
index 15: -30
index 16: -2
index 17: -103
index 18: -14
index 19: -34
index 20: 68
index 21: -44
index 22: -102
index 23: -78
index 24: -72
index 25: -123
index 26: 27
index 27: -92
index 28: -93
index 29: 8
index 30: 0
index 31: 0
index 32: 0
index 33: 0
index 34: 0
index 35: 0
index 36: -29
index 37: 32
index 38: -74
index 39: -62
index 40: -1
index 41: -4
index 42: -115
index 43: 117
index 44: 4
index 45: 35
index 46: -37
index 47: -117
index 48: 30
index 49: -71
index 50: 66
index 51: -82
index 52: 113
index 53: 14
index 54: -107
index 55: 30
index 56: -41
index 57: -105
index 58: -9
index 59: -81
index 60: -4
index 61: -120
index 62: -110
index 63: -80
index 64: -15
index 65: -4
index 66: 18
index 67: 43
index 68: -57
index 69: -11
index 70: -41
index 71: 77
index 72: -14
index 73: -71
index 74: 68
index 75: 26
index 76: 66
index 77: -95
index 78: 70
index 79: -107
.PHP
index 0: 2
index 1: 0
index 2: 0
index 3: 0
index 4: 129
index 5: 205
index 6: 2
index 7: 171
index 8: 126
index 9: 86
index 10: 158
index 11: 139
index 12: 205
index 13: 147
index 14: 23
index 15: 226
index 16: 254
index 17: 153
index 18: 242
index 19: 222
index 20: 68
index 21: 212
index 22: 154
index 23: 178
index 24: 184
index 25: 133
index 26: 27
index 27: 164
index 28: 163
index 29: 8
index 30: 0
index 31: 0
index 32: 0
index 33: 0
index 34: 0
index 35: 0
index 36: 227
index 37: 32
index 38: 182
index 39: 194
index 40: 255
index 41: 252
index 42: 141
index 43: 117
index 44: 4
index 45: 35
index 46: 219
index 47: 139
index 48: 30
index 49: 185
index 50: 66
index 51: 174
index 52: 113
index 53: 14
index 54: 149
index 55: 30
index 56: 215
index 57: 151
index 58: 247
index 59: 175
index 60: 252
index 61: 136
index 62: 146
index 63: 176
index 64: 241
index 65: 252
index 66: 18
index 67: 43
index 68: 199
index 69: 245
index 70: 215
index 71: 77
index 72: 242
index 73: 185
index 74: 68
index 75: 26
index 76: 66
index 77: 161
index 78: 70
index 79: 149
所以你可以C++注意到小于 0 的值在 php 中显示在另一个 what.我想在 php 中拥有相同的功能链接。
您的示例包含数字"00",该数字将转换为字符值 0,printf 会将其解释为零终止符并停止打印。
printf("DECODED: %sn", hex2bin("0200000081cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122bc7f5d74df2b9441a42a14695").c_str());
hex2bin
函数实际上返回一个包含 80 个字符的std::string
。这只是一个显示问题。只需单独打印字符及其整数值即可查看问题:
std::string const s = hex2bin("0200000081cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122bc7f5d74df2b9441a42a14695");
for (std::string::size_type i = 0; i < s.size(); ++i)
{
std::cout << "index " << i << ": " << static_cast<int>(s[i]) << ", printed like this: " << s[i] << "n";
}
您将获得如下输出:
index 0: 2, printed like this: ☻
index 1: 0, printed like this:
index 2: 0, printed like this:
[...]
index 77: -95, printed like this: í
index 78: 70, printed like this: F
index 79: -107, printed like this: ò
在我看来,像hex2bin
这样的函数应该返回std::vector<char>
,因为它更好地传达意图,并且更难错误地打印。二进制数据不应该被馈送到std::cout
或printf
,就好像它是一个人类可读的字符串一样,而编译器没有任何机会抱怨。
相关文章:
- visual在c++中将十进制数转换为二进制数
- C++十进制到二进制,如何转换
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- C++从二进制转换为十进制数
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 我编写了代码将十进制分数转换为其二进制等效数.它编译得很好,但在执行时挂起
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- 如何在 O(n) 中将二进制和转换为二进制
- 我写的 ASCII 到二进制转换器C++向后显示二进制,如何使其正确显示?
- 十进制到二进制转换器 c++
- 在C++中将结构转换和存储为二进制
- 从二进制流中读取时,将双精度变量的地址转换为 char* 意味着什么?
- 如何在C ++中将二进制字符串128位转换为十进制字符串?
- 如何将二进制文件头示例代码 (C++/C#) 转换为 Python?
- 二进制 '==':未找到采用 'Enemy' 类型左侧操作数的运算符(或者没有可接受的转换)
- 将二进制转换为十进制,我如何要求用户仅输入二进制
- QTextStream的操作数到二进制的转换无效
- 在c++中读取和转换二进制数据
- c++转换二进制到十进制从字符串输入
- 计算器转换二进制到浮点值-我做错了什么