0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

java > String > "AAA" + 0x0A > "AAA<LF>"ではなく、"AAA10"になります / 演算子"+"使用時にはまりそうな点

Last updated at Posted at 2016-08-06
動作環境
Processing 3.1.1 on Windows 8.1 pro(64bit)
ControlP5 v2.2.6

状況

http://qiita.com/7of9/items/1edf5a109eea9b711058
を実装中にはまった点を整理。

以下のjavaプログラムで0x0Aが<LF>として扱われなかったために、通信先が<LF>を拾えなかった。

txt = tx + 0x0A;
myPort.write(txt);

確認

ideoneで確認した。

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String str1 = "AAA" + "\n";
		System.out.print(str1);
		
		String str2 = "BBB" + 0x0A;
		System.out.print(str2);
		
		System.out.println("END");
	}
}
結果
Success	time: 0.1 memory: 320512 signal:0
AAA
BBB10END

String方に対して+ 0x0Aは10という数値を文字列に変換した上でString文字列に足されるようだ。

文字列連結演算子 +が働いているのでふるまいとしては正しいのだろう。

<LF>を足したい場合は+ "\n"にしないといけない。

演算子"+"使用時にはまりそうな点

@shiracamus さんのコメントを参照ください。

0
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?