Re: ASCII Hex string to character conversion



Do a search on google for "Base64.java". You should find plenty of examples
including at least one public domain utility at sourceforge.net. The Base64
scheme can encode/decode anything to ascii characters. Its been around for
years. Its pretty much the standard.

If for some reason that doesn't float your boat, use this as a starting
point. It essentially produces 2 "hex" characters for every byte so its
extremely inefficient. Base64 is much more efficient. I had to remove some
company specific stuff so no gaurantees that this compiles/works.

public static String encodeHexString(String sourceText) {

Byte[] rawData = sourceText.getBytes();
StringBuffer hexText= new StringBuffer();
String initialHex = null;
int initHexLength=0;

for(int i=0; i<rawData.length; i++) {
int positiveValue = rawData[i] & 0x000000FF;
initialHex = Integer.toHexString(positiveValue);
initHexLength=initialHex.length();
while(initHexLength++ < 2) {
hexText.append("0");
}
hexText.append(initialHex);
}
return hexText.toString();
}
public static String decodeHexString(String hexText) {

String decodedText=null;
String chunk=null;

if(hexText!=null && hexText.length()>0) {
int numBytes = hexTextlength()/2;

byte[] rawToByte = new byte[numBytes];
int offset=0;
int bCounter=0;
for(int i =0; i <numBytes; i++) {
chunk = hexText.substring(offset,offset+2);
offset+=2;
rawToByte[i] = (byte) (Integer.parseInt(chunk,16) & 0x000000FF);
}
decodedText= new String(rawToByte);
}
return decodedText;
}




.



Relevant Pages

  • =?iso-8859-1?Q?Re:_Ordner_f=FCr_Fonts?=
    ... public static string GetFolderPath ... StringBuilder lpszPath = new StringBuilder; ... int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath); ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: How to convert string "AD-A6-0D-1F" to byte[]?
    ... or you can try Int32.Parse(string, NumberStyles.HexNumber), then convert ... int to byte. ... Here's some code I wrote a while ago to parse hex ... > public static string ParseHex ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help converting Vb.Net to C#
    ... public static string BadlyCrypt ... int b = text; ... > Public Function SimpleCrypt(ByVal Text As String) As String ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • RE: Controling Modal Dialogs (Solution)
    ... doesn't return until the 'modal' browser returns. ... string varOptions) ... public void DocumentComplete ... int rc = winDisp.Invoke(rgDispId, ref guid, 0, ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)