473,394 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Sure a simple parsing question (hex conversion)

I have a file, containing hex values for dates (MMDDYYYY)<status
code><??such as:

30353031323030310200303530323230303102003035303332 30303102003035303432303031020030353037323030310200 30353038323030310200

breaking that down:

30353031323030310200
30353032323030310200
30353033323030310200
30353034323030310200...

How do I break it down, convert and, say, throw it into an Array/
HashTable/etc.?? Never messed w/ HEX (binary??) files before.

Appreciate any and all help.

David D.
Oct 30 '08 #1
6 3363
Hi David,

Below is sample code I wrote how to interpret the data blocks. You should
implement appropriate error handling when parsing non-integer strings.

string content =
"3035303132303031020030353032323030310200303530333 23030310200303530343230303102003035303732303031020 030353038323030310200";

// TODO: Implement proper exception handling
// Constants based on the block format
const int datePartLength = 8;
const int otherPartLength = 1;
const int blockLength = (datePartLength + otherPartLength * 2) * 2;

for (int i = 0; i < content.Length; i += blockLength)
{
// Each block is extracted and converted here
string blockRaw = content.Substring(i, blockLength);
string blockReadable = string.Empty;

for (int j = 0; j < blockRaw.Length; j += 2)
blockReadable += (char)Convert.ToByte(blockRaw.Substring(j,
2), 16);

string datePart = blockReadable.Substring(0, datePartLength);

DateTime date = DateTime.ParseExact(datePart, "MMddyyyy",
System.Globalization.CultureInfo.InvariantCulture) ;
int status = (int)blockReadable[datePartLength];

// Make use of date and status here
}
--
Stanimir Stoyanov
http://stoyanoff.info

<i_*******@hotmail.comwrote in message
news:d6**********************************@q35g2000 hsg.googlegroups.com...
>I have a file, containing hex values for dates (MMDDYYYY)<status
code><??such as:

30353031323030310200303530323230303102003035303332 30303102003035303432303031020030353037323030310200 30353038323030310200

breaking that down:

30353031323030310200
30353032323030310200
30353033323030310200
30353034323030310200...

How do I break it down, convert and, say, throw it into an Array/
HashTable/etc.?? Never messed w/ HEX (binary??) files before.

Appreciate any and all help.

David D.
Oct 30 '08 #2
<i_*******@hotmail.comwrote in message
news:d6**********************************@q35g2000 hsg.googlegroups.com...
>I have a file, containing hex values for dates (MMDDYYYY)<status
code><??such as:

30353031323030310200303530323230303102003035303332 30303102003035303432303031020030353037323030310200 30353038323030310200

breaking that down:

30353031323030310200
30353032323030310200
30353033323030310200
30353034323030310200...

How do I break it down, convert and, say, throw it into an Array/
HashTable/etc.?? Never messed w/ HEX (binary??) files before.
Do you mean your file contains an ASCII representation of hex digits (in
other words, if you open your file in Notepad you see exactly what you wrote
above)?

If so, you can use the Convert.ToByte() overload which takes a String and an
Int32 representing the base of the number in the string. Then you can build
a byte array and convert it to a string with the ASCIIEncoding class. At
that point you'll just have to substring to get out the values you need.

If, however, you were just making a visual representation of your file for
posting purposes and it actually contains "real bytes" then you're simply
skipping the conversion and you can decode your bytes with ASCIIEncoding
directly.

In other words, take a look at the ASCIIEncoding class first, especially
methods like GetString().
Oct 30 '08 #3
On Oct 30, 3:58*pm, "Jeff Johnson" <i....@enough.spamwrote:
<i_robo...@hotmail.comwrote in message

news:d6**********************************@q35g2000 hsg.googlegroups.com...
I have a file, containing hex values for dates (MMDDYYYY)<status
code><??such as:
30353031323030310200303530323230303102003035303332 3030310200303530343230303*102003035303732303031020 030353038323030310200
breaking that down:
30353031323030310200
30353032323030310200
30353033323030310200
30353034323030310200...
How do I break it down, convert and, say, throw it into an Array/
HashTable/etc.?? *Never messed w/ HEX (binary??) files before.

Do you mean your file contains an ASCII representation of hex digits (in
other words, if you open your file in Notepad you see exactly what you wrote
above)?

If so, you can use the Convert.ToByte() overload which takes a String andan
Int32 representing the base of the number in the string. Then you can build
a byte array and convert it to a string with the ASCIIEncoding class. At
that point you'll just have to substring to get out the values you need.

If, however, you were just making a visual representation of your file for
posting purposes and it actually contains "real bytes" then you're simply
skipping the conversion and you can decode your bytes with ASCIIEncoding
directly.

