sprintf问题 [英] sprintf problem

查看:55
本文介绍了sprintf问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想以十六进制格式显示指针指向的地址

,就像我的调试器显示它一样。

说我有P * ptr;其中P是某种类型。


我试过:

std :: sprintf(message,"%#0x,(unsigned int)ptr);


但是这给了我:0xFFFF例如,当我想要0x0000FFFF时,即,我不要/ b $ b想要丢弃任何前导零。我知道这只是一个小小的化妆品问题,但我仍然想知道如何做到这一点。


感谢您的回复!


/ WP

Hello, I want to display the address a pointer points to in hexadecimal form
in the same way my debugger displays it.
Say I have P* ptr; where P is some type.

I tried:
std::sprintf(message, "%#0x, (unsigned int)ptr);

But that gives me: 0xFFFF for example, when I want 0x0000FFFF, i.e., I dont
want to drop any leading zeros. I know this is just a small cosmetic
problem, but I still would like to know how to accomplish this.

Thanks for any replies!

/ WP

推荐答案



" William Payne" < MI ************** @ student.liu.se>在留言中写道

news:ci ********** @ news.island.liu.se ...

"William Payne" <mi**************@student.liu.se> wrote in message
news:ci**********@news.island.liu.se...
你好,我想要显示将指针指向十六进制
形式,就像我的调试器显示它一样。
说我有P * ptr;其中P是某种类型。

我试过:
std :: sprintf(消息,"%#0x,(unsigned int)ptr);

但是这给了我:0xFFFF例如,当我想要0x0000FFFF时,即I
不想丢弃任何前导零。我知道这只是一个小的化妆品问题,但我仍然想知道如何做到这一点。

感谢您的回复!
Hello, I want to display the address a pointer points to in hexadecimal form in the same way my debugger displays it.
Say I have P* ptr; where P is some type.

I tried:
std::sprintf(message, "%#0x, (unsigned int)ptr);

But that gives me: 0xFFFF for example, when I want 0x0000FFFF, i.e., I dont want to drop any leading zeros. I know this is just a small cosmetic
problem, but I still would like to know how to accomplish this.

Thanks for any replies!



#include< stdio.h>


#define char_count(sizeof(unsigned int)* 2)


int main()

{

unsigned int i = 0;

int * ptr =& i;

char message [2 * sizeof * ptr] = {0};


sprintf(message,"%0 * x",2 * sizeof ptr,(unsigned int)ptr);


put(留言);

返回0;

}


备注:


将指向unsigned int的指针的结果定义为实现。


指示指针的格式说明符是%p。


使用%p说明符生成的文本格式是实现

定义。


类型的大小' 'unsigned int''不一定是四个字节(但必须是

至少两个字节)。


一个字节的大小不一定是8位(但必须是

至少8位)。


IOW:上面的代码不便携。


-Mike



#include <stdio.h>

#define char_count (sizeof (unsigned int) * 2)

int main()
{
unsigned int i = 0;
int *ptr = &i;
char message[2 * sizeof *ptr] = {0};

sprintf(message, "%0*x", 2 * sizeof ptr, (unsigned int)ptr);

puts(message);
return 0;
}

Notes:

The result of casting a pointer to unsigned int is implementation defined.

The format specifier indicating a pointer is %p.

The format of the text produced with the %p specifier is implementation
defined.

The size of type ''unsigned int'' is not necessarily four bytes (but must be
at least two bytes).

The size of a byte is not necessarily eight bits (but must be at
least eight bits).

IOW: the above code is not portable.

-Mike




" Mike Wahler < MK ****** @ mkwahler.net>在消息中写道

新闻:Eh ************** @ newsread1.news.pas.earthlink .net ...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Eh**************@newsread1.news.pas.earthlink .net...
# include< stdio.h>

#define char_count(sizeof(unsigned int)* 2)
#include <stdio.h>

#define char_count (sizeof (unsigned int) * 2)




不使用此宏。只是一个'心理记录''

我忘了扔掉。 :-)


-Mike



This macro is not used. Just a ''mental note''
I forgot to throw away. :-)

-Mike




" Mike Wahler" < MK ****** @ mkwahler.net>在消息中写道

news:am *************** @ newsread1.news.pas.earthlin k.net ...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:am***************@newsread1.news.pas.earthlin k.net...

Mike Wahler < MK ****** @ mkwahler.net>在消息中写道
新闻:Eh ************** @ newsread1.news.pas.earthlink .net ...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Eh**************@newsread1.news.pas.earthlink .net...




使用C ++ IOStreams:


#include< iomanip>

#include< ios>

#include< ; iostream>

#include< sstream>


int main()

{

int i = 0;

int * ptr =& i;

std :: ostringstream oss;


oss< < std :: hex

<< std :: setfill(''0'')

<< std :: setw(2 * sizeof * ptr)

<< ptr

<< ''\ n'';


std :: cout<< oss.str()。c_str()<< ''\ n'';

返回0;

}

-Mike



Using C++ IOStreams:

#include <iomanip>
#include <ios>
#include <iostream>
#include <sstream>

int main()
{
int i = 0;
int *ptr = &i;
std::ostringstream oss;

oss << std::hex
<< std::setfill(''0'')
<< std::setw(2* sizeof *ptr)
<< ptr
<< ''\n'';

std::cout << oss.str().c_str() << ''\n'';
return 0;
}
-Mike


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

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