如何将 long long ASCII 十六进制值转换为字符串?

发布于 2024-12-15 12:32:17 字数 424 浏览 2 评论 0原文

我有一个 long long 保存 ASCII 十六进制值,并希望将其转换为字符串。我有这样的代码:

char myBuffer[8];
long long myLongLong = 0x7177657274797569;
sprintf(myBuffer,"%c%c%c%c%c%c%c%c",myLongLong);
int x;
cout << myBuffer;
cin >> x;
return 0;

十六进制代码应该是 "qwertui",但它总是给出其他值。

我尝试使用 %c%s%X 但它没有给我所需的输出,最接近的是 %c 但它只打印出一个字符。

I have a long long holding ASCII hex values and want to convert it to a string. I have this code:

char myBuffer[8];
long long myLongLong = 0x7177657274797569;
sprintf(myBuffer,"%c%c%c%c%c%c%c%c",myLongLong);
int x;
cout << myBuffer;
cin >> x;
return 0;

The hex code should be "qwertyui", but it always gives other value.

I tried with %c, %s, %X but it doesn't give me the output I need, the closest was %c but it prints out only one char.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

剪不断理还乱 2024-12-22 12:32:17

该代码在很多方面都是错误的,我不知道从哪里开始...

  • myBuffer 太小,无法容纳 8 chars + NUL 终止符,即。应该是myBuffer[9]
  • sprintf 需要 8 个参数,而您只传递 1 个。其他必需的参数将是堆栈上的任何参数。
  • myLongLong 不是 char
  • 您没有考虑字节顺序。
  • 您正在使用 C 函数并在 C++ 中以 C 方式执行操作。为什么不使用 std::string 来代替 C 样式字符串,并使用 stringstream 来替代 sprintf 呢?

最接近您想要的几乎工作示例,与您的示例类似,类似于:

#include <cstdio>
#include <iostream>

using namespace std;

int main(void)
{
   char myBuffer[9];
   long long myLongLong = 0x7177657274797569;
   char *c_ptr = (char*)&myLongLong;
   sprintf(myBuffer,"%c%c%c%c%c%c%c%c", c_ptr[0], c_ptr[1], c_ptr[2], c_ptr[3], c_ptr[4], c_ptr[5], c_ptr[6], c_ptr[7]);
   int x;
   cout<<myBuffer;
   cin>>x;
   return 0;
}

这将在我的小设备上输出“iuytrewq” - 字节序机器。正如我提到的,这没有考虑字节序。如果机器是小端字节序,那么您可以反向读取/打印字节。

我真的不明白你为什么要这样做......

That code is wrong in so many ways I don't know where to start...

  • myBuffer is too small to hold the 8 chars + the NUL terminator, ie. should be myBuffer[9].
  • sprintf is expecting 8 arguments, you're only passing 1. The other required arguments will be whatever's on the stack.
  • myLongLong is not a char
  • You don't take into account endianness.
  • You're using C functions and doing things in a C way in C++. Why don't you use std::strings as opposed to C-style strings and stringstreams as an alternative to sprintf?

The closest almost working example of what you want, as similar to your example, is something like:

#include <cstdio>
#include <iostream>

using namespace std;

int main(void)
{
   char myBuffer[9];
   long long myLongLong = 0x7177657274797569;
   char *c_ptr = (char*)&myLongLong;
   sprintf(myBuffer,"%c%c%c%c%c%c%c%c", c_ptr[0], c_ptr[1], c_ptr[2], c_ptr[3], c_ptr[4], c_ptr[5], c_ptr[6], c_ptr[7]);
   int x;
   cout<<myBuffer;
   cin>>x;
   return 0;
}

Which will output "iuytrewq" on my little-endian machine. As I mentioned, that doesn't take into account the endianness. If the machine is little-endian then you could read/print the bytes in reverse.

I really don't understand why you're trying to do this though...

○闲身 2024-12-22 12:32:17

你可以尝试

union { char buf[8]; long long num; } u;
u.num =  0x7177657274797569LL;
cout << u.str << endl;

,但我不明白你真正想要做什么。那么字节序呢?

You could try

union { char buf[8]; long long num; } u;
u.num =  0x7177657274797569LL;
cout << u.str << endl;

But I don't understand what you want really to do. What about endianness ?

我的影子我的梦 2024-12-22 12:32:17

使用字符串流

long long myLongLong = 0x7177657274797569;
std::stringstream ss;
ss << std::hex << myLongLong;
std::cout << ss << std::endl

Use a string stream

long long myLongLong = 0x7177657274797569;
std::stringstream ss;
ss << std::hex << myLongLong;
std::cout << ss << std::endl
暖心男生 2024-12-22 12:32:17

您想将 long-long 的每个字节打印为 ascii 字符吗?

然后,您需要循环一次 long long 提取一个字节,查看位移位和掩码。

提示通常更容易(如果您知道长度)从最后一个字节开始工作并右移

,或者 - 您可以将 long-long memcpy 到 char 数组中 - 除了任何字节排序问题

You want to print each byte of the long-long as an ascii char?

Then you need to loop over the long long extracting one byte at a time, look at bit shifts and masking.

Hint it's generally easier (if you know the length) to work from the last byte and shift right

or - you could just memcpy the long-long into the char array - except for any byte ordering issues

愚人国度 2024-12-22 12:32:17

尝试以下代码。

#include <iostream>

using namespace std;

int main(void)
{

char myBuffer[8];
long long myLongLong = 0x7177657274797569;

for(int i = 0; i<8;i++)
{
    myBuffer[i] = myLongLong>>(64-(i+1)*8);
}

cout<<myBuffer<<endl;

return 0;

}

Try the following code.

#include <iostream>

using namespace std;

int main(void)
{

char myBuffer[8];
long long myLongLong = 0x7177657274797569;

for(int i = 0; i<8;i++)
{
    myBuffer[i] = myLongLong>>(64-(i+1)*8);
}

cout<<myBuffer<<endl;

return 0;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文