When monitoring data sent to device i get this:
http://i.stack.imgur.com/u0kIT.png
but i expect one character: F0 ð
public void WritingDataToPort() {
SerialPort port = new SerialPort("COM26");
try {
System.out.println(port.openPort());
port.setParams(9600, 8, 1, 0);
port.writeString((char)240+""));
port.closePort();
} catch (SerialPortException ex) {
System.out.println(ex);
}
}
I completly don't know how to send this "ð" characater. Tried all ascii codes.
You can't just send the char (or UTF8) through the serial port. You have to convert it.
First get the length of the char in UTF-8, then get the bytes and send the bytes.
Getting the size of UTF-8: Getting the actual length of a UTF-8 encoded std::string?
Then send it one by one. On the other side you need something to assemble them back.
You could also consider using a better alternative to writing bytes directly to the port, such as http://code.google.com/p/java-simple-serial-connector/
You could try unicode. UTF-8 code.
http://www.fileformat.info/info/unicode/char/f0/index.htm
C/C++/Java source code "\u00F0"
Related
I am trying to send encrypted data between a client and server. Due to the RSA encryption its in Byte array form. This means I have had to change the way I send data. I curently cant get it working, Ill leave my method (sendMessage) below which is what handles the sending of the message, If anyone could tell me what I am doing wrong that would be great :)
public void sendMessage(byte[] msg){
if(msg.equals("null")){
}
else{
try{
ByteArrayOutputStream f = new ByteArrayOutputStream(CSocket.getOutputStream());
out = new PrintWriter(f);
out.write(msg);
countSend++;
}catch (IOException e){
System.out.println("ERROR");
}
}
}
Sorry should have clarified, essentially CSocket is a socket I have opend and I want to send msg through the socket. The issue I have specifically with this code is It says that: OutputStream can not be converted to int on the line where I creat the ByteArrayOutputStream object f and No suitable method found for write(byte[]) on the line out.write(msg);
Thanks for the clarification! Please see https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#write(byte%5B%5D,int,int)
The write method in ByteArrayOutputStream for byte[] needs two more arguments. Something like the following might work:
out.write(msg, 0, msg.length);
Please let me know if this is useful.
I think I fixed my issue now. Its probibably not the most efficient way of doing it but essentially I encode the byte array in a format that means I wont loose any data. This means I send it in this encoded format and then on the receving end I just simply decode it. Works so much better with print writer doing it this way.
OutputStream f = CSocket.getOutputStream();
out = new PrintWriter(f);
String encodedmsg = new String(msg, "ISO-8859-1"); // ISO-8859-1 is supposed to give every character a unique representation so I shouldent loose any data during encoding and decoding
out.write(encodedmsg);
My assignment is to create a program that does compression using the Huffman algorithm. My program must be able to compress any type of file. Hence why i'm not using the Reader that works with characters.
Im not understanding how to be able to make some kind of frequency table when encoding a binary file?
EDIT!! Problem solved.
public static void main(String args[]){
try{
FileInputStream in = new FileInputStream("./src/hello.jpg");
int currentByte;
while((currentByte = in.read())!=-1){ //in.read()
//read all byte streams in file and create a frequency
//table
}
}catch (IOException e){
e.printStackTrace();
}
}
I'm not sure what you mean by "reading from an image and look at the characters" but talking about text files (as you're reading one in in your code example) this is most of the time working by casting the read byte to char by doing a
char charVal = (char) currentByte;
It's mostly working because most data is ASCII and most charsets contain ASCII. It gets more complicated with non-ASCII characters because a simple cast is equivalent with using charset ISO-8859-1. This will still most of the time produce correct results, because e.g. Window's cp1252 (on german systems) only differ with ISO-8859-1 at the Euro-sign.
Things start to run havoc with charsets like UTF-8 where non-ASCII characters are encoded with multiple bytes, so you will see things like ä instead of an ä. Same for files being encoded with Unicode where every second byte is most likely a binary zero.
You could use Files.readAllBytes and then iterate over this array.
Path path = Paths.get("hello.txt");
try {
byte[] array = Files.readAllBytes(path);
} catch (IOException ) {
}
I have the following code snippet for a Java server that can't be changed:
....
while(true){
try {
System.out.println("Will listen . . .");
instruction = this.dis.readUTF(); // Socket's dataInputStream
System.out.println("Got instruction: " + instruction);
} catch (IOException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
}
....
} // end while(true)
And I have the following client code for python:
....
self.socket.send(b"GET~CARD\n")
print self.socket.recv(1024)
....
The problem that I'm facing is that I can get the client to send information to the server but the server won't stop listening, so it stays on the blocking call this.dis.readUTF();.
As you can see I tried using the \n character at the end of the string but it stays listening. Does anybody knows how to write from a python client to a java server using readUTF()?
Have a look at the docs for the readUTF function here
Primarily, this stands out
First, two bytes are read and used to construct an unsigned 16-bit
integer in exactly the manner of the readUnsignedShort method . This
integer value is called the UTF length and specifies the number of
additional bytes to be read. These bytes are then converted to
characters by considering them in groups. The length of each group is
computed from the value of the first byte of the group. The byte
following a group, if any, is the first byte of the next group.
Try something like this in your python code
import struct
message = u"GET~CARD\n"
size = len(message)
...
sock.send(struct.pack("!H", size))
sock.send(message)
I have a trouble to convert email attachment(simple text file in windows-1251 encoding with latin and cyrillic symbols) to String. I.e I have a problem with converting cyrillic.
I got attachment file as base64 encoded String like this:
Base64Encoded email Attachment
Original file
So when I try to decode it, I got "?" instead of Cyrillic symbols.
How can I get right Cyrillic(Russian) symbols instead of "?"
I've already tried this code with all encodings, but nothing help to get correct Russian symbols.
BASE64Decoder dec = new BASE64Decoder();
for (String key : Charset.availableCharsets().keySet()) {
System.out.println("K=" + key + " Value:" +
Charset.availableCharsets().get(key));
try {
System.out.println(new String(dec.decodeBuffer(encoded), key));
} catch (Exception e) {
continue;
}
}
Thank You beforehand.
I am not very familiar with BPEL and protocols it uses. If you communicate between nodes using some binary protocols, then you must 1) ensure, client and receiver use the same charset and 2) convert java string into proper bytes in this encoding. Java stores string internally in UTF-16 format. So when you execute String correct = new String(commonName.getBytes("ISO-8859-1"), "ISO-8859-5") you will get correct string in UTF-16. Then you need to export it to bytes in requested encoding, eg. byte[] buff = correct.getBytes("UTF-8") assuming the encoding you use between nodes is UTF-8. If happen the encoding is different, then you must make sure, it actually supports Cyrillic characters (e.g. ISO-8859-1 does not support it).
If you use XML for data exchange, make sure it uses suitable encoding in <?xml encoding="UTF-8"?>. You don't need then to play with bytes, you just need to correctly "import" the string (see correct variable). Writing to XML converts characters automatically, but it (encoding) must support characters you want to write. So if you set encoding="ISO-88591", then you will get those question marks again.
I am using Jsch to connect sshd server and I am new in this area. I successfully send non-control character such as, a, b ...so-on. However, I need to send "Esc" key command (Control Character) to perform specific task. According to the link, I tried several ways (by passing "27", "0033", "0x1b", and "^[") but did not work.
I have found a link to use sshj. Is there way to send Control Character using Jsch?
Thanks M.F.H
How about following snippet?
byte[] esc = { (byte)0x1b };
out.write(esc);
out.flush();
JCTerm, which is using JSch, has following definitions,
byte[] ENTER = {(byte)0x0d};
byte[] UP = {(byte)0x1b, (byte)0x4f, (byte)0x41};
byte[] DOWN = {(byte)0x1b, (byte)0x4f, (byte)0x42};
byte[] RIGHT = {(byte)0x1b, (byte)0x4f, (byte)0x43};
byte[] LEFT = {(byte)0x1b, (byte)0x4f, (byte)0x44};
....
Thanks. I have figured out this issue a little different way. I am sending character to VT100 terminal using JSCH to perform specific tasks. In the code, all inputs are converted to array of byte but the ASCII control character cannot be sent as a array of byte to VT100 terminal. According to post, I have to send control character as ASCII code (INT). Such as, If VT100 terminal needs ESC command then 27 (INT) must be written in the SSH Session input stream. ESC's ASCII code is 27. (Before, I sent 27 as string and converted to array of byte.)