Solved

How do you Save Binary file with some requirements?

Posted on 2003-10-23
6
353 Views
Last Modified: 2010-05-18
Hello,

I have one more question about SavingbinaryFile.  I have this function, void saveBinaryFile( string text, string filename), where the parameter filename is the name of the input or the output file, as the case may be, and where the parameter text is the hexadecimal text to be written to the file.  I am doing the whole project called Text Editor and SaveBinaryFile is  one of my functions.  Within the TextEditor, the hex bytes can be edited, and upon saving the binary file (void saveBinaryFile(string text, string filename), it should convert the hex bytes back into binary and write the binary file....i am thinking using "Integer.parseInt(hexByte, 16) for the convsion...any help will be appreciated..

Thanks a lot
0
Comment
Question by:daskino12
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 9611383
Do you need help in converting the Hex string back to binary bytes?
0
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 9611391
Here are a couple of conversion functions.

template< typename T > std::string HexStrToBinaryStr(const T &HexStr, const char MaxWidth = 32)
{
      std::istringstream ist(HexStr);
      int value;
      ist >> std::hex >> value;
      return std::bitset<256>(value).to_string().c_str() + (256-MaxWidth);
}
template< typename T > char HexStrToChar(const T &HexStr)
{
      std::istringstream ist(HexStr);
      int value;
      ist >> std::hex >> value;
      return value;
}
template< typename T > int HexStrToInt(const T &HexStr)
{
      std::istringstream ist(HexStr);
      int value;
      ist >> std::hex >> value;
      return value;
}
0
 

Author Comment

by:daskino12
ID: 9611462
Hello,

Acutally, i need help for writing this subroutine, void saveBinaryFile(string text, string filename).

Thanks
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 7

Expert Comment

by:burcarpat
ID: 9611496
this looks awfully like the hw/project we had many years ago in a c++ class.  are you sure this is not a hw?

-- ba
0
 

Author Comment

by:daskino12
ID: 9611648
Hello,

Just trying to do some work:)

Any helps?

Thanks
0
 
LVL 22

Expert Comment

by:ambience
ID: 9612020
do a simple cout <<  text and post the results here, this will help understand what kind of coversion you require.

Creating a binary file and writing data to it is easily accomplished by using ofstream. Can you post what you have done so far (within saveAsBiinary)  ?
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to convert c++ code to Android App 3 132
AVI wait icons for CAnimateCtrl in Visual Studio 2008 MFC 1 162
Unable to start eclipse ? 17 163
C++ mouse_event mouse look 7 112
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 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