Solved

binary string to textfile

Posted on 2006-11-30
4
189 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 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