• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ascii to binary conversion

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 );
}
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need a java equivalent.

Thanks,
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic