在printf功能上需要帮助 [英] Need help on printf function

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

问题描述



我对C ++不太熟悉,但是我试图做一些简单的事情:将所有字符打印为十六进制单行.

测试功能:

Hi,

I am not much familiar with C++ but I am trying to do few simple things: print all characters as hex in single line.

test function:

void Test::Start(void)
{
    unsigned char rnd[14]={0xDE ,0x75 ,0x27 ,0xA0 ,0x72 ,0xAB ,0x83 ,0x72 ,0xF2 ,0x7D ,0x34 ,0x3C ,0xAC ,0x36};

    prn("random:",rnd,14);
    for (int i=0; i<(int)sizeof(desKey); i++)
      { desKey[i]=char (i+1); }
    prn("desKey:",desKey,14);
}



打印功能:



print function:

void Test::prn(std::string txt, unsigned char *a, int nLen)
{
    int len = (int) sizeof(a);
    printf("\n%t  Length: 2%d (Hex 2%x)\n", txt ,nLen);
    for (int i=0;i<nLen;i++) //go through all elements
        std::cout<<' 0x'<<std::hex<<a[i];
    std::cout<<std::endl;
}



不幸的是,输出不打印字符串,也不打印字符作为十六进制值.长度也打印不正确.



Output unfortunately does not print string and also does not print chars as hex values. Length printed incorrectly as well.

推荐答案

这里有很多要解决的问题.
Quite a few things to address here.
void Test::prn(std::string txt, unsigned char *a, int nLen)
{
    int len = (int) sizeof(a);
    printf("\n%t  Length: 2%d (Hex 2%x)\n", txt ,nLen);
    for (int i=0;i        std::cout<<' 0x'<<std::hex<<a[i];
    std::cout<<std::endl;
}


为什么将常量字符指针(txt)定义为std::string?
sizeof(a)始终为4(在64位系统上为8).
您忘记了在printf()语句中包含十六进制值的参数.实际上,您似乎在参数说明和实际参数之间感到困惑. %t应该代表什么?
为什么混合printf()cout调用;使用一个或另一个来保持一致性?


Why have you defined a constant character pointer (txt) as std::string?
sizeof(a) will always be 4 (or 8 on a 64 bit system).
You forgot to include the parameter for the hex value in your printf() statement. In fact you seem to be getting confused between your parameter specifications and actual parameters. What is %t supposed to represent?
Why mix printf() and cout calls; use one or the other for consistency?


Tomazas,

如果使用的是支持lambda函数的C ++ 11编译器...例如Visual Studio 2010,则只需执行以下几行代码即可:

Hi Tomazas,

If you are using a C++11 compiler that supports lambda functions...such as Visual Studio 2010 then you can do this with only a few lines of code:

unsigned char rnd[14]={0xDE ,0x75 ,0x27 ,0xA0 ,0x72 ,0xAB ,0x83 ,0x72 ,0xF2 ,0x7D ,0x34 ,0x3C ,0xAC ,0x36};
vector<unsigned char> v;
v.assign(rnd,rnd+_countof(rnd));
std::for_each(v.begin(), v.end(), [](unsigned char c){std::cout << hex << uppercase << (int)c;});



使用较旧的C ++编译器,您可以使用函子:



With an older C++ compiler you could use a functor:

void print(unsigned char c)
{
	std::cout << hex << uppercase << (int)c;
}

unsigned char rnd[14]={0xDE ,0x75 ,0x27 ,0xA0 ,0x72 ,0xAB ,0x83 ,0x72 ,0xF2 ,0x7D ,0x34 ,0x3C ,0xAC ,0x36};
vector<unsigned char> v;
v.assign(rnd,rnd+_countof(rnd));
std::for_each(v.begin(), v.end(),print);


这对我有用(足以测试):

This worked for me (good enough for testing):

void Test::prn(std::string txt, unsigned char *a, int nLen)
{
    printf("\n%s  Length: %d \n", txt.c_str() ,nLen);
    for (int i=0;i<nLen;i++)
    {
        int v = (int) a[i];
        char temp[16];
        sprintf(temp,"%02X, ",v);
        std::cout<<' '<<temp;
    }
    std::cout<<")"<<std::endl;
}


这篇关于在printf功能上需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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