1 11 package org.eclipse.debug.internal.ui.views.memory; 12 13 import java.math.BigInteger ; 14 import org.eclipse.debug.internal.core.memory.MemoryByte; 15 16 17 22 public class HexRenderer extends AbstractMemoryRenderer implements IFixedLengthOutputRenderer { 23 24 27 public String getString( 28 String dataType, 29 BigInteger address, 30 MemoryByte[] data, String paddedStr) { 31 32 StringBuffer strBuffer = new StringBuffer (); 33 34 for (int i=0; i<data.length; i++) 35 { 36 if ((data[i].flags & MemoryByte.VALID) != 0) 37 { 38 strBuffer.append(new String (convertByteToCharArray(data[i].value))); 39 } 40 else 41 { 42 strBuffer.append(paddedStr); 44 } 45 } 46 47 return strBuffer.toString().toUpperCase(); 48 49 } 50 51 54 public byte[] getBytes( 55 String dataType, 56 BigInteger address, 57 MemoryByte[] currentValues, String data) { 58 59 byte[] bytes = convertHexStringToByteArray(data); 60 61 return bytes; 62 } 63 64 67 public int getNumCharPerByte() { 68 return 2; 69 } 70 71 76 static public String convertByteArrayToHexString(byte[] byteArray) 77 { 78 StringBuffer strBuffer = new StringBuffer (); 79 char charArray[]; 80 81 for (int i=0; i<byteArray.length;i ++) 82 { 83 charArray = convertByteToCharArray(byteArray[i]); 84 strBuffer.append(charArray); 85 } 86 87 return strBuffer.toString(); 88 } 89 90 static private char[] convertByteToCharArray(byte aByte) 91 { 92 char charArray[] = new char[2]; 93 int val = aByte; 94 if (val<0) val += 256; 95 charArray[0] = Character.forDigit(val/16, 16); 96 charArray[1] = Character.forDigit(val%16, 16); 97 98 return charArray; 99 } 100 101 107 public byte[] convertHexStringToByteArray(String str) throws NumberFormatException 108 { 109 if (str.length() < getNumCharPerByte()) 110 return null; 111 112 byte[] bytes = new byte[str.length()/getNumCharPerByte()]; 113 114 for (int i=0; i<bytes.length; i++) 116 { 117 String oneByte = str.substring(i*2, i*2+2); 119 120 Integer number = Integer.valueOf(oneByte, 16); 121 if (number.compareTo(Integer.valueOf(Byte.toString(Byte.MAX_VALUE))) > 0) 122 { 123 int temp = number.intValue(); 124 temp = temp - 256; 125 126 String tempStr = Integer.toString(temp); 127 128 Byte myByte = Byte.valueOf(tempStr); 129 bytes[i] = myByte.byteValue(); 130 } 131 else 132 { 133 Byte myByte = Byte.valueOf(oneByte, 16); 134 bytes[i] = myByte.byteValue(); 135 } 136 } 137 138 return bytes; 139 } 140 141 } 142 | Popular Tags |