English 中文(简体)
• 如何有效地将编外阵列转换成护卫
原标题:How to efficiently convert byte array to string
  • 时间:2010-12-29 11:00:18
  •  标签:
  • java
  • byte

我有151个tes子,通常是一个记录,记录需要插入分析仪数据库。 在151个阵列中,从0到1个是创纪录的,2至3个是参考,4至9个是日值。 下面的星体数据为日期值。 i 想将其改为扼制

byte[] b= {48,48,49,48,48,52};  // when converted to string it becomes 10042. 

new String(b);  // current approach

有效转换一些范围(Arrays.copyOfRange(b,0,5))的外围阵列以加以扼制。

最佳回答
new String(b, 0 ,5);

See API doc, for more information.

问题回答

使用<代码>String(bytes[] bytes, intlack, int length)的构造者: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#String(byte[], int, int

new String(b, 0, 5);

如果你们需要在记录中为每个区域作出说明,我就建议采取分明的办法:

byte[] wholeRecord = {0,1,2 .. all record goes here .. 151}
String wholeString = new String(wholeRecord);
String id = wholeString.substring(0,1);
String refId = wholeString.substring(1,3);
...

实际抵消可能因地制宜而有所不同。

这种做法的好处是,只有一刀切的阵列。 之后打电话到<代码>(>substring(>)将不会产生复制件,而只是将第一份复印件与被抵消。 因此,你可以节省一些记忆和阵列复制时间。

这里的任何答复都不认为你可能不使用ASCII。 在将 by改成str时,你总是应考虑char。

new String(bytes, offset, length, charset);

并在此充满热情(效率不高):

    byte[] b = { 48, 48, 49, 48, 48, 52 };
    ByteArrayInputStream bais = new ByteArrayInputStream(b);

    BufferedReader buf = new BufferedReader(new InputStreamReader(bais));

    String s = buf.readLine();
    System.out.println(s);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签