3 Replies - 2169 Views - Last Post: 11 November 2009 - 07:00 PM Rate Topic: -----

#1 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 282
  • Joined: 22-July 09

Get readable text from hex values

Post icon  Posted 11 November 2009 - 10:48 AM

I am tryign to read a file which is full of hex values. I know I need to go to uint but am not sure how to pass it the file and output readable text to say a text box. Does anyone have any link so tutorials or a MSDN page that I could read? I tried to Google search but got nothing or at least nthing which did a good job explaining what I need to do.
Is This A Good Question/Topic? 0
  • +

Replies To: Get readable text from hex values

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Get readable text from hex values

Posted 11 November 2009 - 01:30 PM

What's an example of the hex value? What if the hex-encoded data contains unprintable characters?
Was This Post Helpful? 0
  • +
  • -

#3 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 282
  • Joined: 22-July 09

Re: Get readable text from hex values

Posted 11 November 2009 - 06:12 PM

View PostJackOfAllTrades, on 11 Nov, 2009 - 12:30 PM, said:

What's an example of the hex value? What if the hex-encoded data contains unprintable characters?

I don't have an example of the hex string as I don't currently have a hex editor installed. I think all characters would be printable though as a different application can get certain information from it.

When I use this code I get an error:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			StreamReader Reader = new StreamReader("save.sav");
			string cContent = "";

			while (Reader.ReadLine() != null)
			{
				string rContent = Reader.ReadLine();
				cContent += System.Convert.ToChar(System.Convert.ToUInt32(rContent.Substring(0, 2), 16)).ToString();

				rContent = rContent.Substring(2, rContent.Length - 2);
			}

			Console.WriteLine(cContent);


			Reader.Close();
			Console.ReadLine();
		}
	}
}


The line with += says there are no recognizable digits.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Get readable text from hex values

Posted 11 November 2009 - 07:00 PM

So you want to convert a hexadecimal string into a readable character string. First you convert it into an array of bytes. Each two digits represents a byte value:

string inputValue = "6100620063006400";
// Create the array, which will be half the length of the string:
int len = inputValue.Length/2;
byte[] outputValue = new byte[len];

for (int i =0; i < len; ++i)
{
	// Get two digits
	var d = inputValue.Substring(i * 2, 2);
	// Convert to a byte and add to the array
	outputValue[i] = byte.Parse(d, System.Globalization.NumberStyles.AllowHexSpecifier);
}

Console.WriteLine("Data: {0}", System.Text.Encoding.UTF8.GetString(outputValue));


I think that should do it, but it's sort of off the cuff.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1