how to print a byte to the console

R

Roderik

Hi,

I try to print a byte to the console. I like to print it in the
hexadeximal format. like
FF or something

How can I do this with System.out.println?
 
M

Martin Honnen

Roderik wrote:

I try to print a byte to the console. I like to print it in the
hexadeximal format. like
FF or something

How can I do this with System.out.println?

Try
Integer.toString(byteVariable, 16).toUpperCase()
 
R

Roderik

Martin said:
Roderik wrote:




Try
Integer.toString(byteVariable, 16).toUpperCase()

I tried:
System.out.println(Integer.toString(dis.readByte(), 16).toUpperCase());

which results in:
-1
12
-1
34
-1
56
-1
-56
-1
-44
3B
3

-1 should be FF. The only right output values are the last two bytes.

Any idea how I can output the bytes to the console the way a hex editor
would do.
 
M

Michael Borgwardt

Roderik said:
I tried:
System.out.println(Integer.toString(dis.readByte(), 16).toUpperCase());

which results in:
-1 []
-1 should be FF. The only right output values are the last two bytes.

Try instead

System.out.println(Integer.toString(dis.readByte() & 0xff, 16).toUpperCase());
 
R

Roderik

Michael said:
Roderik said:
I tried:
System.out.println(Integer.toString(dis.readByte(), 16).toUpperCase());

which results in:
-1
[]

-1 should be FF. The only right output values are the last two bytes.


Try instead

System.out.println(Integer.toString(dis.readByte() & 0xff,
16).toUpperCase());

Thanks, that works right.
Only, numbers below ten, like hexadecimal 03 are printed as 3 (without a
leading zero). Is there a standard function to change this or should I
make a conditional statement to add this 0 to the console output?
 
M

Michael Borgwardt

Roderik said:
Only, numbers below ten, like hexadecimal 03 are printed as 3 (without a
leading zero). Is there a standard function to change this or should I
make a conditional statement to add this 0 to the console output?

Starting with Java 1.5 there is java.util.Formatter and String.format() that
are basically the analogue of C's printf(). They also do the hex conversion
for you. But if you're using an older Java version, you'll have to do the
zero padding yourself.
 
E

Eric Sosman

Roderik said:
I tried:
System.out.println(Integer.toString(dis.readByte(), 16).toUpperCase());

which results in:
-1
12
-1
34
-1
56
-1
-56
-1
-44
3B
3

-1 should be FF. The only right output values are the last two bytes.

Why should negative one be two hundred fifty-five?
And why do you think a `byte' should be able to hold so
large a value, since its range goes from -128 through +127?
If you ponder this for a minute or two, I think you'll
solve at least part of your problem.

You've provided no means to study your claim that only
the final two values are "right," since you haven't shown a
sample of the input stream's data. Nor have you explained
what "rightness" is; from your -1 vs. FF confusion I'd have
guessed that the second, fourth, and sixth lines would also
be considered "right," but apparently you have some other
criterion in mind.
Any idea how I can output the bytes to the console the way a hex editor
would do.

I *had* an idea, but it wouldn't change the second, fourth,
or sixth lines -- and since these are "wrong," my idea probably
isn't what you're after. Sorry.
 
M

Martin Honnen

Roderik wrote:

System.out.println(Integer.toString(dis.readByte(), 16).toUpperCase());

which results in:
-1
12
-1
34
-1
56
-1
-56
-1
-44
3B
3

-1 should be FF. The only right output values are the last two bytes.

I think it is a sign issue, if you use

byte[] testBytes = new byte[] {
(byte) 1,
(byte) 2,
(byte) 4,
(byte) 8,
(byte) 16,
(byte) 32,
(byte) 64,
(byte) 128,
(byte) 255
};
for (int i = 0; i < testBytes.length; i++) {
System.out.println(Integer.toString(testBytes & 0xFF, 16));
}

then you should get the desired result.
 
J

Jim

Hi,

I try to print a byte to the console. I like to print it in the
hexadeximal format. like
FF or something

How can I do this with System.out.println?

Try the following

System.out.print(Integer.toHexString((i >= 0) ? i : 256 + i));

If you need two digit hex then

StringBuffer s =
new StringBuffer(Integer.toHexString((i >= 0) ? i : 256 + i));

if(s.length() == 1) s.insert(0,'0');


Jim
 
Joined
Nov 6, 2007
Messages
1
Reaction score
0
Eric Sosman said:
Why should negative one be two hundred fifty-five?
And why do you think a `byte' should be able to hold so
large a value, since its range goes from -128 through +127?
If you ponder this for a minute or two, I think you'll
solve at least part of your problem.

You've provided no means to study your claim that only
the final two values are "right," since you haven't shown a
sample of the input stream's data. Nor have you explained
what "rightness" is; from your -1 vs. FF confusion I'd have
guessed that the second, fourth, and sixth lines would also
be considered "right," but apparently you have some other
criterion in mind.

--
(e-mail address removed)

i registered here just to tell you this:

learn to program.

Eric Sosman said:
-1 vs. FF confusion

:hahahaha:

i don't know how old this is, but i really hope you see this.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top