Solved

binary string to textfile

Posted on 2006-11-30
4
191 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
[X]
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
4 Comments
 
LVL 14

Expert Comment

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

Accepted Solution

by:
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]);
}
0
 

Author Comment

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

Expert Comment

by:wayside
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. :)

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

705 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