printf(): giving format to an unsigned long questions

Discussion in 'C Programming' started by Lathe_Biosas, Mar 14, 2006.

  1. Lathe_Biosas

    Lathe_Biosas Guest

    Hi

    I would like to print on a win cmd console a register value, the value
    is an "unsigned long" and have some output like these:

    Register: 0x00000000

    Tryed with

    printf("Register: %#010lx \n", register);
    printf("Register: %#08lx \n", register);

    but I get 0x000003, only the first 6 values ??? from LSB to MSB,
    normally MSBs are cero, but I would like to see them all.

    Is there a way to print, hexadecimal, the hole 32 bits and add a nice
    0x at the beginning?
    I have read some books, googled and couldn't give a solution, any help
    or info would be kindly appreciated.

    Best Regards
     
    Lathe_Biosas, Mar 14, 2006
    #1
    1. Advertising

  2. On Tuesday 14 March 2006 21:26, Lathe_Biosas opined (in
    <>):

    > Hi
    >
    > I would like to print on a win cmd console a register value, the
    > value is an "unsigned long" and have some output like these:
    >
    > Register: 0x00000000
    >
    > Tryed with
    >
    > printf("Register: %#010lx \n", register);
    > printf("Register: %#08lx \n", register);
    >
    > but I get 0x000003, only the first 6 values ??? from LSB to MSB,
    > normally MSBs are cero, but I would like to see them all.
    >
    > Is there a way to print, hexadecimal, the hole 32 bits and add a nice
    > 0x at the beginning?
    > I have read some books, googled and couldn't give a solution, any help
    > or info would be kindly appreciated.


    What's your compiler? Are you telling us all?

    On my GCC, for:

    #include<stdio.h>

    unsigned long reg = 0x12345678;

    int main(void)
    {
    printf("Register: %#010lx \n", reg);
    printf("Register: %#08lx \n", reg);
    }

    I get:

    Register: 0x12345678
    Register: 0x12345678

    Which is, I guess, what you expected.

    --
    BR, Vladimir

    This novel is not to be tossed lightly aside, but to be hurled with
    great force.
    -- Dorothy Parker
     
    Vladimir S. Oka, Mar 14, 2006
    #2
    1. Advertising

  3. Lathe_Biosas wrote:
    > Hi
    >
    > I would like to print on a win cmd console a register value, the value
    > is an "unsigned long" and have some output like these:
    >
    > Register: 0x00000000
    >
    > Tryed with
    >
    > printf("Register: %#010lx \n", register);
    > printf("Register: %#08lx \n", register);


    Minor nit, the space before the newline (\n) is redundant and need not
    be output
    to a text stream. [This applies to all trailing whitespace on a line
    before the new-
    line.]

    > but I get 0x000003, only the first 6 values ??? from LSB to MSB,
    > normally MSBs are cero, but I would like to see them all.


    Please show a complete compilable program that exhibits the problem.

    > Is there a way to print, hexadecimal, the hole 32 bits and add a nice
    > 0x at the beginning?


    #include <stdio.h>

    int main(void)
    {
    unsigned long n = 0xDEADBEEF;
    printf("Register: 0x%08lX\n", n);
    return 0;
    }

    --
    Peter
     
    Peter Nilsson, Mar 14, 2006
    #3
  4. Lathe_Biosas

    Alex Fraser Guest

    "Vladimir S. Oka" <> wrote in message
    news:dv7d1q$aaj$-infra.bt.com...
    > On Tuesday 14 March 2006 21:26, Lathe_Biosas opined (in
    > <>):
    > > I would like to print on a win cmd console a register value, the
    > > value is an "unsigned long" and have some output like these:
    > >
    > > Register: 0x00000000
    > >
    > > Tryed with
    > >
    > > printf("Register: %#010lx \n", register);
    > > printf("Register: %#08lx \n", register);
    > >
    > > but I get 0x000003, only the first 6 values ??? from LSB to MSB,
    > > normally MSBs are cero, but I would like to see them all.

    [snip]
    > On my GCC, for:
    >
    > #include<stdio.h>
    >
    > unsigned long reg = 0x12345678;
    >
    > int main(void)
    > {
    > printf("Register: %#010lx \n", reg);
    > printf("Register: %#08lx \n", reg);
    > }
    >
    > I get:
    >
    > Register: 0x12345678
    > Register: 0x12345678
    >
    > Which is, I guess, what you expected.


    Try changing reg to (eg) 0x1234. If I do, I get (as I would expect):

    Register: 0x00001234
    Register: 0x001234

    In other words, the "0x" prefix (present due to the # flag) is counted as
    part of the field width, but your example forces the field width to be
    exceeded.

    Changing reg to 0 gives me:

    Register: 0000000000
    Register: 00000000

    This is also as expected; the "0x" prefix is only added when the value is
    non-zero. But this apparently isn't what the OP wants; for that the simple
    solution is to use "0x%08lx".

    Alex
     
    Alex Fraser, Mar 14, 2006
    #4
  5. Lathe_Biosas

    Lathe_Biosas Guest

    Hi

    Thank you very much for the answers and explaining
    It worked with "0x%08lx", is good to know that # works only when the
    value is non-zero
    Best Regards
     
    Lathe_Biosas, Mar 15, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    707
    Eric Sosman
    Jul 8, 2003
  2. Joerg Schwerdtfeger

    printf / sscanf unsigned long long int variables

    Joerg Schwerdtfeger, Apr 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    40,824
    Joerg Schwerdtfeger
    Apr 2, 2004
  3. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,218
    Peter Shaggy Haywood
    Sep 20, 2005
  4. TK
    Replies:
    3
    Views:
    437
  5. Schraalhans Keukenmeester

    printf unsigned long long as hex

    Schraalhans Keukenmeester, Jul 1, 2007, in forum: C Programming
    Replies:
    5
    Views:
    32,945
    Army1987
    Jul 2, 2007
Loading...

Share This Page