1st off, I'm new to JAVA/NETBEANS, and my experience lies mainly in C and the Sparc
assembly language. The above problem is one I've solved in C (input a string of hex
characters and then convert it to a binary array for the purposes of bit manipulation,
namely for debug tools such as ecc generation). I'd like to put a gui on this code and
thought java/netbeans might be the simplest way to go about it. In the process I hope
to learn just enough Java to be dangerous and get away from tedious cli's. I already
designed the gui using Netbeans. (That was simple part.)

Problem in a nutshell:
Perhaps someone has solved this problem before...I want to enter a 16 character hexadecimal string into a jTextField. I then want to convert that string into a 64 bit array
of 1's and 0's.

Once I have that, the rest is merely doing a lot (and I do mean a lot of xor's on
particular bits in the array and outputting the bits.

I would appreciate any help or even a pointer in the right direction because I'm
relatively clueless here.

Thanks in advance,
thorneko

The above problem is one I've solved in C ...

This isn't pretty, but this works in C...(and works for any string length of hex)

/* Function to input the hexadecimal data */

indata1(prompt,repet,a0)
char *prompt,a0[];
int repet;
{
int i;
printf("%s",prompt);
for(i=0;i<repet;++i)
a0=getchar();
a0[repet]='\0';
}

/* Function to convert hexadecimal to binary */

bindata(repet1,a1,a2)
char a1[],a2[];
int repet1;
{
int i;
for(i=0;i<repet1;++i)
{
if (a1=='0'){
a2[4*i]='0';
a2[4*i+1]='0';
a2[4*i+2]='0';
a2[4*i+3]='0';
} else if (a1=='1') {
a2[4*i]='0';
a2[4*i+1]='0';
a2[4*i+2]='0';
a2[4*i+3]='1';
} else if (a1=='2') {
a2[4*i]='0';
a2[4*i+1]='0';
a2[4*i+2]='1';
a2[4*i+3]='0';
} else if (a1=='3') {
a2[4*i]='0';
a2[4*i+1]='0';
a2[4*i+2]='1';
a2[4*i+3]='1';
} else if (a1=='4') {
a2[4*i]='0';
a2[4*i+1]='1';
a2[4*i+2]='0';
a2[4*i+3]='0';
} else if (a1=='5') {
a2[4*i]='0';
a2[4*i+1]='1';
a2[4*i+2]='0';
a2[4*i+3]='1';
} else if (a1=='6') {
a2[4*i]='0';
a2[4*i+1]='1';
a2[4*i+2]='1';
a2[4*i+3]='0';
} else if (a1=='7') {
a2[4*i]='0';
a2[4*i+1]='1';
a2[4*i+2]='1';
a2[4*i+3]='1';
} else if (a1=='8') {
a2[4*i]='1';
a2[4*i+1]='0';
a2[4*i+2]='0';
a2[4*i+3]='0';
} else if (a1=='9') {
a2[4*i]='1';
a2[4*i+1]='0';
a2[4*i+2]='0';
a2[4*i+3]='1';
} else if (a1=='a' || a1=='A') {
a2[4*i]='1';
a2[4*i+1]='0';
a2[4*i+2]='1';
a2[4*i+3]='0';
} else if (a1=='b' || a1=='B') {
a2[4*i]='1';
a2[4*i+1]='0';
a2[4*i+2]='1';
a2[4*i+3]='1';
} else if (a1=='c' || a1=='C') {
a2[4*i]='1';
a2[4*i+1]='1';
a2[4*i+2]='0';
a2[4*i+3]='0';
} else if (a1=='d' || a1=='D') {
a2[4*i]='1';
a2[4*i+1]='1';
a2[4*i+2]='0';
a2[4*i+3]='1';
} else if (a1=='e' || a1=='E') {
a2[4*i]='1';
a2[4*i+1]='1';
a2[4*i+2]='1';
a2[4*i+3]='0';
} else if (a1=='f' || a1=='F') {
a2[4*i]='1';
a2[4*i+1]='1';
a2[4*i+2]='1';
a2[4*i+3]='1';
}
}
a2[4*repet1]='\0';
}

So again, thanks,
thorneko

Take a look at some of the methods on the Long and BigInteger classes.

I reversed the above as well. (In C)
It allows you to go from an array of binary
digits and creates a hex string output. It is
a great deal more complex and also provides the ability to select bits out of the
array that are in a row and print out the
hex value. I can post that code if anyone is
interested.
Thanks,
thorneko

Yes, that C code is definitely not pretty!

Seems to me that if you want to store the hex number as a String containing characters '0' through '9' and 'a' - 'f' and the corrsponding binary number as a String containing characters '0' and '1' (that appears to be what you were doing in the C code), you could set up two functions:

String toHex(String binNumber)

and then the reverse function

String toBinary(String hexNumber)

You could change your C code into Java code to fill in the function code. It's the same logic, though Java has more conversion functions than C does that you can take advantage of. But I think you have way too many if statements, whether you are doing it in C or Java. A few lines of code could get rid of all of those if statements in either language. As to how to get the Strings into and out of the JTextFields, just use getText and setText.

What part exactly are you having problems with?

Yes, that C code is definitely not pretty!

Seems to me that if you want to store the hex number as a String containing characters '0' through '9' and 'a' - 'f' and the corrsponding binary number as a String containing characters '0' and '1' (that appears to be what you were doing in the C code), you could set up two functions:
just use getText and setText.

This is close: 1) using getText (thanks, this was the starting point, and I'm slogging
through the programming manuals) I get the hex numbers as a string. I then turn it
into a character array containing characters '0' through '9' and 'a' - 'f'. (done!)
2) Since I need to perform a large number of Boolean operations, I take the hex character
array, create a new integer array (digits 0 and 1 only) that is 4x as long as the hex character array. (not done!!!)

