How to convert integer to hexadecimal in Java ?

T

tobleron

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.
 
S

Stefan Rybacki

tobleron 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);
file.writeShort(12);


How to do it ? Please help.
 
T

tobleron

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.
 
R

Roedy Green

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.
 
A

Andreas Leitgeb

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.
 
A

Arne Vajhøj

Andreas said:
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
 
M

Mark Space

Sabine said:
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?"
 
A

Arne Vajhøj

Mark said:
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
 
A

Arne Vajhøj

Lew said:
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
 
M

Martin Gregorie

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.
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top