aspose file tools*
The moose likes Java in General and the fly likes ascii to binary conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ascii to binary conversion" Watch "ascii to binary conversion" New topic
Author

ascii to binary conversion

Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
I need in java what's referred to in c as an ascii to binary conversion.
Here is a snipit of c++ code to do it. The thing I'm not sure of is using
char and int interchangeably. Any ideas?


char asciiToBinary( char* asciiCode )
{
char retValue;

retValue = hexConvert( asciiCode[1] ) * 16 + hexConvert( asciiCode[2] );

return( retValue );
}

int hexConvert( char convertChar )
{
int digit;

if(convertChar <= '9')
{
digit = convertChar - '0';
}
else
{
digit = (convertChar - 'A') + 10;
}

return( digit );
}
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Are you supposed to write this in Java? Or do you need help with your C code? If the later, this is the wrong place to ask such a question since this website is primarily devoted to Java programming. However, there are several message boards here that are have non-Java related topics. This just isn't one of them. You may get more help trying one of the other message boards or another website that is devoted to just C programming.

Layne


Java API Documentation
The Java Tutorial
Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
I need a java equivalent.

Thanks,
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Everything you have there will work in Java. You just need to wrap the methods in a class and add appropriate access modifiers to each one.

Layne
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Of course, if this isn't a school assignment, you can do it much easier with Integer.parseInt(). There is a version that takes a String with the character representation of the number and an int for the radix (base) to do the conversion.

HTH

Layne
Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
Not sure I understand. How do convert from char to int as in the following:

char retValue;

retValue = hexConvert( asciiCode[1] ) * 16 + hexConvert( asciiCode[2]


I get an error here.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

the natural way would be to use a byte - wouldn't it?

String->byte easy
byte->hex easy too.



Hm. get the feeling you mean something else.
However

shouldn't this be ...asciiCode[0] and asciiCode[1]?
Your input is already hex - but as String - c4, ff, 17 for example.
And you want to get the decimal value.

as Layne mentioned years before...
[ October 05, 2004: Message edited by: Stefan Wagner ]
[ October 05, 2004: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Barry Brashear:
Not sure I understand. How do convert from char to int as in the following:

char retValue;

retValue = hexConvert( asciiCode[1] ) * 16 + hexConvert( asciiCode[2]


I get an error here.


Is this a homework assignment where you absolutely have to write this method yourself? If not, then you should use Integer.parseInt() as Stefan illustrated above. One advantage of Integer.parseInt() is that it can convert a string of any arbitrary length. ATM, your function can only convert strings with two "digits". It will take a bit more work to modify your code to get the behavior that is already provided by Integer.parseInt().

Unlike C++, Java has many, many pre-written, pre-tested methods that are very, very useful. You should use these wherever possible. If you are interested, you should browse the Java API docs to get a feel for what is available. In fact, learning how to navigate the API docs is a valuable skill.

After examining your code more closely, I have a question: why does asciiToBinary() return a char? Shouldn't this be an int? Perhaps I don't fully understand what you are trying to do in this method. Can you explain in English with some examples of the parameter that is passed in and the expected return value?

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ascii to binary conversion