binary string to textfile

Posted on 2006-11-30
Last Modified: 2010-04-01
I have a program that encrypts a password. I want to store the encrypted password in a text file. How do I do that? Is there some function I can use to automatically convert it to a text format and then back again?

I  use Visual Studio.
Question by:mdoland
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
  • 3
LVL 14

Expert Comment

ID: 18048353
Simple way: print the binary password as hexidecimal digits. The length in the disk will be twice as long as in memory because it takes 2 hex digits to write one byte. When you read it back in it is very easy to convert back to binary.

A little tougher: base64-encode the binary password before writing it. The length in the disk file will be about 1/3 longer than the length in memory. base64 encoding and decoding functions are widely available, just google for it.

Easy, but less user-friendly: just write out the binary password as binary data. The down-side is that you won't be able to easily view it in an editor.

Which to choose depends on your needs.
LVL 14

Accepted Solution

wayside earned 125 total points
ID: 18048566
To convert a binary buffer to a string of hex digits:

BYTE password[256];

// fill somehow

char buffer[513] = { 0 };

for (int i=0; i<255; i++) {
   sprintf(&buffer[i], "%x", (password[i] >> 4) & 0x0f);
   sprintf(&buffer[i+1], "%x", password[i] & 0x0f);

To convert back is similar:

byte convert(char hexdigit) {
  if (hexdigit >= '0' || hexdigit <= '9')   return char - '0';
 else if (hexdigit == 'a' || hexdigit == 'A') return 10;
else if (hexdigit == 'b' || hexdigit == 'B') return 11;
else if (hexdigit == 'c' || hexdigit == 'C') return 12;
else if (hexdigit == 'd' || hexdigit == 'D') return 13;
else if (hexdigit == 'e' || hexdigit == 'E') return 14;
else if (hexdigit == 'f' || hexdigit == 'F') return 15;
else return 0;

BYTE password[256] = { 0 };

char buffer[513] = { 0 };
// read the password into the buffer

for (int i=0; i<255; i++) {
   password[i] = convert(buffer[i*2]) << 4 | convert(buffer[i*2+1]);

Author Comment

ID: 18052336
Shouldn't this part:
for (int i=0; i<255; i++) {
   sprintf(&buffer[i], "%x", (password[i] >> 4) & 0x0f);
   sprintf(&buffer[i+1], "%x", password[i] & 0x0f);

be like this:

for (int i=0; i<255; i++) {
   sprintf(&buffer[2*i], "%x", (password[i] >> 4) & 0x0f);
   sprintf(&buffer[2*i+1], "%x", password[i] & 0x0f);
LVL 14

Expert Comment

ID: 18053603
Yep, you are right. That's what happens when you don't test your code before posting it, no matter how simple it looks. :)


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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