0

I am actually reading int values from the java socket bufferedread and later converting into hex representation. I am using a method as below.

StringBuilder sb = new StringBuilder();
sb.append(Integer.toHexString(nextChar));

if (sb.length() < 2) {
    sb.insert(0, '0'); //pad with leading zero if needed
}

String hexChar = sb.toString();
System.out.println("\n\n hex value is "+hexChar +"   "+"Int value is:"+nextChar);

The codes are working fine. Only I got one or two data which in normal case after conversion into hex is either BE or A9 but I am getting ffdd and the integer value is 65533. Could it be my conversion method is wrong or is it input value is it self is having that value?

TimeToCode
  • 901
  • 2
  • 16
  • 34
user5313398
  • 713
  • 3
  • 9
  • 28
  • Sorry its int nextChar=0; and then I run this while ((nextChar=r.read()) != -1) – user5313398 Sep 08 '16 at 18:31
  • The settings for r is r = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream())); It return integer cause I capture it to nextChar ? – user5313398 Sep 08 '16 at 18:37
  • could this be related to the fact that Java does not use unsigned int? So if you have the value of 169 (0xA9) and it is stored in variable of type byte, then that is not 169 but rather -87. If you promote it to an int, it will still be -87. Which when you convert to hex will be some thing like FFFFFFA9. Could you please post the output and show me which one is wrong? – Teto Sep 08 '16 at 18:38
  • @Teto here is one example hex value is FFFD Int value is:65533 – user5313398 Sep 08 '16 at 18:39
  • @user5313398 What are the inputs that are giving you the wrong result? Can you clarify what inputs are giving you BE and A9. – Michael Markidis Sep 08 '16 at 18:40
  • Isn't 0xFFFD = 65533d ? What value were you expecting for that? – Teto Sep 08 '16 at 18:44
  • @MichaelMarkidis I can determine that is the issue I am getting it as ffdd and the integer value is 65533. For example I have this hex value is 2D Int value is:45 this is working fine as it supposed to be – user5313398 Sep 08 '16 at 18:44
  • Sounds like an encoding/decoding problem; `'\ufffd'` is what you get when the received bytes do not represent a correctly encoded character. Are you obtaining `nextChar` from an InputStream, or a Reader? – VGR Sep 08 '16 at 18:49
  • @VGR this how I set my reader r = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream())); I am not too sure on this encoded? – user5313398 Sep 08 '16 at 18:50
  • @VGR what encoding should I use then? – user5313398 Sep 08 '16 at 19:03
  • You will want to set the encoding (charset) to match whatever encoding the server uses to encode its data. – VGR Sep 08 '16 at 19:07
  • I am not too sure on this I am running on centos 6.8? What could be the best charset for this server or this any specific settings? – user5313398 Sep 08 '16 at 19:10
  • Does the charaset depends on the incoming input or depends on the server? – user5313398 Sep 08 '16 at 19:17

1 Answers1

1

It's not really clear what you're trying to do, but as defined in the documentation the return from read is: The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached.

This is of course because you are using a text reader.

You might want to read this answer.

OK, Got it.

You thought you are reading bytes, but since you are using BufferedReader you actually read chars (The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).)

I believe that this is what you wanted to achieve. Please note this is not the best way, its just a sample!

InputStream is = sock.getInputStream()

BufferedInputStream bis = new BufferedInputStream(is);

int nextByte;
StringBuilder sb = new StringBuilder();

while( (nextByte = bis.read()) != -1 ) {
    sb.append(String.format("%02X ", nextByte));
}
Community
  • 1
  • 1
Gal Nitzan
  • 391
  • 2
  • 12
  • what I am trying to do is read byte by bytes from r = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream())); where I am doing in this while ((nextChar=r.read()) != -1) . So if you see I have implemented the -1 to check end of stream. So I am reading the in integer form and there after I convert it into hex. All my other bytes are are fine when I conversion only these specific ones are giving me ffdd is where I am lost? – user5313398 Sep 08 '16 at 19:34
  • All my other values are fine and the conversion is perfect as per requirement only these specific bytes – user5313398 Sep 08 '16 at 19:37
  • I think is mostly probably due to character set what is best to use or try all? – user5313398 Sep 09 '16 at 09:46