convert decimal number in a hexadecimal number ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • muss

    convert decimal number in a hexadecimal number ?

    how to convert decimal number in a hexadecimal number using C?
    Please help me.
    Thanks.

  • Ian Collins

    #2
    Re: convert decimal number in a hexadecimal number ?

    muss wrote:[color=blue]
    > how to convert decimal number in a hexadecimal number using C?
    > Please help me.
    > Thanks.
    >[/color]
    What have you tried?

    --
    Ian Collins.

    Comment

    • CoL

      #3
      Re: convert decimal number in a hexadecimal number ?

      This solves ur problem...

      #include<stdio. h>

      //function to convert a value to the desired base binary,oct,Hex
      char* itoa(int val, int base){



      static char buf[32] = {0};



      int i = 30;



      for(; val && i ; --i, val /= base)



      buf[i] = "0123456789abcd ef"[val % base]; //its a c way to
      pick up that particular index value that is in [].



      return &buf[i+1];


      }


      int main()
      {

      char c;
      char* p,*q
      p=itoa(8,8);
      q=itoa(8,2);
      printf("%s",p);
      printf("%s",p);

      }

      muss wrote:[color=blue]
      > how to convert decimal number in a hexadecimal number using C?
      > Please help me.
      > Thanks.[/color]

      Comment

      • Vladimir S. Oka

        #4
        Re: convert decimal number in a hexadecimal number ?

        On Thursday 16 March 2006 05:58, muss opined (in
        <1142488729.465 257.290540@v46g 2000cwv.googleg roups.com>):
        [color=blue]
        > how to convert decimal number in a hexadecimal number using C?[/color]

        Try `printf()`, it's described in your textbook. You did read it?

        --
        BR, Vladimir

        Friction is a drag.

        Comment

        • Vladimir S. Oka

          #5
          Re: convert decimal number in a hexadecimal number ?

          On Thursday 16 March 2006 07:48, CoL opined (in
          <1142495289.587 465.154320@i40g 2000cwc.googleg roups.com>):[color=blue]
          > muss wrote:[color=green]
          >> how to convert decimal number in a hexadecimal number using C?
          >> Please help me.
          >> Thanks.[/color]
          >
          > This solves ur problem...[/color]

          No, it does not, as it doesn't work (even when all syntax errors are
          corected).

          Also, don't top-post (corrected here).
          [color=blue]
          > #include<stdio. h>
          >
          > //function to convert a value to the desired base binary,oct,Hex[/color]

          (Don't use `//` when posting to Usenet.)
          [color=blue]
          > char* itoa(int val, int base){[/color]

          You never check that inputs are valid.
          [color=blue]
          > static char buf[32] = {0};[/color]

          Are you sure 31 characters is enough for a 256-bit int on DS9K?
          [color=blue]
          > int i = 30;[/color]

          a) Why 30?
          b) Isn't it clearer to put initialisation into `for`?
          [color=blue]
          > for(; val && i ; --i, val /= base)
          >
          > buf[i] = "0123456789abcd ef"[val % base]; //its a c way to
          > pick up that particular index value that is in [].[/color]

          Obfuscated C Contest is at <www.ioccc.org> .
          [color=blue]
          >
          > return &buf[i+1];
          > }
          >
          > int main()[/color]

          It's:

          int main(void)

          Spell out what you mean.
          [color=blue]
          > {
          >
          > char c;
          > char* p,*q[/color]
          ^
          Syntax error: missing ';'.
          [color=blue]
          > p=itoa(8,8);
          > q=itoa(8,2);
          > printf("%s",p);
          > printf("%s",p);[/color]

          You need '\n' at the end of `printf` for anything to be output. You also
          never output string pointed to by `q`. Do you think it's guaranteed
          that the same memory will be allocated to `buf` on the second
          invocation of `itoa`? If you do, you are mistaken. More likely you did
          not copy and paste your code, but retyped it -- badly.

          Also:

          return 0;

          Would've been nice (although not required in C99).
          [color=blue]
          > }[/color]

          Once corrected, when I run it, I get:

          00
          00

          Which is incorrect.

          As OP's question sounds like a homework assignment, I'll leave it to two
          of you to correct this (or come up with something different).

          --
          BR, Vladimir

          Those who in quarrels interpose, must often wipe a bloody nose.

          Comment

          • CoL

            #6
            Re: convert decimal number in a hexadecimal number ?

            Who the hell is this Vladimir.....?? ?????
            I just gave him a hint of solution....
            My fuction char* itoa(int val, int base) is perfectly right ..it
            accepts only one input at a time so in main I gave two different inputs
            first try itoa(8,8)base 8..then
            itoa(8,2)base 2, not both same time...U will see the perfect answers.
            and this is hint not a narration of exact program given with a
            intention the the one who raised this problem can modify this according
            to his requirements.

            Do you think all mails posted in the groups contain exact perfect
            optimised and performant code...???


            Vladimir S. Oka wrote:[color=blue]
            > On Thursday 16 March 2006 07:48, CoL opined (in
            > <1142495289.587 465.154320@i40g 2000cwc.googleg roups.com>):[color=green]
            > > muss wrote:[color=darkred]
            > >> how to convert decimal number in a hexadecimal number using C?
            > >> Please help me.
            > >> Thanks.[/color]
            > >
            > > This solves ur problem...[/color]
            >
            > No, it does not, as it doesn't work (even when all syntax errors are
            > corected).
            >
            > Also, don't top-post (corrected here).
            >[color=green]
            > > #include<stdio. h>
            > >
            > > //function to convert a value to the desired base binary,oct,Hex[/color]
            >
            > (Don't use `//` when posting to Usenet.)
            >[color=green]
            > > char* itoa(int val, int base){[/color]
            >
            > You never check that inputs are valid.
            >[color=green]
            > > static char buf[32] = {0};[/color]
            >
            > Are you sure 31 characters is enough for a 256-bit int on DS9K?
            >[color=green]
            > > int i = 30;[/color]
            >
            > a) Why 30?
            > b) Isn't it clearer to put initialisation into `for`?
            >[color=green]
            > > for(; val && i ; --i, val /= base)
            > >
            > > buf[i] = "0123456789abcd ef"[val % base]; //its a c way to
            > > pick up that particular index value that is in [].[/color]
            >
            > Obfuscated C Contest is at <www.ioccc.org> .
            >[color=green]
            > >
            > > return &buf[i+1];
            > > }
            > >
            > > int main()[/color]
            >
            > It's:
            >
            > int main(void) //this is wrong...[/color]
            its:
            int main(int argc, char** argv)[color=blue]
            >
            > Spell out what you mean.
            >[color=green]
            > > {
            > >
            > > char c;
            > > char* p,*q[/color]
            > ^
            > Syntax error: missing ';'.
            >[color=green]
            > > p=itoa(8,8);
            > > q=itoa(8,2);
            > > printf("%s",p);
            > > printf("%s",p);[/color]
            >
            > You need '\n' at the end of `printf` for anything to be output. You also
            > never output string pointed to by `q`. Do you think it's guaranteed
            > that the same memory will be allocated to `buf` on the second
            > invocation of `itoa`? If you do, you are mistaken. More likely you did
            > not copy and paste your code, but retyped it -- badly.[/color]
            [color=blue]
            > ///what crap you trying to make ??[/color]
            [color=blue]
            > Also:
            >
            > return 0;
            >
            > Would've been nice (although not required in C99).
            >[color=green]
            > > }[/color]
            >
            > Once corrected, when I run it, I get:
            >
            > 00
            > 00
            >
            > Which is incorrect.
            >
            > As OP's question sounds like a homework assignment, I'll leave it to two
            > of you to correct this (or come up with something different).
            >
            > --
            > BR, Vladimir
            >
            > Those who in quarrels interpose, must often wipe a bloody nose.[/color]

            Comment

            • Richard Heathfield

              #7
              Re: convert decimal number in a hexadecimal number ?

              CoL said:
              [color=blue]
              > Who the hell is this Vladimir.....?? ?????[/color]

              A guy that pointed out some errors in your code, it seems.
              [color=blue]
              > I just gave him a hint of solution....[/color]

              And Vladimir gave you some hints on how to make it better.
              [color=blue]
              > My fuction char* itoa(int val, int base) is perfectly right ..[/color]

              The original doesn't appear to have reached my server (at least not yet).
              I'd be glad to look it over if you want a second opinion and are prepared
              to post it again. But if Vladimir says it's broken, I'm inclined to believe
              him. He certainly has a tendency to be right more often than not.
              [color=blue]
              > Do you think all mails posted in the groups contain exact perfect
              > optimised and performant code...???[/color]

              Of course they don't. But they ought to, if they are being presented as
              solutions rather than questions. Or at least, if short cuts /have/ been
              taken, these should be made explicit, perhaps with a brief comment.

              --
              Richard Heathfield
              "Usenet is a strange place" - dmr 29/7/1999

              email: rjh at above domain (but drop the www, obviously)

              Comment

              • Vladimir S. Oka

                #8
                Re: convert decimal number in a hexadecimal number ?


                CoL wrote:[color=blue]
                > Who the hell is this Vladimir.....?? ?????[/color]

                You are being rude...

                (A: Someone not afraid to show his full name.)
                [color=blue]
                > I just gave him a hint of solution....
                > My fuction char* itoa(int val, int base) is perfectly right ..it[/color]

                You're not telling the truth...
                [color=blue]
                > Do you think all mails posted in the groups contain exact perfect
                > optimised and performant code...???[/color]

                The answers should...
                [color=blue]
                > Vladimir S. Oka wrote:[color=green]
                > > On Thursday 16 March 2006 07:48, CoL opined (in
                > > <1142495289.587 465.154320@i40g 2000cwc.googleg roups.com>):[color=darkred]
                > > >
                > > > int main()[/color]
                > >
                > > It's:
                > >
                > > int main(void) //this is wrong...[/color]
                > its:
                > int main(int argc, char** argv)[/color]

                ....and you don't really know C.

                FYI, it can be both:

                int main(void)
                int main(int argc, char *argv[])
                or anything that an implementation cares to define

                Your

                int main()

                is close, but not exactly the same as the first one above.
                [color=blue][color=green][color=darkred]
                > > > printf("%s",p);
                > > > printf("%s",p);[/color]
                > >
                > > You need '\n' at the end of `printf` for anything to be output. You also[/color]
                >[color=green]
                > > ///what crap you trying to make ??[/color][/color]

                That's being rude twice, and that's twice too many times...
                [color=blue][color=green]
                > > Once corrected, when I run it, I get:
                > >
                > > 00
                > > 00
                > >
                > > Which is incorrect.[/color][/color]

                Care to dispute this with the output of your code? If you do, please
                post *exact* code that you compiled and ran, together with it's output.

                That way someone may be able to check your claims (but beware the sig).
                I won't, as I won't be reading any more of your posts. Goodbye.

                *PLONK*

                --
                BR, Vladimir

                Those who in quarrels interpose, must often wipe a bloody nose.

                Comment

                • CBFalconer

                  #9
                  Re: convert decimal number in a hexadecimal number ?

                  CoL wrote:[color=blue]
                  >
                  > Who the hell is this Vladimir.....?? ?????
                  > I just gave him a hint of solution....
                  > My fuction char* itoa(int val, int base) is perfectly right ..it
                  > accepts only one input at a time so in main I gave two different inputs
                  > first try itoa(8,8)base 8..then
                  > itoa(8,2)base 2, not both same time...U will see the perfect answers.
                  > and this is hint not a narration of exact program given with a
                  > intention the the one who raised this problem can modify this according
                  > to his requirements.
                  >
                  > Do you think all mails posted in the groups contain exact perfect
                  > optimised and performant code...???[/color]

                  Don't toppost. Do quote relative portions, and snip
                  irrelevancies. Ignoring your rudeness, poor spelling, poor
                  punctuation, and lacking proper code to criticize, we can say
                  little more, except that yes, we do expect code to be correct, but
                  not optimized.

                  --
                  "If you want to post a followup via groups.google.c om, don't use
                  the broken "Reply" link at the bottom of the article. Click on
                  "show options" at the top of the article, then click on the
                  "Reply" at the bottom of the article headers." - Keith Thompson
                  More details at: <http://cfaj.freeshell. org/google/>
                  Also see <http://www.safalra.com/special/googlegroupsrep ly/>


                  Comment

                  • Vladimir S. Oka

                    #10
                    Re: convert decimal number in a hexadecimal number ?

                    On Thursday 16 March 2006 10:53, Vladimir S. Oka opined (in
                    <1142506390.333 571.222260@i40g 2000cwc.googleg roups.com>):[color=blue]
                    > CoL wrote:[/color]

                    <snip>
                    [color=blue][color=green]
                    >> Vladimir S. Oka wrote:[color=darkred]
                    >> > On Thursday 16 March 2006 07:48, CoL opined (in
                    >> > <1142495289.587 465.154320@i40g 2000cwc.googleg roups.com>):[/color][/color][/color]

                    <snip>
                    [color=blue][color=green][color=darkred]
                    >> > > printf("%s",p);
                    >> > > printf("%s",p);
                    >> >
                    >> > You need '\n' at the end of `printf` for anything to be output. You
                    >> > also[/color]
                    >>[color=darkred]
                    >> > ///what crap you trying to make ??[/color][/color]
                    >
                    > That's being rude twice, and that's twice too many times...
                    >[color=green][color=darkred]
                    >> > Once corrected, when I run it, I get:
                    >> >
                    >> > 00
                    >> > 00
                    >> >
                    >> > Which is incorrect.[/color][/color]
                    >
                    > Care to dispute this with the output of your code? If you do, please
                    > post *exact* code that you compiled and ran, together with it's
                    > output.[/color]

                    FWIW, here's the code from CoL's original post, in its unadulterated
                    incorrectness (I've only reduced spacing, mostly vertical, and removed
                    C++/C99 style comments, and added missing ';'):

                    #include<stdio. h>

                    /* function to convert a value to the desired base binary,oct,Hex */
                    char* itoa(int val, int base){

                    static char buf[32] = {0};

                    int i = 30;

                    for(; val && i ; --i, val /= base)
                    buf[i] = "0123456789abcd ef"[val % base]; /* its a c way to
                    pick up that particular index value that is in []. */

                    return &buf[i+1];
                    }

                    int main()
                    {
                    char c;
                    char* p,*q; /* VSO added ';' */
                    p=itoa(8,8);
                    q=itoa(8,2);
                    printf("%s",p);
                    printf("%s",p);
                    }


                    --
                    BR, Vladimir

                    We are not loved by our friends for what we are;
                    rather, we are loved in spite of what we are.
                    -- Victor Hugo

                    Comment

                    • Rod Pemberton

                      #11
                      Re: convert decimal number in a hexadecimal number ?


                      "Richard Heathfield" <invalid@invali d.invalid> wrote in message
                      news:dvbf6l$sin $2@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
                      [color=blue]
                      > The original doesn't appear to have reached my server (at least not yet).[/color]
                      I've noticed you've had a number of problems with this. Try another open
                      read only server:

                      freenews.netfro nt.net
                      news.hdrc.edu.t w
                      nntp.aioe.org


                      Rod Pemberton


                      Comment

                      • Mark McIntyre

                        #12
                        Re: convert decimal number in a hexadecimal number ?

                        On 16 Mar 2006 02:29:42 -0800, in comp.lang.c , "CoL"
                        <apraghuvanshi@ gmail.com> wrote:
                        [color=blue]
                        >Who the hell is this Vladimir.....?? ?????[/color]

                        A regular contributor to CLC.
                        [color=blue]
                        >I just gave him a hint of solution....[/color]

                        and he pointed out a bunch of errors in your code.
                        [color=blue]
                        >My fuction char* itoa(int val, int base) is perfectly right[/color]

                        Since when is 8 in base eight "00" but also "00" in base 2?
                        [color=blue]
                        >itoa(8,2)bas e 2, not both same time...U will see the perfect answers.[/color]

                        I see. Your code was actually two different programmes merged into
                        one. Very helpful to the OP, who will have had no clue how to decipher
                        such gibberish.
                        [color=blue]
                        >and this is hint not a narration of exact program[/color]

                        Actually it now looks a lot like code posted by a ninny trying to be
                        smugly superior.
                        [color=blue]
                        >Do you think all mails posted in the groups contain exact perfect
                        >optimised and performant code...???[/color]

                        No, but if you post b*llocks disguised as solutions to problems,
                        expect people to pick holes in it.
                        Mark McIntyre
                        --
                        "Debugging is twice as hard as writing the code in the first place.
                        Therefore, if you write the code as cleverly as possible, you are,
                        by definition, not smart enough to debug it."
                        --Brian Kernighan

                        Comment

                        • Dave Thompson

                          #13
                          Re: convert decimal number in a hexadecimal number ?

                          On Thu, 16 Mar 2006 08:09:44 +0000 (UTC), "Vladimir S. Oka"
                          <novine@btopenw orld.com> wrote:
                          [color=blue]
                          > On Thursday 16 March 2006 07:48, CoL opined (in
                          > <1142495289.587 465.154320@i40g 2000cwc.googleg roups.com>):[/color]
                          [color=blue][color=green]
                          > > char* itoa(int val, int base){[/color]
                          >[/color]
                          I would make base unsigned, and probably val also.
                          [color=blue]
                          > You never check that inputs are valid.
                          >[/color]
                          Neither do standard library routines in most cases. There are
                          arguments on both sides, and (IMO properly) different decisions in
                          different situations and usages.
                          [color=blue][color=green]
                          > > static char buf[32] = {0};[/color]
                          >
                          > Are you sure 31 characters is enough for a 256-bit int on DS9K?
                          >[/color]
                          That's a fair point. Although easily fixed.
                          [color=blue][color=green]
                          > > int i = 30;[/color]
                          >
                          > a) Why 30?
                          > b) Isn't it clearer to put initialisation into `for`?
                          >[color=green]
                          > > for(; val && i ; --i, val /= base)
                          > >[/color][/color]
                          Test-at-top means that value 0 is output as no digits (an empty
                          string). While mathematically defensible, this is not what most people
                          usually want. Test-at-bottom is one fix; another is an override (or
                          digits_so_far == 0) which can be extended to the additional and often
                          useful functionality (or digits_so_far < min_digits).
                          [color=blue][color=green]
                          > > buf[i] = "0123456789abcd ef"[val % base]; //its a c way to
                          > > pick up that particular index value that is in [].[/color]
                          >
                          > Obfuscated C Contest is at <www.ioccc.org> .
                          >[/color]
                          I don't consider that obfuscated. If you don't understand that C
                          character strings, and particularly literal ones, are actually array
                          of char, you're in big trouble; and if you do, I think that is obvious
                          and admirably terse. (val % base) ["digits"] would be obfuscated.

                          The doubleslash comment doesn't add anything though.

                          <snip>[color=blue]
                          > You need '\n' at the end of `printf` for anything to be output. You also
                          > never output string pointed to by `q`. Do you think it's guaranteed
                          > that the same memory will be allocated to `buf` on the second
                          > invocation of `itoa`? If you do, you are mistaken. More likely you did
                          > not copy and paste your code, but retyped it -- badly.
                          >[/color]
                          Since he declared buf static, it certainly will remain allocated at
                          the same place throughout the program's entire execution.
                          [color=blue]
                          > Once corrected, when I run it, I get:
                          >
                          > 00
                          > 00
                          >
                          > Which is incorrect.
                          >[/color]
                          After fixing the buffer reuse I get 10 and 1000 as expected.

                          - David.Thompson1 at worldnet.att.ne t

                          Comment

                          • Vladimir S. Oka

                            #14
                            Re: convert decimal number in a hexadecimal number ?

                            Dave Thompson opined:
                            [color=blue]
                            > On Thu, 16 Mar 2006 08:09:44 +0000 (UTC), "Vladimir S. Oka"
                            > <novine@btopenw orld.com> wrote:
                            >[color=green]
                            >> On Thursday 16 March 2006 07:48, CoL opined (in
                            >> <1142495289.587 465.154320@i40g 2000cwc.googleg roups.com>):[/color]
                            >[color=green][color=darkred]
                            >> > char* itoa(int val, int base){[/color]
                            >>[/color]
                            > I would make base unsigned, and probably val also.
                            >[color=green]
                            >> You never check that inputs are valid.
                            >>[/color]
                            > Neither do standard library routines in most cases. There are
                            > arguments on both sides, and (IMO properly) different decisions in
                            > different situations and usages.
                            >[color=green][color=darkred]
                            >> > static char buf[32] = {0};[/color]
                            >>
                            >> Are you sure 31 characters is enough for a 256-bit int on DS9K?
                            >>[/color]
                            > That's a fair point. Although easily fixed.
                            >[color=green][color=darkred]
                            >> > int i = 30;[/color]
                            >>
                            >> a) Why 30?
                            >> b) Isn't it clearer to put initialisation into `for`?
                            >>[color=darkred]
                            >> > for(; val && i ; --i, val /= base)
                            >> >[/color][/color]
                            > Test-at-top means that value 0 is output as no digits (an empty
                            > string). While mathematically defensible, this is not what most
                            > people usually want. Test-at-bottom is one fix; another is an
                            > override (or digits_so_far == 0) which can be extended to the
                            > additional and often useful functionality (or digits_so_far <
                            > min_digits).
                            >[color=green][color=darkred]
                            >> > buf[i] = "0123456789abcd ef"[val % base]; //its a c way
                            >> > to
                            >> > pick up that particular index value that is in [].[/color]
                            >>
                            >> Obfuscated C Contest is at <www.ioccc.org> .
                            >>[/color]
                            > I don't consider that obfuscated. If you don't understand that C
                            > character strings, and particularly literal ones, are actually array
                            > of char, you're in big trouble; and if you do, I think that is
                            > obvious and admirably terse. (val % base) ["digits"] would be
                            > obfuscated.[/color]

                            OK, not exactly obfuscated, but does derail (at least mine) thinking
                            process when reading the code. Also, makes it unnecessarily difficult
                            to modify the function to support base 36 numbers (which I failed to
                            mention originally).
                            [color=blue]
                            > The doubleslash comment doesn't add anything though.
                            >
                            > <snip>[color=green]
                            >> You need '\n' at the end of `printf` for anything to be output. You
                            >> also never output string pointed to by `q`. Do you think it's
                            >> guaranteed that the same memory will be allocated to `buf` on the
                            >> second invocation of `itoa`? If you do, you are mistaken. More
                            >> likely you did not copy and paste your code, but retyped it --
                            >> badly.
                            >>[/color]
                            > Since he declared buf static, it certainly will remain allocated at
                            > the same place throughout the program's entire execution.[/color]

                            I admit I managed not to see `static`. Thanks!
                            [color=blue][color=green]
                            >> Once corrected, when I run it, I get:
                            >>
                            >> 00
                            >> 00
                            >>
                            >> Which is incorrect.
                            >>[/color]
                            > After fixing the buffer reuse I get 10 and 1000 as expected.
                            >
                            > - David.Thompson1 at worldnet.att.ne t[/color]

                            --
                            BR, Vladimir

                            There's a fine line between courage and foolishness.
                            Too bad it's not a fence.

                            Comment

                            Working...