The moose likes Sockets and Internet Protocols and the fly likes Sending header info in hex format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Sending header info in hex format" Watch "Sending header info in hex format" New topic

Sending header info in hex format

Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
I'm supposed to send a header in hex format. I was given the following:

The header needs to be in HEX.

The "Chr" represent's a decimal value.

HEADER = Chr(85) & Chr(170) & Chr(0) & Chr(255)

If I prepend the hex value to my socket message, the hex value is FF00AA55.
Since hex is not a supported type in java, how should this be transmitted to their server using the Printwriter?

Thanks in advance.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I would say hex is nowhere a type.
It's a notation-convention.
0xFF00AA55 is a 4-byte Hexvalue, and might be 4 bytes, a 4-byte Number, a 4-byte Color (transparency, RGB) or an header.
I'm not used to Printwriter - looks a bit curious to me in this context.

And the javadocs show me that I'm right:
It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.


could work.

Are you bound to Printwriter, or may you use another kind of Stream?

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
See if Integer.toHexString() does what you want. Maybe on a char by char basis.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Warren Dew
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
If you use Java Strings, be sure the other end is expecting unicode rather than ASCII.

One question - when you say "header is supposed to be in hex", do you actually mean it's supposed to be characters representing the number in hexadecimal notation, or is what's really meant that the header is supposed to be in raw binary? If the latter, you'll probably need to convert to bytes and send that.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

if no appropriate Stream for bytes can be used.
Of course you would allways need 4 chars to send.

What is your primary input - int, char, byte, string?

[ August 27, 2004: Message edited by: Stefan Wagner ]
[ August 27, 2004: Message edited by: Stefan Wagner ]
Consider Paul's rocket mass heater.
subject: Sending header info in hex format
Similar Threads
Writing Hex Values to a file
Need help for convert Hex Unicode to HTML codes?
Packed decimal conversion - Java
how to print ASCII value of a number ?
Program seems to hang; I can't find the bug