Solved

binary string to textfile

Posted on 2006-11-30
4
186 Views
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.
0
Comment
Question by:mdoland
  • 3
4 Comments
 
LVL 14

Expert Comment

by:wayside
Comment Utility
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.
0
 
LVL 14

Accepted Solution

by:
wayside earned 125 total points
Comment Utility
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]);
}
0
 

Author Comment

by:mdoland
Comment Utility
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);
}
0
 
LVL 14

Expert Comment

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

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now