Forums Register Login
hex into byte
I want to use a hex number format as a representation of a byte parameter.

mymethod(byte hexnumber)

calling it


is there an easy way to do this?
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).
great, thanks for that,
do you know the convention for declaring a binary number?
ie mymethod (10101100)
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 ]
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
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

    And you know you can use bit manipulation.
    You can use an int, eg
    00000000000011011101111111100001 and "and" it with another int, eg
    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.

    [ May 02, 2006: Message edited by: Campbell Ritchie ]
    Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards

    All times above are in ranch (not your local) time.
    The current ranch time is
    Oct 23, 2017 04:37:58.