Hex String Manipulation in Java

I have this problem:
I wish to take a string from the user. It represents the Pysical Address in Hex (say Mac address).
To pass this on to an SNMP agent, I need to send it as type STRING of 6 OCTETS. So if the user inputs an address in hex as: "aa bb cc dd ee ff", How do I
pack this correctly into 6 OCTETS correctly so as to be interpreted as a PhyAddress by the SNMP agent?
PS: I am using JDK 1.0.2 and have been unable to find a suitable method to do this...
Thanks!
Sameer
sameer@dnrc.bell-labs.com
sameer043097Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jpk041897Commented:
I don't quite understand your question, which 6 octets are you refering to, the PDU's maybe? And exactly what kind of physical address output are you looking for?

Some cooments:

MAC addresses are used only by ARP and RARP and are defined as NIC interfaces only. Whats more, MAC addresses can and usualy are of diferent lengths depending on what type of Ethernet NIC you are using (see RFC 826 An Ethernet Address Resolution Protocol). Your 96 bit example is used only by 10Mbit Ethernet cards.

What's more, MAC addresses will only allow you to locate IP address on boxes that actualy have an Ethernet adapter installed and on the same subnet. (This excludes all of Token-ring, SNA, and modems).

RARP (RFC 904), which is the other MAC associated protocol is used only to determine a NIC's own IP address on diskless work stations.

On the other hand, SNMP uses IP addresses (either in dot notation or full 64 bit address).

Partial answer:

If you are allready aware of all these limitations, then for as much of yuor question as I can interpret, here is a partial answer:

Java handles hex values via excape sequences as character literals in the form \xdd.

This implies you can parse your string into to hex character pairs and stor the values in integer variables with a code structure like:

String MAC;
int res[6];

res[0].ParseInt(MAC.substring(0,1);
res[1].ParseInt(MAC.substring(2,3);
...

To convert back to hex or oct "string values" you will have to define a method. Since the code fragment above packs the values into neat byte size bundles, converting to octal or hex should not be a problem using the modulus operator (%). For example, to convert back to hex you can use:

String res;

hi = (int) intval/16;
lo=intval % 16;

switch (hi){
   case 15:
      res = "f";
      break;
   case 14:
      res = "e";
      break;
...
   case 10:
       res= "a";
       break;
   default:
     String Tmp;
     Tmp.valueOf(hi);
      res = Tmp.toString();
}
switch (lo){
  case 15:
      res = res + "f";
      break;
   case 14:
      res = res + "e";
      break;
...
   case 10:
       res= res + "a";
       break;
   default:
     String Tmp;
     Tmp.valueOf(hi);
      res = res + Tmp.toString();
}  

Once res contains the hex pair, you can return it to the calling method and build a result string with that.

For octal values the solution is similar, just use division and modulo 8, and generate 3 values instead of two.

I hope I understood your question properly and gave you a coheerent suggestion


0
PhilippeCommented:

Hi Sameer,

JDK 1.1 has a Byte wrapper with a method called decode which should do what you want. In the meantime you have to hand-code it eg. like this:

public class hex {
  static int i;
  public static void main(String argv[]) throws java.io.IOException{
    byte[] a=new byte[12], hex=new byte[6];

    System.out.println("enter six bytes as 12 hex digits: ");
    System.in.read(a);

    for (i=0 ; i<a.length; i++) {
      // upper_case:
      if (a[i] > 'F')  a[i] = (byte)(a[i]-'a'+'A');
      // hex_digit:
      if (a[i] >= '9') a[i] = (byte)(a[i]-'A'+10);
                  else a[i] = (byte)(a[i]-'0');
      // hex byte
      if ((i % 2) == 1)      hex[i/2] = (byte)(a[i-1]*16 + a[i]);
    }

    System.out.println("here are your bytes in ascii: >"+
                   (char)hex[0]+
                   (char)hex[1]+
                   (char)hex[2]+
                   (char)hex[3]+
                   (char)hex[4]+
                   (char)hex[5]+ "<");
  }

}


Here is a test run:

darkstar/java: java hex
enter six bytes in 12 hex digits:
48454c4C4F20
here are your bytes in ascii: >HELLO <

  hope this helps,

    Philippe

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jpk041897Commented:
Not to compete with philippe's answer, and it doesn't address your question directly; but I just found a comertial SNMP class library you might find interesting at:


http://www.adventnet.com/docs.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.