473,799 Members | 3,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hexa reading from a file

Hi,

I have a problem related to File Reading. I have a file which contains
data which is in Hexa format. I have to read that data and push it to
a function which takes a parameter of type char. I am able to read the
data from the file, which i am getting as characters.

For example: Suppose the Hexa value from the File data is ABCDEFGH. In
hexa form 0xAB is one byte. So, while reading this data A is taken as
a byte which is not a required format. I am supposed to get AB as a
byte, which i am not getting.

Hope you people understand my query.

with regards,

Chandra.

Mar 1 '07 #1
1 2117
On Mar 1, 10:58 am, "Chandra" <forquer...@gma il.comwrote:
I have a problem related to File Reading. I have a file which contains
data which is in Hexa format. I have to read that data and push it to
a function which takes a parameter of type char. I am able to read the
data from the file, which i am getting as characters.

For example: Suppose the Hexa value from the File data is ABCDEFGH. In
hexa form 0xAB is one byte. So, while reading this data A is taken as
a byte which is not a required format. I am supposed to get AB as a
byte, which i am not getting.
First, the common abbreviation for hexadecimal is hex (cf. std::hex),
not hexa. Second, G and H are not valid hexadecimal values.

Now, let's say have a std::string with "ABCDEF" and you want to divide
it up into 8-bit bytes. You could do something like this (untested):

const string str = "ABCDEF";
const unsigned i = ConvertString<u nsigned>( "0x" + str.substr( 0,
2 ) ); // Parameter yields "0xAB"
assert( i == 0xAB );

where

template<typena me T>
T ConvertString( const std::string& s )
{
std::istringstr eam iss( s );
T t;
if( !(iss >std::resetiosf lags( std::ios::basef ield ) >t >>
std::ws).eof() )
throw std::invalid_ar gument( "Unable to convert \"" + s +
"\"" );
return t;
}

See also http://www.parashift.com/c++-faq-lit....html#faq-39.2
and the docs on Boost's lexical_cast (http://boost.org/libs/conversion/
lexical_cast.ht m).

Now, assuming I typed everything correctly, you just need to put it in
a loop to extract the rest of the values. Also note that the
conversion function throws an exception if the input is invalid, so
you'll need to handle that.

Cheers! --M

Mar 1 '07 #2

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

Similar topics

4
5266
by: Dany | last post by:
Hi When I open an Hexa file I have some weird char like when I open it with Notepad. I want to open it in a readeable form (ex: "FF25AB") like with Ultra edit. Thanks regards, Dany
1
11743
by: Adam | last post by:
Hello, I'm trying to decifer the data in the table that stores the data in the binary format. All numbers are placed in varbinary fields. All I know is the MS SQL 2000 database useing collation SQL_Latin1_General_CP1_CI_AS (default). For example the content of the field is: (0xB4F5000000000000) in unicode and defined as varbinary(8).
3
6896
by: Golan | last post by:
Hello, I have a hexa file which I need to convert to decimal. I use memcpy into variables (char for one octet, short for 2 octets and int for 4 octets) and then print the value into the file by using fprintf. The problem is that I don't know how to convert a field of 6 octets? Should I use a long variable? Thanks
2
3547
by: [Gho] | last post by:
How to convert a Hexa value to negative and non negative value : if i get 0x1d = result should be 29 but if i get fd = result should be -3 How to do that
6
2195
by: Natan Vivo | last post by:
Ok, let me explain this first. I did a search about 'hexadecimal' first and found a lot of threads about it, but none of them tell me what i want to know. I am new to C#, and want to work with binary data (actually some custom file formats). I used to do this a lot in PHP, but i can't understand how this works in C#. in PHP i could do this:
2
17048
by: Nadav | last post by:
Hi, How can I use C# to print a number in hexadecimal e.g. printf(" %x "); or print 15 ( decimal ) as 'F' (hexa) Can a number be printed as binary as-well? Thanks in advance, Nadav.
2
3700
by: sarchanakumar | last post by:
Hi friends, I want to write a program to add two hexa-decimal numbers. I know there is no way of direction addtion. My logic is, first convert two hexa-numbers into decimal after that we add and finally we have to convert the resultant decimal into hexadecimal. Is it right? Give your suggestion.
5
2367
by: lokeshrajoria | last post by:
hello, how to convert binary to hexa decimal..... thanks & regards lokiiiii
1
2261
by: hpbrothers | last post by:
How to declare hexa & octadecimals data types in c++
0
9538
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
10473
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...
1
10219
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
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...
0
6804
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();...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
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
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.