|
Bit of an odd one here. I have the following string:
4e554c4c00
Which is a HEX representation of
NULL
I need to convert that intial string into it human readable format, does anybody know of any framework methods/classes to do this? If not can anybody think of an elegant/quick solution?
I was initially thinking of string parsing, however this portion of code will be called 100's of times a second and parsing string is fairly heavy.
-- modified at 9:17 Thursday 13th July, 2006
EDIT :
Here is a chunk of code that works, however I have a few concerns about speed
string raw = "4e554c4c00";
raw = "556e6b6e6f776e204576656e742025545241504944252e00";
string[] rawParts = new string[raw.Length / 2];
int rawPos = 0;
int partsPos = 0;
while(rawPos < raw.Length)
{
rawParts[partsPos] = raw.Substring(rawPos, 2);
partsPos++;
rawPos++;
rawPos++;
}
string humanReadable = string.Empty;
for(int partsLoop=0; partsLoop<rawParts.Length - 1; partsLoop++)
{
char charPart = Convert.ToChar(
Int32.Parse(rawParts[partsLoop],
System.Globalization.NumberStyles.HexNumber));
humanReadable += charPart.ToString();
}
Console.WriteLine(humanReadable);
if the first value of the string "raw" is used "NULL" is output by the final Console.WriteLine. if the second value of the string is used "Unknown Event %TRAPID%." is output.
-- modified at 9:23 Thursday 13th July, 2006
|
|
|
|
|
MrEyes wrote: I need to convert that intial string into it human readable format
What do you mean by human readable? What do you want it to say? Do you just want your hex string replaced with "null"? In that case:
string humanReadable = inputString.Replace("4e554c4c00","NULL");
|
|
|
|
|
Unfortunatly that wont work, 4e554c4c00 is just one example. The application that generates these string could have encoded anything at all so I need to dynamically translate it
|
|
|
|
|
|
As an example:
4e554c4c00
breaks down to (ignoring the 00 termination)
4e
55
4c
4c
Which are the hex values for
N
U
L
L
This is based on standard hex codes (http://www.lookuptables.com/[^])
As mention earlier I was considering using something like Replace() however this code will be called many times and this might be a little too slow
|
|
|
|
|
See this:
byte[] b = new byte[4] {0x4e, 0x55, 0x4c, 0x4c};
string s = System.Text.Encoding.ASCII.GetString(b);
The result is "NULL". First you must change your "4e554c4c" into byte array.
static string HexToString(string input)
{
byte[] b = new byte[input.Length / 2];
for(int i = 0; i < input.Length; i+=2)
b[i / 2] = byte.Parse(input.Substring(i, 2), NumberStyles.HexNumber);
return System.Text.Encoding.ASCII.GetString(b);
}
|
|
|
|
|
stancrm wrote: First you must change your "4e554c4c" into byte array.
And therein lies the issue, I need something quick - if you look at my modified first post I have added the hacky method I am using at the moment.
|
|
|
|
|
See my message again. I have added the function to change 4e554c4c to NULL.
I think that is the fastest way.
|
|
|
|
|
Thats great, considerably better than my loop and loop attempt
I have only made one amendment and that is to remove the trailing terminator:
public static string HexToString(string input)
{
input = input.Substring(0, input.Length - 2);
byte[] b = new byte[input.Length / 2];
for(int i = 0; i < input.Length; i+=2)
{
b[i / 2] = byte.Parse(input.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
}
return System.Text.Encoding.ASCII.GetString(b);
}
|
|
|
|
|
Here's my entry (no need to prepare the data as a byte array):
private static string HexToText(string hex)
{
string result = string.Empty;
for (Int32 i = 0; i < hex.Length; i += 2)
result += (char)byte.Parse(hex.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
return result;
}
Share and enjoy.
Sean
|
|
|
|
|
This is getting shorter and shorter
Anybody got a one liner?
Time to start doing some timings
|
|
|
|
|
Just remember, shorter may not necessarily mean faster. IMHO, you should perf test your alternatives before selecting one.
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
Sean Michael Murphy wrote: (char)byte.Parse(hex.Substring(i, 2), System.Globalization.NumberStyles.HexNumber)
Damn, I didn't know you could do that... I was doing it the hard way
public string DecryptHexString(string text) {
string output = string.Empty;
for (int i = 0; i < text.Length; i += 2) {
int hval = DeHex(text.Substring(i, 2));
int temp = (hval ^ (output.Length + 1) % 256);
output += (char)temp;
}
return output;
}
private int DeHex(string input) {
int val;
int result = 0;
for (int i = 0; i < input.Length; i++) {
string chunk = input.Substring(i, 1).ToUpper();
switch (chunk) {
case "A":
val = 10; break;
case "B":
val = 11; break;
case "C":
val = 12; break;
case "D":
val = 13; break;
case "E":
val = 14; break;
case "F":
val = 15; break;
default:
val = int.Parse(chunk); break;
}
if (i == 0) {
result += val * 16;
} else {
result += val;
}
}
return result;
}
Try code model generation tools at BoneSoft.com.
|
|
|
|
|
Does anyone know how to programmatically delete a previously created custom menu in Excel? I need to delete a menu on multiple machines and would really like to do this though a program. These menus were individually created directly in Excel. Thanks for you help in advance.
Jeremy
|
|
|
|
|
|
Hi,
Just wanted to ask if any body has worked on interactive services using .NET 2.0... MSDN says : "The creation of interactive services is not supported. To workaround this, you can create a non-interactive service and a separate control GUI application that communicates with the service using sockets or remoting.", on ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref11/html/T_System_ServiceProcess_ServiceType.htm.
If some body could help me regarding creation of interactive services, it will be great help...
Thanks in advance.
Regards.
|
|
|
|
|
What it is about "you can't do this" that's hard to understand?? MSDN already described the process I was going to suggest.
You create two applications, one being the service with no UI, and the other being the Windows Forms app that talks to the service app over Sockets or .NET Remoting.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hello!
It seems I can´t use SendMessage with WM_GETTEXT either GetWindowText to retrieve a label´s caption =/
I read about it and most of the people used ReadMemory, but it´s too complicated! and reading the label´s caption is not my main goal.
Does anyone have suggestion?
Thanks!
|
|
|
|
|
Then what IS the main goal??
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
I use a dll to click on popup windows and what I need to do is just read a caption, then I know what is being clicked!
|
|
|
|
|
Hi,
I want to create Custom Control just like MessageBox.
also want to add all the functionality of the MessageBox.
Plz help me out.
It's Urgent.
|
|
|
|
|
Messagebox is not a control , its a dialog window that is invoked by a static method.
you can create something similair by doing:
public static class MyMessageBox
{
public static MyOwnDialogResult Show(string title,string message,.whatever params you want.)
{
MyOwnMessageBoxForm form = new MyOwnMessageBoxForm ();
form.Text = title;
form.lblMessage.Text = message;
... setup icons etc.
form.ShowDialog();
MyOwnDialogResult result = form.GetMyOwnDialogResult;
return result;
}
}
this way you can make your own messagebox return the kind of result you want yourself.
it is also possible by doing a bit of win32 hacking to display the "real" messagebox and alter things in it.
but I dont see much reason to do so, its easier to roll your own and you can easier adapt it to your own needs.
//Roger
http://www.puzzleframework.com
|
|
|
|
|
|
|
How to use Commandwindow, Immediate to interact with code in a project.
Can use it in Runtime?
Jey
|
|
|
|