[英]Convert Integer to Little Endian Hex String
I'm trying to convert an integer to a little endian hex string. 我正在尝试将整数转换为小的endian十六进制字符串。 I can get to a little endian hex long but I'm not sure how to convert to string from there. 我可以长到一点endian十六进制,但是我不确定如何从那里转换为字符串。
int iNum = 17706;
// convert to long little endian hex
long lNum = (long)_byteswap_ushort(iNum);
// convert to string??
Alternatively, is there a way to go straight from an integer to little endian hex string? 另外,是否有办法直接从整数到小尾数十六进制字符串?
Thanks. 谢谢。
Use std::stringstream
to format strings. 使用std::stringstream
格式化字符串。
Also, use _byteswap_ulong
or large ints will not be accurate. 另外,使用_byteswap_ulong
或大整数将不准确。
long iNum = 17706;
// convert to long little endian hex
long lNum = (long)_byteswap_ulong(iNum);
// convert to string
std::ostringstream oss;
oss << std::hex << lNum;
std::string mystring = oss.str();
For a portable solution, just mask and shift: 对于便携式解决方案,只需掩盖和移动:
while (iNum != 0) {
int byte = iNum & 0x0F;
std::cout << std::hex << byte;
iNum /= 16;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.