In other words, take a look at the ASCIIEncoding class first, especially
methods like GetString().
The file is from an 'in-house' vendor provided solution:

file opened in a hex editor:

30353031323030310200303530323230303102003035303332 30303102003035303432303031020030353037323030310200 ...

file opened in wordpad:

050120010502200105032001...
Oct 31 '08 #4
On Oct 30, 3:39*pm, "Stanimir Stoyanov" <stoya...@REMOVETHIS.live.com>
wrote:
Hi David,

Below is sample code I wrote how to interpret the data blocks. You should
implement appropriate error handling when parsing non-integer strings.

* * * * string content =
"3035303132303031020030353032323030310200303530333 2303031020030353034323030*310200303530373230303102 0030353038323030310200";

* * * * // TODO: Implement proper exception handling
* * * * // Constants based on the block format
* * * * const int datePartLength = 8;
* * * * const int otherPartLength = 1;
* * * * const int blockLength = (datePartLength + otherPartLength * 2) * 2;

* * * * for (int i = 0; i < content.Length; i += blockLength)
* * * * {
* * * * * * // Each block is extracted and converted here
* * * * * * string blockRaw = content.Substring(i, blockLength);
* * * * * * string blockReadable = string.Empty;

* * * * * * for (int j = 0; j < blockRaw.Length; j += 2)
* * * * * * * * blockReadable += (char)Convert.ToByte(blockRaw.Substring(j,
2), 16);

* * * * * * string datePart = blockReadable.Substring(0, datePartLength);

* * * * * * DateTime date = DateTime.ParseExact(datePart, "MMddyyyy",
System.Globalization.CultureInfo.InvariantCulture) ;
* * * * * * int status = (int)blockReadable[datePartLength];

* * * * * * // Make use of date and status here
* * * * }
--
Stanimir Stoyanovhttp://stoyanoff.info

<i_robo...@hotmail.comwrote in message

news:d6**********************************@q35g2000 hsg.googlegroups.com...
I have a file, containing hex values for dates (MMDDYYYY)<status
code><??such as:
30353031323030310200303530323230303102003035303332 3030310200303530343230303*102003035303732303031020 030353038323030310200
breaking that down:
30353031323030310200
30353032323030310200
30353033323030310200
30353034323030310200...
How do I break it down, convert and, say, throw it into an Array/
HashTable/etc.?? *Never messed w/ HEX (binary??) files before.
Appreciate any and all help.
David D.- Hide quoted text -

- Show quoted text -
Code works great, throws some 'odd' characters (the '0200'...looks
like a square). Guess the last ? is....how to I READ in the file :P

FileStream to byte[]?
Oct 31 '08 #5
On Oct 31, 7:20*am, i_robo...@hotmail.com wrote:
On Oct 30, 3:58*pm, "Jeff Johnson" <i....@enough.spamwrote:


<i_robo...@hotmail.comwrote in message
news:d6**********************************@q35g2000 hsg.googlegroups.com....
>I have a file, containing hex values for dates (MMDDYYYY)<status
code><??such as:
30353031323030310200303530323230303102003035303332 3030310200303530343230303**10200303530373230303102 0030353038323030310200
breaking that down:
30353031323030310200
30353032323030310200
30353033323030310200
30353034323030310200...
How do I break it down, convert and, say, throw it into an Array/
HashTable/etc.?? *Never messed w/ HEX (binary??) files before.
Do you mean your file contains an ASCII representation of hex digits (in
other words, if you open your file in Notepad you see exactly what you wrote
above)?
If so, you can use the Convert.ToByte() overload which takes a String and an
Int32 representing the base of the number in the string. Then you can build
a byte array and convert it to a string with the ASCIIEncoding class. At
that point you'll just have to substring to get out the values you need..
If, however, you were just making a visual representation of your file for
posting purposes and it actually contains "real bytes" then you're simply
skipping the conversion and you can decode your bytes with ASCIIEncoding
directly.
In other words, take a look at the ASCIIEncoding class first, especially
methods like GetString().

The file is from an 'in-house' vendor provided solution:

file opened in a hex editor:

30353031323030310200303530323230303102003035303332 3030310200303530343230303*102003035303732303031020 0...

file opened in wordpad:

05012001 *05022001 *05032001 *...- Hide quoted text -

- Show quoted text -
With the lovely help of all here, I've learned alot today.
File is read as binary (decimal?) into a byte array
each byte[] is converted & appended to string as hex

Then use the code above to parse:

string FILE= (@"<path to file>");
FileStream fs = File.OpenRead(FILE);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, (int)fs.Length);
fs.Close();

