Solved

Parsing hex values from an xml file

Posted on 2012-03-30
11
207 Views
Last Modified: 2012-09-12
I am converting a software application from reading and parsing a standard ASCII text file containing operational parameters to an input file in XML format. One of the parameters needs to be a sequence of bytes. Each byte can have a value from 0x00 to 0xFF. So a sample line from the XML file might then be:
<value>0x34 0x3 0x08 0xf2 0xD7 0X39</value>
Note that I purposely presented variations in the format of how a user might populate this field. What is the best way to convert that string into an array of bytes with these values?
0
Comment
Question by:bbrms
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
11 Comments
 
LVL 10

Expert Comment

by:pfrancois
ID: 37789370
No problem. In which language do you want to code?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37789430
Actually, there will be problems because a number of characters are not legal in XML.  Only a few control characters are allowed and four characters must be 'escaped' to be included in an XML document.  http://www.w3.org/TR/2000/REC-xml-20001006#charsets

XML is supposed to be a text format.  The standard says you can use % encoding for unrecognized characters.  There is nothing, however, about a real 'byte array'.
0
 

Author Comment

by:bbrms
ID: 37789461
I apologize. I forgot to mention the language. I am coding in C++.  
Per Dave’s comment: please note that my example demonstrates that I am inputting a  human-readable ASCI string that contains only digits, spaces and the letters A through F in any combination of lower and upper case. This should pose no problem for  XML. The application will process the string and convert the text to  an array of bytes corresponding to the string. I can code up my own solution using sscanf() but I am hoping that there is a solution that is more elegant than that and less time-consuming to code. The string can have any number of hexadecimal  values designated (i.e. the size of the string can be from zero to N, where N is probably limited to a few hundred at the most, and that is a fringe case)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37789619
I misunderstood.  I thought you were trying to put the byte array into the XML.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37849173
0
 

Author Comment

by:bbrms
ID: 37849775
mplungjan provded a google search for C++ string representation of  hex. If you read my two posts carefully under this topic you will see that I already have a string of hex characters being parsed by my XML parser. When I posted this originally I had hoped that some expert user of XML would know of a routine available from perhaps some open source utility that would process the text line as I described it and generate perhaps a vector<uint_8> container of translated values from that string, without being sensitive to case of the 'x' character, the number of spaces or tabs and other wise be very tolerant in its range of acceptable syntax for the input string. I don't believe what I was looking for exists based on the posts I am getting, so I have coded up my own solution.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37849907
I asked because the top links gave examples of how to convert strings back to binary.
I am not at all a c programmer but since the question was not commented on at all I hoped I could at least get you started.
Feel free to post what you ended up with and accept your own comment as answer.
0
 
LVL 34

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 37851864
the below code should do

 std::string s = "0x34 0x3 0x08 0xf2 0xD7 0X39";
 std::istringstream istmp(s);
 std::vector<unsigned char> v;
 unsigned int b;
 char c1, c2;
 while (istmp >> c1 >> c2 >> std::hex >> b)
 {
       if (c1 != '0' || toupper(c2) != 'X' || b >= 256)
            break;
        v.push_back((unsigned char)b);
 }

Open in new window


Sara
0
 
LVL 1

Accepted Solution

by:
philrosenberg earned 250 total points
ID: 37854839
You could simplify Sara's code by using showbase I think
   fstream fin;
   //code to open file and read to correct place
   unsigned int hexnumber;
   unsigned char hexbyte;
   fin >> std::showbase >> std::hex >> hexnumber;
   hexbyte=(unsigned char) hexnumber;

Open in new window

Phil
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question