Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Error converting and recording Hex file format to Ascii

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

I have a Java program that has the following function: - Receive a string of 800 characters in Hexa format. After that, convert this string in Ascii format. Finally, write this string to a file in a determined directory with the extension ".rec".

However, the program is recording with an unknown number of characters. It is not recording the file with the content that should.

Below are the following: How should be the file being recorded. The format that is currently being recorded. And finally, the Java code where the program do the converting and recording the file.

What am I doing wrong?

Thanks,

Fernando

Below how should be the file being recorded

.ß÷Q¬˜À’Z!pÞ'Z1ÅF-ù>ÁÀ†çâwq
f@Ç’É8û½+ê‘w‰j/,÷DS¿Ê0Áô©p)A3òèËN½+ýy®¼·r0Zk ¨žE4CDMUÏøz߯`‰÷ý1làuÌ

Below the format in which it is being recorded

ß÷Q¬˜À’Z!pÞ'Z1ÅF-ù>ÁÀ†çâwq
f@ǒÉ8û½+ê‘w‰j/,÷DS¿Ê0Áô©p)A3òèËN½+ýy®¼·r0Zk ¨žE4CDMUÏøz߯`‰÷ý1làuÌ

Below the Java code where I do the converting and recording the file

 
Saloon Keeper
Posts: 8943
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"09dff751ac98c0925a2170de275a31c5462df93e08c1c086e7e..."
Looking at the above string as pairs of hex digits, any pair (e.g. "df") that has the high bit set is not ASCII.
Don't know what you expect to happen with"...".
 
Fernando Skackauskas
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sir Carey Brown,

The situation is as follows:

I need to convert the String "dado" from Hex to Ascii for execute a biometric identification.

I already have this ready routine developed in JavaScript. The program correctly converts the characters and I can make biometric identification successfully.

Below the code in JavaScript and output writes to the file biometria.rec


biometria.rec


.ß÷Q¬˜À’Z!pÞ'Z1ÅF-ù>ÁÀ†çâwq
f@Ç’É8û½+ê‘w‰j/,÷DS¿Ê0Áô©p)A3òèËN½+ýy®¼·r0Zk ¨žE4CDMUÏøz߯`‰÷ý1làuÌ

What I can not understand is the question:

Why the JavaScript can convert, for example, the Hex character "df" as referring to "ß" and Java can not?

What am I doing wrong?

Thanks,

Fernando.
 
Ranch Hand
Posts: 121
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Fernando

Fernando Skackauskas wrote:
Why the JavaScript can convert, for example, the Hex character "df" as referring to "ß" and Java can not?



Why do you think than Java can't do that? It can and actually do. But file does not contain characters, it contains bytes. And there are many ways to convert the same character into different set of bytes (those conversions are called "encodings"). That conversion happens in the PrintWriter. As you does not pass an explicit encoding to use, some "default platform encoding" is used to convert characters into bytes. Then your editor reads those bytes and uses a different encoding to convert them back to characters.

Looks like you should not use string manipulations at all. Your file is not text file, it is binary file. So you have to use binary output streams (like FileOutputStream) and just write decoded bytes (not characters).
 
Fernando Skackauskas
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maxim Karvonen,

Thank you for explanation.

But I do not know exactly where I should pass an explicit encoding to use and write decoded bytes (not characters).

Where must I change the code for it record the right format? I did not find an example in web.



Thanks
 
Carey Brown
Saloon Keeper
Posts: 8943
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try

This specifies the character encoding that you want to take place.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic