programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Liutauras Vilda
• Paul Clapham
Sheriffs:
• paul wheaton
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Piet Souris
Bartenders:
• Mike London

# hex into byte

Ranch Hand
Posts: 243
• Number of slices to send:
Optional 'thank-you' note:
I want to use a hex number format as a representation of a byte parameter.

mymethod(byte hexnumber)

calling it

mymethod(ff)

is there an easy way to do this?

Java Cowboy
Posts: 16084
88
• Number of slices to send:
Optional 'thank-you' note:
You can use the "0x" prefix to specify a hexadecimal number literal. For example:

mymethod((byte) 0xff);

Note that you need to cast it to byte to do this because 0xff does not normally fit into a byte (byte values go from -128 to 127, and 0xff = 255 doesn't fit).

Nikos Stavros
Ranch Hand
Posts: 243
• Number of slices to send:
Optional 'thank-you' note:
great, thanks for that,
do you know the convention for declaring a binary number?

Nikos Stavros
Ranch Hand
Posts: 243
• Number of slices to send:
Optional 'thank-you' note:
ie mymethod (10101100)

Jesper de Jong
Java Cowboy
Posts: 16084
88
• Number of slices to send:
Optional 'thank-you' note:
There is no way to do this for binary numbers just like hexadecimal numbers. There is for octal numbers (base 8): preceed the number by a zero.

So for example 065 is an octal number, with the decimal value 6 x 8 + 5 = 53.

Why hexadecimal and octal but not binary? I don't know, but the hex and octal notations are one of those things that Java inherited from the C programming language.

IMO they should have left out the octal notation, nobody ever uses it (even in C or C++ I have never seen a program where this was used seriously).
[ April 28, 2006: Message edited by: Jesper Young ]

Nikos Stavros
Ranch Hand
Posts: 243
• Number of slices to send:
Optional 'thank-you' note:
hmm that could be a bit of a pain, I'm setting the outputs of a pic chip, I suppose i'll just have to workout the bit pattern using hex notation

thatnks again Jesper

Marshal
Posts: 77212
370
• Number of slices to send:
Optional 'thank-you' note:
Why leave out the octal notation?
Even if a few people use it, you still need it in the language. And when Java came out ten years ago there were probably more people who used octal than there are nowadays. You can't remove support for their legacy programming just because octal is hardly used any more.

Anyway there are at least two easy ways to convert binary.
1: Use a sort of lookup table,
• 0000=0
• 0001=1
• 0010=2
• . . .
• 1001=9
• 1010=A
• 1011=B
• . . .
• 1111=F
• 2: Use a parser, eg

CR

Campbell Ritchie
Marshal
Posts: 77212
370
• Number of slices to send:
Optional 'thank-you' note:
And you know you can use bit manipulation.
You can use an int, eg
00000000000011011101111111100001 and "and" it with another int, eg
10000000000000000000000000000000
That present example will give 0 for all positive numbers, and Integer.MIN_VALUE for any negative numbers, because you will only get the most significant bit (farthest left) coming through if it is "1" in both numbers.
In code that would come out as

If you use & 4, you will get 4 if your number has a "1" in its no 2 bit (ie 3rd from the right) (which is equivalent to myFirstInt % 4 > 3), and 0 if that bit it "0" (or myfirstInt % 4 <= 3).
That is how those numbers work where you use 1 2 4 8 etc. Go to the API specification, look up the Font class, find the fields BOLD or ITALIC, and look up their Constant values. You will find values of 1 2 4 etc. So you can actually add up those values to get something like 3 = bold and italic.

There is a bit in the Java tutorial about bitwise operators. That might be what you are looking for.

I have got to go now; I hope i have explained it all so you can understand it.

CR
[ May 02, 2006: Message edited by: Campbell Ritchie ]

 Don't get me started about those stupid light bulbs.