I have a string "SOMETHING /\xff" and I want to save the hex representation of 0xff into a char buffer. So I strncpy everything after the forward slash (/) into my buffer (let’s call it buff).
However, when I use the gdb command print \x buff to view the contents of buff, it doesn’t show 0xff. Any ideas as to what could be wrong? Is it possible that my forward slash is messing things up?
I think your problem has to do with the way you’re printing the variable in gdb. Take this simple program, compile as debug (
-gif using gcc), and run and break at the firstputsstatement:If I try and print
ptrthe way you’ve mentioned,p /x ptr, it’ll print the value ofptr(the address it’s pointing to):However if I do the same command for
arr, I’ll get:That’s because gdb can see that
arris of typechar[6]and notchar*. You can get the same results with the commandp /x "str \xff", which is useful for testing things:Now if you want to be able to print a certain amount of bytes from the address pointed to by a pointer, use the examine (
x) memory command instead of print (p):That’ll print 6 bytes in hex from the address pointed to by
ptr. Try that with yourbuffvariable and see how you go.Alternatively, another thing you can try is:
This will treat the
charpointed to byptras the first element in an array of 6chars, and will allow you to use the print command.