printf hex array woes

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

However, I have one string, and 2 arrays of 8 bytes to print out. I'm doing this:

int m = input_line( buf, sizeof(buf) );

        sscanf(buf, " cmd % c", plain);
		
		DES_Encrypt(plain, cypher, key);
		
			printf("% s % x % x", plain, *key, *cypher);

Ignore the spaces between % and the formatting - this website rejects it, despite being withing code tags :evil:
I'm linking with libprintf_min and libscanf_min. Chip is ATXMega128A1
cypher and key are
uint8_t key[8] = { ... } //secret key ;0

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
and 2 arrays of 8 bytes to print out

% x will not print out an array of hex values, it will print out the value passed as a hex value. You are passing it *key, so it will print out the value of the first element of key. If key and cypher already have the hex values already in ASCII, you just need to use % s and send in key and cypher directly (but make sure that each is really 9 long and the last element is a NULL).

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
(but make sure that each is really 9 long and the last element is a NULL).

Unfortunately, no - the key is just 8 plain 'ol bytes. I suppose a null could be appended to it, but they are not in ascii.

So you're saying I should convert the 64 bits into ascii first? Is there not a standard function for that?

---
EDIT

I found someone's HtoA function, using that is more or less acceptable

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Btw., % c reads just a single character from the input stream,
without NUL-terminating anything, so the logical counterpart to
it for printf would be another % c format, but you have to
dereference the pointer ("*plain") in the argument list for
printf. As % c consumes /any/ character (including white space,
new line etc.) the scanf is complete overkill; *plain will always
get the value buf[0].

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.