473,327 Members | 2,007 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,327 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 20929
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.