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

1
  • 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 Commented Jan 12, 2016 at 9:43

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

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
@RachitaNanda 179 is not pipe it 179 B3 U+2502 │ box drawings light vertical. Source : stackoverflow.com/a/22274036/2254048
Sorry for the confusion , I mean to use 179(box drawing light vertical )please check my edit 2 .
@RachitaNanda I have updated the answer. Please check it now.
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
|
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 :)

1 Comment

the command documentation states that the |(ascii 179) should be represented in ascii format in the command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.