//Convert DEC (binary) byte[] to HEX
string content = null;
foreach (byte dec in data)
{
string content = += dec.ToString("X");
}

Woohoo....Thanks again!
Oct 31 '08 #6
<i_*******@hotmail.comwrote in message
news:d8**********************************@i18g2000 prf.googlegroups.com...
File is read as binary (decimal?)
Okay, let me try to clear up this misunderstanding. In the programming world
we sometimes use words in ways that aren't always technically correct.
Computers deal in nothing but binary (in bytes). So if you think about it,
EVERY file is a "binary" file. However, when talking about data (files), we
use the term "binary" to refer to files that contain characters with no
printable representation. In general, this means that the file contains
bytes in the range of 0 -31, with the exception of a few bytes which control
printing, such as a carriage return (13), line feed (10), and tab (9). If a
file contains nothing but bytes from 32-255 (plus 9, 10, and/or 13) then we
refer to it as a "text" file, because you'll more than likely be able to
open it up in Notepad and see all of the characters in it without "weird"
stuff. The file you described in your original post would be considered a
"binary" file because it contains 2 and 0.

Now, as to binary, decimal, and hexadecimal. These are number systems (or,
apparently more correctly, "numeral systems":
http://en.wikipedia.org/wiki/Numeral_system). They are merely a
REPRESENTATION of a number made for the ease of human beings. It is possible
to represent (or display) the same amount of something in several different
ways. For example, consider the number of bars written below:

| | | | | | | | | | | |

In the decimal system, which is what humanity has basically adopted globally
(because we have 10 TOES, not fingers, or so anthropologists believe), this
amount is represented as "12".

In the binary system, this number is represented as "1100" (or, if you like
your binary numbers in 8-bit chunks, "00001100").

In the hexadecimal system, it's "C", or if you prefer 2 digits, "0C".

When switching between systems (changing "bases"), there is no conversion of
the amount. Twelve things are always twelve things, but there can be
conversions of the way the amount is DISPLAYED. This is what it means when
you "convert" decimal to hex, for example.

When you wrote this:

//Convert DEC (binary) byte[] to HEX
string content = null;
foreach (byte dec in data)
{
string content = += dec.ToString("X");
}

what you really meant was "make a 2-digit hexadecimal representation of the
values in the byte array and store it in a string." Saying that your bytes
are "in decimal" is incorrect. If anything, they are "in binary," but it
doesn't matter. The computer knows that twelve is twelve, and forty is
forty.

Back to the original issue: I don't know why you've turned your data into a
string of hex digits. I thought you were looking to extract "05012001" from
the string and turn it into the date 2001-05-01. (Which, by the way, is just
a way to DISPLAY a date, but that's another story. If you're American you
probably prefer 5/1/2001, and if you're European you probably want
1/5/2001.) You should do what I recommended: take the first 8 bytes and use
ASCIIEncoding.GetString() to get a string from them and then use
DateTime.Parse() to get a date.
Oct 31 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: TazCoder | last post by:
I need to figure out an algorithm to convert any file, into it's hex representation in order to print out that file in windows forms. I do not want byteviewer in this and that is why it is...
6
by: Danny Lesandrini | last post by:
I'm using an Access database to drive a web site and the colors of various table backgrounds are stored in Access. I want users of the Access database to be able to select colors for the site, but...
2
by: farseer | last post by:
every example i found shows how to convert an into to a hex STRING. But what if i have an API that expects and int or byte parameter, but that parameter is expected to be a hex value? for...
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
5
by: Russ | last post by:
Hi guys & gals I need to convert, in C#, a decimal hex string into an ascii string (ie "56" -> "V"), and I can't find anything in the vs2003 help files that is remotely helpful. Any suggestions...
7
by: joshua siew | last post by:
heeeeeeeeeeeeeeeeeelpp !!! Can anyone tell me how the heck does VC++ create the ASCII 'ENQ' ???? I'm on a project of programming a PLC devices. I need to send the ENQ ascii value together with...
67
by: Philippe Martin | last post by:
Hi, I'm looking for an algo that would convert a list such as: I'm using python to prototype the algo: this will move to C in an embedded system where an int has 16 bits - I do not wish to use...
2
by: RG | last post by:
I am having trouble parsing the data I need from a Serial Port Buffer. I am sending info to a microcontroller that is being echoed back that I need to remove before I start the actual important...
4
by: dondigitech | last post by:
I want to convert hex to binary without losing bits. I want to preserve the 8-bits because I ultimately need a 24-bit string to grab information from. I am just using this line of code for the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.