Showing numbers algorithm

D

Darksun4

Hi all,
I am making an emulator and I want to show all its memory contents. But i
don't want to say :
memory address 1 : xxxx
memory address 2 : xxxx

but something like that :
memory address 0001 : xxxx
memory address 0002 : xxxx

I hope you understand what I mean. I tried doing it with strings but I know
there is a simplier way ...
Thanks is advance
 
R

Richard Bos

Darksun4 said:
don't want to say :
memory address 1 : xxxx
memory address 2 : xxxx

but something like that :
memory address 0001 : xxxx
memory address 0002 : xxxx

I hope you understand what I mean. I tried doing it with strings but I know
there is a simplier way ...

Yes, there is. Use a precision specifier in your printf() format:

printf("memory address %.4u: %.2x\n", address, memory[address]);

This presumes both address and memory are unsigned ints, of course.

Richard
 
J

Jens.Toerring

Yes, there is. Use a precision specifier in your printf() format:
printf("memory address %.4u: %.2x\n", address, memory[address]);
This presumes both address and memory are unsigned ints, of course.

Wouldn't the OP better off with a field width (and a preceeding '0'
to fill with zeros instead of spaces) like

printf("memory address %04u : %02x\n", address, memory[address]);

Regards, Jens
 
D

Darksun4

Yes, there is. Use a precision specifier in your printf() format:
printf("memory address %.4u: %.2x\n", address, memory[address]);
This presumes both address and memory are unsigned ints, of course.

Wouldn't the OP better off with a field width (and a preceeding '0'
to fill with zeros instead of spaces) like

printf("memory address %04u : %02x\n", address, memory[address]);
Thanks a lot guys :) That's what I wanted ( I should have man printf first
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top