In other words if the first character in the hex array is "A", then the 1st 4 integers in the integer array are 1010. Once I have the integer array I'm golden...

Longer example: text entered... 52af (becomes string of hex)
This string becomes a character array.
The character array then becomes an integer array consisting only of 1's and 0's
(0101001010101111)

Just an observation:
I believe this is the age old conversion problem in all languages since no one believed
we ever needed to enter hexadecimal integers as data...Therefore hex has to come
inputted as either a text string or a character array. Personally I blame K&R for not
setting a standard for this in C. All languages that followed would most likely have
just followed suit. Seeing as no matter how complex code gets, we still are flipping
1's and 0's inside the processor.

thanks,
thorneko

This is close: 1) using getText (thanks, this was the starting point, and I'm slogging
through the programming manuals) I get the hex numbers as a string. I then turn it
into a character array containing characters '0' through '9' and 'a' - 'f'. (done!)
2) Since I need to perform a large number of Boolean operations, I take the hex character
array, create a new integer array (digits 0 and 1 only) that is 4x as long as the hex character array. (not done!!!)

In other words if the first character in the hex array is "A", then the 1st 4 integers in the integer array are 1010. Once I have the integer array I'm golden...

Longer example: text entered... 52af (becomes string of hex)
This string becomes a character array.
The character array then becomes an integer array consisting only of 1's and 0's
(0101001010101111)

Just an observation:
I believe this is the age old conversion problem in all languages since no one believed
we ever needed to enter hexadecimal integers as data...Therefore hex has to come
inputted as either a text string or a character array. Personally I blame K&R for not
setting a standard for this in C. All languages that followed would most likely have
just followed suit. Seeing as no matter how complex code gets, we still are flipping
1's and 0's inside the processor.

thanks,
thorneko

Is there a reason you are converting the String to a character array? Why not just leave it as a String? You mentioned that you haven't created a new integer array. You didn't mention why. Are you stuck on how to declare it?

int[] intArray = new int[50];

Thanks and I've made great progress so far...

