How to convert integer to hexadecimal in Java ?

Discussion in 'Java' started by tobleron, Oct 20, 2008.

  1. tobleron

    tobleron Guest

    Hi, how to convert integer into hexadecimal in Java ? For example :

    int dec = 12;

    I want to make it become 0x000c. I don't want to use
    Integer.toHexString(n) because it will return the result as string. I
    need the hex as integer because i need to write :

    file.writeShort(0x000c);

    How to do it ? Please help.
    tobleron, Oct 20, 2008
    #1
    1. Advertising

  2. tobleron schrieb:
    > Hi, how to convert integer into hexadecimal in Java ? For example :
    >
    > int dec = 12;
    >
    > I want to make it become 0x000c. I don't want to use
    > Integer.toHexString(n) because it will return the result as string. I
    > need the hex as integer because i need to write :
    >
    > file.writeShort(0x000c);


    file.writeShort(12);

    >
    > How to do it ? Please help.
    Stefan Rybacki, Oct 20, 2008
    #2
    1. Advertising

  3. tobleron

    tobleron Guest

    On Oct 20, 3:29 pm, Stefan Rybacki <> wrote:
    > tobleron schrieb:
    >
    > > Hi, how to convert integer into hexadecimal in Java ? For example :

    >
    > > int dec = 12;

    >
    > > I want to make it become 0x000c. I don't want to use
    > > Integer.toHexString(n) because it will return the result as string. I
    > > need the hex as integer because i need to write :

    >
    > > file.writeShort(0x000c);

    >
    > file.writeShort(12);
    >
    >
    >
    > > How to do it ? Please help.

    >
    >


    Thank you. You're right. I was thought that I should convert it first
    into hex. In fact, I can directly write file.writeShort(12) or
    file.writeShort(dec) to get hex value written in the file. Thank you
    Stefan.
    tobleron, Oct 20, 2008
    #3
  4. tobleron

    Roedy Green Guest

    On Mon, 20 Oct 2008 00:27:18 -0700 (PDT), tobleron <>
    wrote, quoted or indirectly quoted someone who said :

    >Hi, how to convert integer into hexadecimal in Java ? For example :
    >
    >int dec = 12;
    >
    >I want to make it become 0x000c. I don't want to use
    >Integer.toHexString(n) because it will return the result as string. I
    >need the hex as integer because i need to write :
    >
    >file.writeShort(0x000c);


    Internally you have either binary or String. To talk about whether
    something is decimal or octal or hex only make sense when talking
    about strings.

    You are confused because Java converts decimal and hex literals from a
    string of chars to binary without any great ceremony.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The Canadian national animal should be changed from the beaver to the ostrich.
    Canadians elected a party that denies global warming so they too could pretend it presents no danger.
    Roedy Green, Oct 20, 2008
    #4
  5. Lew <> wrote:
    > tobleron wrote:
    >>>> Hi, how to convert integer into hexadecimal in Java ? For example :
    >>>> int dec = 12;
    >>>> I want to make it become 0x000c. I don't want to use
    >>>> Integer.toHexString(n) because it will return the result as string.


    > Of course, there is no such thing as "hex" vs. "decimal" for int types. That
    > only applies to String representations.


    Even more confusing is, when "hex" is actually misused as the word
    for "binary". :) I wonder where this confusion originally came from.

    Perhaps even "binary" is still confusing, as it could mean to write
    out eight ascii chars "00001100".

    But then I don't know any unambiguous term, myself, for
    writing ... well, what? chars?... to a file.
    Andreas Leitgeb, Oct 20, 2008
    #5
  6. tobleron

    Arne Vajhøj Guest

    Andreas Leitgeb wrote:
    > Lew <> wrote:
    >> tobleron wrote:
    >>>>> Hi, how to convert integer into hexadecimal in Java ? For example :
    >>>>> int dec = 12;
    >>>>> I want to make it become 0x000c. I don't want to use
    >>>>> Integer.toHexString(n) because it will return the result as string.

    >
    >> Of course, there is no such thing as "hex" vs. "decimal" for int types. That
    >> only applies to String representations.

    >
    > Even more confusing is, when "hex" is actually misused as the word
    > for "binary". :) I wonder where this confusion originally came from.
    >
    > Perhaps even "binary" is still confusing, as it could mean to write
    > out eight ascii chars "00001100".


    I believe:
    * binary
    * text radix 10
    * text radix 16
    * text radix 2
    would be the correct terms to use.

    Arne
    Arne Vajhøj, Oct 26, 2008
    #6
  7. tobleron

    Mark Space Guest

    Sabine Dinis Blochberger wrote:

    > Here's how I keep sane. Every data is a sequence of zeroes and ones, and
    > everything else is merely a display format, if you will. Integer is base
    > 10, Hex is base 16 and so on.


    Integer is not base 10. Integer is binary. Base 10 is only for
    display. "println" is base 10, if you will.

    Arne says "text" in another post but I think "display" might be a better
    term. Hmm, well there's strings stored internally too. "String
    representation?"
    Mark Space, Oct 26, 2008
    #7
  8. Mark Space wrote:
    > Arne says "text" in another post but I think "display" might be a better
    > term.


    That seems a bit too inspired by a certain programming language to me.

    Arne
    Arne Vajhøj, Oct 26, 2008
    #8
  9. Lew wrote:
    > Arne Vajhøj wrote:
    >> Mark Space wrote:
    >>> Arne says "text" in another post but I think "display" might be a
    >>> better term.

    >>
    >> That seems a bit too inspired by a certain programming language to me.

    >
    > Silly prejudice. Java is rife with terms that are similar to other
    > programming languages' but differ somewhat in semantics.
    >
    > If you couldn't reuse terminology from other programming environments
    > that would be very difficult. For that matter, if you couldn't use
    > words that have conventional natural-language meanings, especially based
    > on a prejudice about an unrelated environment, you'd truly be too
    > limited to function. Er, method.


    I think there are languages closer to Java than Cobol.

    Arne
    Arne Vajhøj, Oct 27, 2008
    #9
  10. On Sun, 26 Oct 2008 22:42:42 -0400, Arne Vajhøj wrote:

    > I think there are languages closer to Java than Cobol.
    >

    I'm sure you're right, but COBOL's "display numeric" term is quite a
    useful one for distinguishing stored numeric values from their human
    readable representations.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Oct 27, 2008
    #10
    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. Derk Gwen
    Replies:
    3
    Views:
    1,299
    Zoran Cutura
    Jun 26, 2003
  2. Martin Ambuhl
    Replies:
    1
    Views:
    817
    Dan Pop
    Jun 26, 2003
  3. bd
    Replies:
    0
    Views:
    670
  4. les ander
    Replies:
    4
    Views:
    861
    wes weston
    Oct 5, 2004
  5. Replies:
    15
    Views:
    11,920
    Eric Sosman
    Jun 23, 2006
Loading...

Share This Page