Solved

Hex String Manipulation in Java

Posted on 1997-04-30
3
1,263 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL  on Tomcat 8 46
Java exception bubble up 2 18
spring jars download 1 27
Unhandled exception type Exception 18 31
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

777 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