Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hexadecimal Encoding  RSS feed

 
Simon Ritchie
Ranch Hand
Posts: 105
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

As part of a university project I'm working on, I have to send a series of numbers encoded hexadecimally across a socket (from a client application to a server application) using DataInputStream/DataOutputStream.  My code is working fine in that data is transmitting over the socket but I don't understand how to encode hex numbers so that they are received as hex and not decimal.  For example, I'm trying to send a byte with a hex value 0x10 (decimal 16) across the socket.  When the server receives it and displays what it has received it shows a value of 16 not 0x10.  Is this expected behaviour?  How come a hex number was transformed to decimal in the transmission?
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of numbers are these? Are they common or garden ints? They are neither encoded in hex nor in decimal, but in binary (two's complement). The standard method for displaying numbers,
System.out.println(i);
displays them in decimal. Now, if somebody is telling you to transmit numbers so they go in hex and come out hex, they are having you on because all those numbers are encoded in binary. If you try displaying them like this:-
System.out.printf("0x%08x%n", i);
you will see them appear in hex expanded to eight digits complete with a 0x. I cannot remember how that technique displays negative numbers.
 
Stephan van Hulst
Saloon Keeper
Posts: 7476
134
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that encoding is not a property of numbers (in this case, bytes) but a property of strings that represent numbers. If you just want to send a number, you should just send the number as an int, or something like that. If it has to be a hexadecimal string, transform the number to a byte array first, and then encode it using a Charset. You're in luck, I just wrote one a few months ago. You can find it here: https://coderanch.com/t/675982/java/AES-CBC-Encryption#3166684

If you have problems, you should show us some of your code so we can help you debug.
 
Simon Ritchie
Ranch Hand
Posts: 105
4
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are common ints, yes.

Campbell Ritchie wrote:They are neither encoded in hex nor in decimal, but in binary (two's complement). The standard method for displaying numbers,
System.out.println(i);
displays them in decimal.


And now I feel quite stupid for not realising that!

Thanks to you both for clearing that up.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!