473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to convert binary data into string format in JAVA?

darksteel21
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
11 20959
Dheeraj Joshi
1,123 Expert 1GB
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
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
1,123 Expert 1GB
I can not see the attachment clearly. Can you post the code here?

Regards
Dheeraj Joshi
Feb 7 '11 #4
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
12,516 Expert Mod 8TB
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
oh I see.. I'll try to develop a method that will convert bytes into hex...thanks!
Feb 7 '11 #7
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
Thanks sir, this is a good help!
Feb 8 '11 #9
Hi darksteel21,

Did you got any solution on your query. I am getting same issue like unreadable string.
Aug 17 '15 #10
chaarmann
785 Expert 512MB
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
Sherin
77 64KB
Try This Code
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.nio.channels.*;
  3. import javax.xml.bind.DatatypeConverter;
  4.  
  5. public class EncodeDecode {    
  6.   public static void main(String[] args) throws Exception {
  7.     File file = new File("/bin/ls");
  8.     byte[] bytes = loadFile(file, new ByteArrayOutputStream()).toByteArray();
  9.     String encoded = DatatypeConverter.printBase64Binary(bytes);
  10.     System.out.println(encoded);
  11.     byte[] decoded = DatatypeConverter.parseBase64Binary(encoded);
  12.     // check
  13.     for (int i = 0; i < bytes.length; i++) {
  14.       assert bytes[i] == decoded[i];
  15.     }
  16.   }
  17.  
  18.   private static <T extends OutputStream> T loadFile(File file, T out)
  19.                                                        throws IOException {
  20.     FileChannel in = new FileInputStream(file).getChannel();
  21.     try {
  22.       assert in.size() == in.transferTo(0, in.size(), Channels.newChannel(out));
  23.       return out;
  24.     } finally {
  25.       in.close();
  26.     }
  27.   }
  28. }
  29.  
Jun 12 '20 #12

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

Similar topics

0
by: Dan Stromberg | last post by:
I've written up a page about how to convert native binary data to another platform's native binary data, as I did some fortran data conversions for a client. The programs and documentation are...
27
by: geskerrett | last post by:
I am hoping someone can help me solve a bit of a puzzle. We are working on a data file reader and extraction tool for an old MS-DOS accounting system dating back to the mid 80's. In the data...
10
by: Dan | last post by:
To all the gurus out there. I am writing a tool that receives binary data from a network device. The data arrives in a standard format which the vendor has documented, e.g. byte 0 is the format...
5
by: DBuss | last post by:
I'm trying to convert a binary data feed into something more friendly. Some of the data is Int (and extracts successfully), but some is Double and doesn't. The below line of code gives harsh...
4
by: krupalreddy | last post by:
i have a problem with date i have two dates. one is in Date type and the other one is in String type. i need to compare these two dates. for this i try to convert date which is in String type to...
3
by: Schmacker | last post by:
Hi there, I'm trying to teach myself some things about random file I/O with binary, and have come across a task that I am unsure I know how to approach. Currently I write out a bunch of...
4
by: larry | last post by:
Ok I'm a Python noob, been doing OK so far, working on a data conversion program and want to create some character image files from an 8-bit ROM file. Creating the image I've got down, I open...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.