1

I'm trying to use extended ascii character 179(looks like pipe).

Here is how I use it.

String cmd = "";
        char pipe = (char) 179;
//      cmd ="02|CO|0|101|03|0F""
         cmd ="02"+pipe+"CO"+pipe+"0"+pipe+"101"+pipe+"03"+pipe+"0F";
        System.out.println("cmd "+cmd);

Output

cmd 02³CO³0³101³03³0F

But the output is like this . I have read that extended ascii characters are not displayed correctly.

Is my code correct and just the ascii is not correctly displayed or my code is wrong.

I'm not concerned about showing this string to user I need to send it to server.

EDIT The vendor's api document states that we need to use ascii 179 (looks like pipe) . The server side code needs 179(part of extended ascii) as pipe/vertical line so I cannot use 124(pipe)

EDIT 2

Here is the table for extended ascii

enter image description here

On the other hand this table shows that ascii 179 is "3" . Why are there different interpretation of the same and which one should I consider??

EDIT 3 My default charset value is (is this related to my problem?)

System.out.println("Default Charset=" + Charset.defaultCharset());
Default Charset=windows-1252

Thanks!

I have referred to

How to convert a char to a String?

How to print the extended ASCII code in java from integer value Thanks

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
  • There is no ascii 179, if your server states this then its documentation is incorrect, there is one in extended ascii and unicode, java uses unicode for its characters so you need to fill in the value of the unicode one – Ferrybig Jan 12 '16 at 09:43

2 Answers2

3

Use the below code.

String cmd = "";
char pipe = '\u2502';
cmd ="02"+pipe+"CO"+pipe+"0"+pipe+"101"+pipe+"03"+pipe+"0F";
System.out.println("cmd "+cmd);
System.out.println("int value: " + (int)pipe);

Output:

cmd 02│CO│0│101│03│0F
int value: 9474

I am using IntelliJ. This is the output I am getting.

enter image description here

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
  • The vendor's api document states that we need to use ascii 179 (looks like pipe) . The server side code needs 179 as pipe so I cannot use 124 – Rachita Nanda Jan 12 '16 at 09:41
  • @RachitaNanda 179 is not pipe it `179 B3 U+2502 │ box drawings light vertical`. Source : http://stackoverflow.com/a/22274036/2254048 – YoungHobbit Jan 12 '16 at 09:44
  • Sorry for the confusion , I mean to use 179(box drawing light vertical )please check my edit 2 . – Rachita Nanda Jan 12 '16 at 09:50
  • @RachitaNanda I have updated the answer. Please check it now. – YoungHobbit Jan 12 '16 at 10:06
  • the output for this is 02?CO?0?101?03?0F . I think java is unable to display extend ascii , should I consider that I'm sending the correct │ box drawings light vertical value in command to server – Rachita Nanda Jan 12 '16 at 10:13
  • please check edit 3 my Default Charset=windows-1252. Can this cause the variation? – Rachita Nanda Jan 12 '16 at 10:25
1

Your code is correct; concatenating String values and char values does what one expects. It's the value of 179 that is wrong. You can google "unicode 179", and you'll find "Unicode Character 'SUPERSCRIPT THREE' (U+00B3)", as one might expect. And, you could simply say "char pipe = '|';" instead of using an integer. Or even better: String pipe = "|"; which also allows you the flexibility to use more than one character :)


In response to the new edits...

May I suggest that you fix this rather low-level problem not at the Java String level, but instead replace the byte encoding this character before sending the bytes to the server?

E.g. something like this (untested)

byte[] bytes = cmd.getBytes(); // all ascii, so this should be safe.
for (int i = 0; i < bytes.length; i++) {
   if (bytes[i] == '|') {
      bytes[i] = (byte)179;
   }
}

// send command bytes to server
// don't forget endline bytes/chars or whatever the protocol might require. good luck :)
Jonas N
  • 1,757
  • 2
  • 21
  • 41