Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:
C#
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:
C#
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.
Posted
Comments
Tomazas77 30-Dec-11 14:13pm    
output still not right:
random: Length: 14 (Hex 2109560Ì203078u203078'203078á203078r203078½203078â20307
8r203078‗203078}2030784203078<203078¼2030786)

desKey: Length: 14 (Hex 203078☺203078☻203078♥203078♦203078♣203078♠2030782030720
3078 203078
203078♫)03078♀203078
Press any key to continue . . .

It shoud come like
random: Length: 14 (Hex DE 75 27 A0 72 AB ......)
desKey: Length: 14 (Hex 01 02 03 04 05 06 ......)

Quite a few things to address here.
C++
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;
}

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?
 
Share this answer
 
Comments
JackDingler 30-Dec-11 14:51pm    
Good catch on the sizeof(a) thing.

An alternative is:

unsigned char rnd = "\xDE\x75\x27\xA0\x72\xAB\x83\x72\xF2\x7D\x34\x3C\xAC\x36";

Then you can do a strlen(rnd) to get the length.
Richard MacCutchan 30-Dec-11 15:11pm    
Rather pointless since the caller is passing the length as the third parameter.
JackDingler 30-Dec-11 15:29pm    
Heh, my brain is used up from doing a long code merge....
Richard MacCutchan 30-Dec-11 15:39pm    
You're lucky, I rarely have an excuse :(
Sergey Alexandrovich Kryukov 30-Dec-11 22:34pm    
Sure, a 5.
--SA
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:

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(), [](unsigned char c){std::cout << hex << uppercase << (int)c;});


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

C++
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);
 
Share this answer
 
v4
Comments
[no name] 30-Dec-11 22:09pm    
The codeproject javascript editor is really buggy... it was modifying my c++ code... had to fix it twice.
Sergey Alexandrovich Kryukov 30-Dec-11 22:44pm    
Good to see a use of lambda in C++, my 5.
--SA
This worked for me (good enough for testing):

C#
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;
}
 
Share this answer
 
Comments
Richard MacCutchan 30-Dec-11 15:50pm    
Why use sprintf() when you can use cout directly? You are also converting a simple const char* to a std::string for no reason, but to add overhead to your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900