Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

how to read and show a byte array?

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I have a file following:


I want to read it and show in the console, so i have made following.
Here is Message bean class to store data.


And here is main class to Test.


Here is result in the console of eclipse.


9:10:05 Long Message [B@19821f
9:13:58 Short Message [B@601bb1


I have got frmTime (9:10:05) and typeMsg (Long Message)but i dont know how to get the byte array (data) ?

Suggset me please!!!
Thanks and best regards.
[ November 07, 2007: Message edited by: Bear Bibeault ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing that strings like

030000002f130200000000000800450000800000000001119f7a0a000000e0e02f13c5dac5da006c0000000000000303000000000

are in hexadecimal (since I see digits and letters in the range a=f, but nothing else). Are you familiar with hexadecimal? Each character 0-9 or a-F corresponds to a 4 bits, so every two characters correspond to a single byte (8 bits). You will need to write some methods to read and write byte arrays in hexadecimal here. There are some existing methods that may help you, but you'll need to do some looping.

To read, try something like this:

You may find the Integer.parseInt() method useful here. Be sure to look a the overloaded versions of this method.

And to write:

If you're using JDK 5 or later, you can accomplish this simply using String.format() or System.out.format(). If not, you may need to do somethign like this:

If you can't use String.format(), this will require a little bit of simple math. Remember that every character has a numeric value - you can see a table here.
 
Tran Tuan Hung
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Yingst , thanks you very much,
But to be honest, i understand the aglothirm you suggest me but i am still not yet how to got it exactly in my code. I still code incorrect you can correct me by the way you can give me an example or a snippe code?
I am very sorry for my stupid.
thanks and best regards,
Hung
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hung
It should work by changing your code like this:
while ((line = in.readLine()) != null) {String[] info = line.split(";");msg.setFrmTime(info[0]);msg.setTypeMsg(info[1]);
msg.setData(info[1].getBytes()); <-----------------
System.out.println(msg.toString());
}
But it seem like your data contains a bunch of PACK fields. To understand your data is another story.
Quang
 
Tran Tuan Hung
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Quang, i also edit the code following:

but it throw an exception follow:

java.lang.ArrayIndexOutOfBoundsException: 2
at test.TestMsg.readFileMsg(TestMsg.java:18)
at test.TestMsg.main(TestMsg.java:31)


I searched and think to slove this problem, but i still not yet modyfiy my code. With Jim Yingst's solution i still try to code but it still thrown exception and i can not ...
[ November 07, 2007: Message edited by: Tran Tuan Hung ]
 
Quang Pham
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hung
You can only store 1 frmTime, 1 typeMsg and 1 byte[] data per instance Message. I've shorten your data and modified the source code as follow and it works.
import java.io.BufferedReader;
import java.io.FileReader;
public class TestMessage {
private String filePath;
public void readFileMsg(Message msg) {
String line = null;
try {
String[] info = {"9:10:05","Long Message","030000002f130200000000000800450000800000000001119f7a0a000000e0e02f13c5dac5da006c0000000000000303000000000 00400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000"};
msg.setFrmTime(info[0]);
msg.setTypeMsg(info[1]);
msg.setData(info[2].getBytes());
System.out.println(msg.getFrmTime());
System.out.println(msg.getTypeMsg());
byte b [] = msg.getData();
for (int i=0;i< b.length;i++){
char c = (char)b[i];
System.out.print(c);
}
} catch (Exception e) { // TODO: handle exception//
e.printStackTrace();
}
}
public static void main(String[] args) {
Message msg = new Message();
TestMessage testMsg = new TestMessage();
testMsg.readFileMsg(msg);
}
}
 
Tran Tuan Hung
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to get data from the text file, but BTW i also sloved my problem.
Thanks for helping me Jim Yingst and Quang.
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic