0

This is a frequently question but I could not find appropriate answer.

I have a char array containing HEX values. I need to write this array in a text file "as a string".


My understanding of HEX not being stored in the files (looked garbage data) was flawed. In Hex editor I could see the data.

My Hex array is actually integer array and not characters. This I need to take into consideration.


I tried this:

   FILE * out = fopen(out_file_name, "w");
   char *in = (char*)malloc(sizeof(char) * num_encryption * 16);
   ....
   fwrite(in, sizeof(char), num_encryption*16 + 1, out);

I also tried stream but it again prints garbage in text files.

The in array contains the HEX strings like this: 21c169ea622e7d52ecd35423f4c3c9f4, and there are 32 lines (num_encryption=32) in total.

I also tried something like this:

std::ofstream outfile(argv[21]);
if(outtfile.is_open()){
  //outtfile.write(wo, sizeof(char)*(num_encryption*16+1));
  for(int k = 0; k < num_encryption; k++){
      for(int j = 0; j < 16; ++j){
         outfile << static_cast<unsigned char>(wo[j + k*16] & 0xff);
      }
   outtfile << std::endl;
  }
}

Even the commented part did not work properly. Any suggestions?


Solution


I just redirected the output (which was fine) to the file:

FILE * out = fopen(out_file_name, "w");
for(int k = 0; k < num_encryption; k++){
    for(int j = 0; j < 16; ++j){
       fprintf(outfile, "%02x", wo[j + k*16] & 0xff);
       }
    fprintf(outfile, "\n");
 }
fclose(outfile);

I am not sure if this is the most elegant solution,but it works for me. If you know anything better then please add here.

7
  • Can you give an example of what actual values are in your in array by actual int value? And what values you hope to output? For example if you have a int value of 15 in your byte array, are you expecting to see an ASCII 'F' (int 70) in the out? If so, you need to convert... Commented Feb 7, 2017 at 4:45
  • fwrite generally won't give you a text file, it moves binary data around. Commented Feb 7, 2017 at 4:49
  • I understand the downvotes to this question, as it might be very elementary for most of you. But as long as you are doing it, also write the answer! Commented Feb 7, 2017 at 5:10
  • Is that char array being used to hold actual character values (like '2') or integer values (like 2)? That's fundamental difference is rather important to figuring out the problem and the solution. The fact that you show a string of 32 characters but then say a line is 16 bytes rather suggests integers. Try opening your "garbage" text file in a hex editor and see if you've ended up outputting data as raw integer values instead of characters. Commented Feb 7, 2017 at 5:31
  • @[TheUndeadFish] Hi your are right!! I opened the file in VIM hex mode and the data is there. I guess then I will have to save the file as char and not HEX integers. Does that make sense? Commented Feb 7, 2017 at 5:44

1 Answer 1

1

If the question is about C++ also, let me give you advice on how to use features superseded by C++. You are using C-style features.

You should use iostreams. Avoid malloc, use new, but, even better, use a vector<char> or unique_ptr<char[]> directly. In this case I will use unique_ptr<char[]> since it seems you do not need to resize the array. I assume you want to write a binary file below.

//I assume you want a binary file
std::ostream file("myfile.txt", std::ios::binary);

std::unique_ptr<char []> in = std::unique_ptr<char []>(new char[num_encryption * 16]);
if (file)
   file.write(in.get(), sizeof(char) * num_encryption * 16);

If you want, instead, write text data in hex, open the file in text mode:

std::ostream file("myfile.txt"); //Note, no std::ios::binary
...
file >> std::hex >> std::noshowbase; //Write in hex with no base
std::copy(in.get(),
          in.get() + num_encryption * 16,
          std::ostream_iterator<char>(file)); //Write one char at a time, in hex, with no base

WARNING: Not tested, just shown the idea on how to write binary or formatted hex text, char by char.

Sign up to request clarification or add additional context in comments.

3 Comments

What is the reason for the negative vote? I will correct it if needed.
I did not downvote. Some people just downvote. No reasons given!
Not really nice assuming this might be oriented towards the solution :D I will be able to stand it anyway. BTW, is this information useful to you? Let me know if you need further help @algoProg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.