-1

I have following program

#include <iostream>
#include <string.h>

int main(int argc, char* argv[])
{

  std::cout << std::hex << (int) argv[1][0];
  std::cout << std::hex << (int) argv[1][1];
  std::cout << std::hex << (int) argv[1][2];
  std::cout << std::hex << (int) argv[1][3];
  std::cout << std::endl;

  size_t len = strlen(argv[1]);

  std::cout << "Len: " << len << std::endl;
  
  return 0;
}

When I run it in this way:

./a.out $(python -c "print '\x01\x01\x01\x01'")

the output is:

1111

Len: 4

But when I run it in this way:

./a.out $(python -c "print '\x21\xdd\x09\xec'")

the output differs significantly.

21ffffffdd0ffffffec

Len: 2

I can't figure out why is that. It is probably something that is obvious, but I can't grasp it.

Especially I'm interested in - why strlen does not work as expected by me.

Community
  • 1
  • 1
bielu000
  • 1,826
  • 3
  • 21
  • 45

1 Answers1

1

0xdd's most significant bit is 1. Apparently, char type on your system is signed. So you get a negative value, casting it to int leaves it negative.

Solution: cast to unsigned char before; additionally, I'd prefer unsigned integers, too, for printing hexadecimal representations, so:

std::cout << std::hex << static_cast<unsigned int>(static_cast<unsigned char>(argv[1][0]));

If you prefer the C++ casts (like me) or leave the C-style ones doesn't have an impact here...

Then you have a second problem: Output encoding of python's print function; by default, you get utf-8 output, see related question.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59