By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

How to convert binary data into string format in JAVA?

darksteel21
P: 36
Hi All,

I am currently developing an application that reads data from PC's Memory. It is working but the output is something like this:

(Please see attached..)



the output is unreadable, it contains a squares,slashes,sharp signs,letters, numbers and etc.

what format is this?is this a binary format?
How could i print it in a readable or string format?

Please help,
Thanks in advance,
MARK TAN
Feb 7 '11 #1
Share this Question
Share on Google+
10 Replies


Dheeraj Joshi
Expert 100+
P: 1,123
Are they in ASCII format?
You can convert the byte array into String in Java.
Check the various string constructors in this link.

This may help you.

Regards
Dheeraj Joshi
Feb 7 '11 #2

darksteel21
P: 36
Hi Dheeraj Joshi,

I have no idea what are their format,how could i know it?
by the way, i'll gonna check the link you gave..

Thanks a lot.

Mark Tan
Feb 7 '11 #3

Dheeraj Joshi
Expert 100+
P: 1,123
I can not see the attachment clearly. Can you post the code here?

Regards
Dheeraj Joshi
Feb 7 '11 #4

darksteel21
P: 36
here is the output :
run:
Process Handler of Yahoo! Messenger : 66892
Process ID of Yahoo! Messenger : 4252
hProcess : native@0x278
The operation completed successfully.
DATA : �����

HTML can not display it properly on browsers..

and here is the codes that i used for getting data from memory:

public String getMemoryData(int ProcessId, int bufferSize, int offset, Pointer hProcess){
IntByReference baseAddress = new IntByReference();
baseAddress.setValue(offset);
Memory outputBuffer = new Memory(bufferSize);
boolean success = kernel32.ReadProcessMemory(hProcess, offset, outputBuffer, bufferSize, null);
byte[] bufferBytes = outputBuffer.getByteArray(0, bufferSize);
return new String(bufferBytes);
}
Feb 7 '11 #5

Rabbit
Expert Mod 10K+
P: 12,366
All data is binary. ASCII is just a way of interpreting 8 bits (technically 7) of data at a time. ASCII also contains unprintable characters. Hence the weird characters you're seeing. It's because it's applying ASCII encoding and the data just so happens to map to unprintable characters in the ASCII set.

A common method of representing data is to convert all the bytes into hex representation. This doesn't tell you what that data is but you can make some educated guesses. For example, if it's 64 bits long, it could be a double or an 8 character ASCII encoded string or a 2-8 character UTF-8 encoded string.
Feb 7 '11 #6

darksteel21
P: 36
oh I see.. I'll try to develop a method that will convert bytes into hex...thanks!
Feb 7 '11 #7

P: 2
Something like this should do the trick for you buddy...

Expand|Select|Wrap|Line Numbers
  1. public class BytesToHex {
  2.  
  3.     private byte[] inbytes;
  4.     private String hexString="";
  5.  
  6.     public BytesToHex(byte[] inbytes){
  7.         this.inbytes = inbytes;
  8.     }
  9.  
  10.     public String toHex() {
  11.  
  12.         String subString;
  13.         int i;
  14.         for (byte b: inbytes){
  15.             subString ="";
  16.             i = (int)b & 0xff ;
  17.             subString = Integer.toHexString(i);
  18.             if (subString.length()==1){
  19.                 subString = "0" +subString;
  20.             }
  21.             this.hexString += subString + " ";
  22.         }
  23.  
  24.         return hexString;
  25.     }
  26.  
  27. }
Feb 8 '11 #8

darksteel21
P: 36
Thanks sir, this is a good help!
Feb 8 '11 #9

P: 1
Hi darksteel21,

Did you got any solution on your query. I am getting same issue like unreadable string.
Aug 17 '15 #10

Expert 100+
P: 785
Instead of converting the bytes to hexadecimal yourself, you can just use:
Expand|Select|Wrap|Line Numbers
  1. System.out.printf("%02X", byte);
or in older Java versions:
Expand|Select|Wrap|Line Numbers
  1. System.out.print(String.format("%02X", byte))
Aug 17 '15 #11

Post your reply

Sign in to post your reply or Sign up for a free account.