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
Solved

Use FileWriter to write integer data in hex format

Posted on 2008-06-11
4
667 Views
Last Modified: 2013-12-14
I am currently outputting integer data to a file using FileWriter.  I need to have the data be in hexadecimal format.  The problem is I do not know how to do this with the FileWriter::WriteLine().  Please help.
0
Comment
Question by:gr3ygh0st
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 21768539
Do you mean StreamWriter?
0
 

Author Comment

by:gr3ygh0st
ID: 21769374
Yes sorry that is what I meant.

writer is of StreamWriter type and I am doing this

writer->wrtieLine(m_uTotalValue);

I need m_uTotalValue to appear in hex form in the file.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 21772444
You should be able to use:

    writer->WriteLine(m_uTotalValue->ToString("x"));

but, that all depends m_uTotalValue type.
0
 
LVL 19

Assisted Solution

by:LordOfPorts
LordOfPorts earned 125 total points
ID: 21786250
Would you like to create a text file that contains hexadecimal values of the integers in form of text or an actual binary file? If you would like to create a binary file that can be viewed in a hex editor you can use the BinaryWriter object instead of of StreamWriter. Below is sample code, we take e.g. the integer value 30 which in hex in 1E and save it once with the SteamWriter and once with the BinaryWriter.

Now, if you open FromStreamWriter.bin in Notepad it reads 1E but if you open it in a hex editor it will show you the hex value 31 45 0D 0A because the StreamWriter saved the hex representation of integer 30 as text. If you open FromBinaryWriter.bin in Notepad the value not readable as it is a binary file however it you open it in a hex editor it will show that the value is 1E which is the true hex value of 30.
int nVal = 30;
 
StreamWriter ^ sw = gcnew StreamWriter("FromStreamWriter.bin");
sw->WriteLine(nVal.ToString("X"));
sw->Close();
 
BinaryWriter ^ bw = gcnew BinaryWriter(File::Open("FromBinaryWriter.bin", FileMode::Create));
bw->Write(nVal);
bw->Close();

Open in new window

FromStreamWriterInNotepad.png
FromStreamWriterInHexReader.png
FromBinaryWriterInHexReader.png
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

790 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