Solved

Hex String Manipulation in Java

Posted on 1997-04-30
3
1,232 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:sameer043097
  • 2
3 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1219978
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
 
LVL 2

Accepted Solution

by:
Philippe earned 100 total points
ID: 1219979

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
 
LVL 6

Expert Comment

by:jpk041897
ID: 1219980
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now