473,804 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sure a simple parsing question (hex conversion)

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

303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 102003035303732 303031020030353 038323030310200

breaking that down:

303530313230303 10200
303530323230303 10200
303530333230303 10200
303530343230303 10200...

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 3393
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 =
"30353031323030 310200303530323 230303102003035 303332303031020 030353034323030 310200303530373 230303102003035 303832303031020 0";

// 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.Substri ng(i, blockLength);
string blockReadable = string.Empty;

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

string datePart = blockReadable.S ubstring(0, datePartLength) ;

DateTime date = DateTime.ParseE xact(datePart, "MMddyyyy",
System.Globaliz ation.CultureIn fo.InvariantCul ture);
int status = (int)blockReada ble[datePartLength];

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

<i_*******@hotm ail.comwrote in message
news:d6******** *************** ***********@q35 g2000hsg.google groups.com...
>I have a file, containing hex values for dates (MMDDYYYY)<stat us
code><??such as:

303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 102003035303732 303031020030353 038323030310200

breaking that down:

303530313230303 10200
303530323230303 10200
303530333230303 10200
303530343230303 10200...

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_*******@hotm ail.comwrote in message
news:d6******** *************** ***********@q35 g2000hsg.google groups.com...
>I have a file, containing hex values for dates (MMDDYYYY)<stat us
code><??such as:

303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 102003035303732 303031020030353 038323030310200

breaking that down:

303530313230303 10200
303530323230303 10200
303530333230303 10200
303530343230303 10200...

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.s pamwrote:
<i_robo...@hotm ail.comwrote in message

news:d6******** *************** ***********@q35 g2000hsg.google groups.com...
I have a file, containing hex values for dates (MMDDYYYY)<stat us
code><??such as:
303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 *10200303530373 230303102003035 303832303031020 0
breaking that down:
303530313230303 10200
303530323230303 10200
303530333230303 10200
303530343230303 10200...
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:

303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 102003035303732 3030310200...

file opened in wordpad:

0501200105022 00105032001 ...
Oct 31 '08 #4
On Oct 30, 3:39*pm, "Stanimir Stoyanov" <stoya...@REMOV ETHIS.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 =
"30353031323030 310200303530323 230303102003035 303332303031020 030353034323030 *31020030353037 323030310200303 530383230303102 00";

* * * * // 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.Substri ng(i, blockLength);
* * * * * * string blockReadable = string.Empty;

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

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

* * * * * * DateTime date = DateTime.ParseE xact(datePart, "MMddyyyy",
System.Globaliz ation.CultureIn fo.InvariantCul ture);
* * * * * * int status = (int)blockReada ble[datePartLength];

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

<i_robo...@hotm ail.comwrote in message

news:d6******** *************** ***********@q35 g2000hsg.google groups.com...
I have a file, containing hex values for dates (MMDDYYYY)<stat us
code><??such as:
303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 *10200303530373 230303102003035 303832303031020 0
breaking that down:
303530313230303 10200
303530323230303 10200
303530333230303 10200
303530343230303 10200...
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...@hotma il.com wrote:
On Oct 30, 3:58*pm, "Jeff Johnson" <i....@enough.s pamwrote:


<i_robo...@hotm ail.comwrote in message
news:d6******** *************** ***********@q35 g2000hsg.google groups.com....
>I have a file, containing hex values for dates (MMDDYYYY)<stat us
code><??such as:
303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 **1020030353037 323030310200303 530383230303102 00
breaking that down:
303530313230303 10200
303530323230303 10200
303530333230303 10200
303530343230303 10200...
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:

303530313230303 102003035303232 303031020030353 033323030310200 303530343230303 *10200303530373 23030310200...

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(F ILE);
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....Thank s again!
Oct 31 '08 #6
<i_*******@hotm ail.comwrote in message
news:d8******** *************** ***********@i18 g2000prf.google groups.com...
File is read as binary (decimal?)
Okay, let me try to clear up this misunderstandin g. 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.G etString() 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
2566
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 becoming a problem. The algorithm needs to load up an existing file, convert it to hex, (then save that hex representation into a temp file) output that file into a richtextbox, and then close. Now I have an algorithm that works well, but it is not...
6
6162
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 my mappings between named colors, HEX values and the Long Integer values used in Access are not jibbing. Anyone have a nice list laying around? Danny J Lesandrini dlesandrini@hotmail.com
2
5600
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 instance, i need to convert int zeroVK_D = 47; to int zeroVK_H = 0x2f;
25
7315
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 Byte Dim b As Byte
5
1210
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 that will save me writing my own convert function would be gratefully received!
7
1718
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 others data in a string into the plc. I'm also new in VC++ as well as plc... Thanks for the advice in advanced. Rgds,
67
6314
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 any python library. l1 = #represents the decimal number 12345678 l2 = func (l1) # l2 = #represents 0x12D687
2
4889
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 data reading. For instance this is my buffer string: 012301234FFFFFFxFFFFFFxFFFFFFx Where the FFFFFF is my Hex data I need to read. I am using the "x" as a separater as I was having problems using the VbCrLf. But I think
4
4399
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 conversion: string revLim = Convert.ToString(curveData, 2); // curveData = 0x14 // I want 00010100 // I am getting 10100
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9577
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10569
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10325
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9140
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.