[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

binary string to textfile

Posted on 2006-11-30
4
Medium Priority
?
194 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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

650 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