String myhex = jTextField1.getText();
    int len = myhex.length();
    int[] binarray = new int[len*4];
        for (int i = 0; i < len; i++) {
           if (myhex.charAt(i) == '0') {
              binarray[4*i]=0;
              binarray[4*i+1]=0;
              binarray[4*i+2]=0;
              binarray[4*i+3]=0;
           } 
           else if

           //rest deleted for brevity

Really just 1 thing left, converting my
int binarray of o's and 1's back to a string
and output it to a JTextField.

I want the output to be exactly what is
in the integer array called binarray...

binarray[0]=0
binarray[1]=1
binarray[2]=1
binarray[3]=0

So, 0110 goes into the jTextField as 0110

So it's a 2 part question...

Convert an integer array of 0's and 1's
to output exactly as it is in the array to
a JTextField.

I appreciate all help so far, but no one
gives just the simple answer...Each individual always has to have some wacky
formatting or other weirdness. I just want
the easiest form.

Thanks again,
Thorneko

Thanks and I've made great progress so far...

String myhex = jTextField1.getText();
    int len = myhex.length();
    int[] binarray = new int[len*4];
        for (int i = 0; i < len; i++) {
           if (myhex.charAt(i) == '0') {
              binarray[4*i]=0;
              binarray[4*i+1]=0;
              binarray[4*i+2]=0;
              binarray[4*i+3]=0;
           } 
           else if

           //rest deleted for brevity

Really just 1 thing left, converting my
int binarray of o's and 1's back to a string
and output it to a JTextField.

I want the output to be exactly what is
in the integer array called binarray...

binarray[0]=0
binarray[1]=1
binarray[2]=1
binarray[3]=0

So, 0110 goes into the jTextField as 0110

So it's a 2 part question...

Convert an integer array of 0's and 1's
to output exactly as it is in the array to
a JTextField.

I appreciate all help so far, but no one
gives just the simple answer...Each individual always has to have some wacky
formatting or other weirdness. I just want
the easiest form.

Thanks again,
Thorneko

Not sure what you mean or who you're talking about on this one.

I appreciate all help so far, but no one
gives just the simple answer...Each individual always has to have some wacky
formatting or other weirdness. I just want
the easiest form.

Only Ezzaral and I have commented on this thread and we haven't touched formatting or done anything "wacky" or "weird" in my opinion. You've changed from converting from a character array to a character array in your C code to now going from a String/character array to an int array and back. That's fine, but it's going to take a few posts back and forth to figure out exactly what you want. Also, we're not supposed to give you the exact answer right away on this forum, even though sometimes that'd be faster. It's one of the forum rules. However, since you've already written your function and it works, here's an easier, straightforward way to convert from your String to your integer array. You could actually reduce it in length further by looping through the four digits, but I didn't:

public int[] ConvertHexToBin(String hexNum)
    {
         int[] binNum = new int[4 * hexNum.length()];
         for (int i = 0; i < hexNum.length(); i++)
         {
             char aChar = hexNum.charAt(i);
             int theDigit = Character.digit(aChar, 16);
             binNum[4 * i] = theDigit / 8;
             theDigit = theDigit % 8;
             binNum[4 * i + 1] = theDigit / 4;
             theDigit = theDigit % 4;
             binNum[4 * i + 2] = theDigit / 2;
             theDigit = theDigit % 2;
             binNum[4 * i + 3] = theDigit;
         }
         
         return binNum;
    }

The parseInt function from theInteger class can come in handy too, but with the length of your hex string, it could overflow. I didn't check out Ezzaral's links. There might be an equivalent function there. I don't know.

So you want to do the reverse when you convert from the integer array to the String:

String ConvertBinToHex(int[] binNum, int numBinDigits)

You're going to create an empty String. Each hex digit is four binary digits. Handle it one hex digit at a time and add it to your string when you've calculated the equivalent character hex digit. The Integer class contains some good conversion functions that may help you. In particular check out toHexString:

http://java.sun.com/j2se/1.3/docs/api/java/lang/Integer.html#toHexString(int)

I went back and looked at my original code in C and saw that I went from a char "hex"array (16 characters) to a char "bin" array (64 chars) but I noticed I left out another routine that went from the char "bin" array to an int "bin" array. Looking through the Java api's, I realized I could skip that intermediate step in JAVA. When I wrote the original C program I only printed out the full 64 bit binary as a check, so I am trying the same thing here. In the JAVA gui program (w/Swing) I am calculating the ecc sec/ded value for the 64 bit int string and will only be outputting that. In my JAVA program I need to handle cases for 12 different arrays in a microprocessor design. Sometimes the ecc is 7 bits, sometimes 8. I am writing this as mainly as a debug tool (L2 data cache failures are prevalent in any microprocessor but use ecc correction to prevent single bit errors) for when the chip
design becomes actual silicon.

Thanks again,
thorneko

Pardon my comment about "wacky/weird"
formatting, but I've been all over the internet
on many different sites looking for answers.
Some of the questions I've seen asked in
regards to various aspects of int array to string conversions just seem a bit strange. It would be nice to see answers
built up slowly, from the simplest form,
progressing to the more complex. All I see
are complex answers to complex questions,
and this is a relatively simple application.

Thanks for the pointer...I'm off hunting some more. In the meantime I've fleshed out 11 of the 12 arrays the code
needs to cover for the ecc calculations....

I started this project this past Thursday, downloading Netbeans, designed the gui
in about an hour, and the rest has just been
the nuts and bolts of the underlying code.
Not too bad, and with a little luck, I'll have this wrapped up and ready for distribution by late Monday.

Two thumbs up for netbeans and gui design. A thumb down for the Java API
documentation. It's like reading a foreign
language presently.

Actually, I just saw that the links I posted were to my local copy of the API docs, but anyway, the child classes of Number all have functions for converting between other representations and working with the numbers as bits.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.