0

How can I create a file that uses 4-bit encoding to represent integers 0-9 separated by a comma ('1111')? for example:

2,34,99 = 0010 1111 0011 0100 1111 1001 1001 => actually becomes without spaces 0010111100110100111110011001 = binary.txt

Therefore 0010111100110100111110011001 is what I see when I view the file ('binary.txt')in WINHEX in binary view but I would see 2,34,99 when view the file (binary.txt) in Notepad.

If not Notepad, is there another decoder that will do '4-bit encoding' or do I have a write a 'decoder program' to view the integers?

How can I do this in C++?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
chineerat
  • 97
  • 2
  • 12

3 Answers3

1

The basic idea of your format (4 bits per decimal digit) is well known and called BCD (Binary Coded Decimal). But I doubt the use of 0xF as an encoding for a coma is something well established and even more supported by notepad.

Writing a program in C++ to do the encoding and decoding would be quite easy. The only difficulty would be that the standard IO use byte as the more basic unit, not bit, so you'd have to group yourself the bits into a byte.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
  • Since it is only integers I do not see the need for 8bits. I may use Winhex to read the file instead of notepad. Any ideas on how to output the binary to a to file? I have used '("c:\\test.dat",ios::binary|ios::in);' but it converts the data to ASCII. – chineerat Feb 27 '11 at 21:26
0

You can decode the files using od -tx1 if you have that (digits will show up as digits, commas will show up as f). You can also use xxd to go both directions; it comes with Vim. Use xxd -r -p to copy hex characters from stdin to a binary file on stdout, and xxd -p to go the other way. You can use sed or tr to change f back and forth to ,.

Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78
0

This is the simplest C++ 4-bit (BCD) encoding algorithm I could come up with - wouldn't call it exactly easy, but no rocket science either. Extracts one digit at a time by dividing and then adds them to the string:

#include <iostream>

int main() {
const unsigned int ints = 3;
unsigned int a[ints] = {2,34,99}; // these are the original ints
unsigned int bytes_per_int = 6;
char * result = new char[bytes_per_int * ints + 1];
// enough space for 11 digits per int plus comma, 8-bit chars
for (int j=0; j < bytes_per_int * ints; ++j)
{
    result[j] = 0xFF; // fill with FF
}
result[bytes_per_int*ints] = 0; // null terminated string

unsigned int rpos = bytes_per_int * ints * 2; // result position, start from the end of result
int i = ints; // start from the end of the array too.
while (i != 0) {
    --i;
    unsigned int b = a[i];
    while (b != 0) {
       --rpos;
       unsigned int digit = b % 10; // take the lowest decimal digit of b
       if (rpos & 1) {
           // odd rpos means we set the lowest bits of a char
           result[(rpos >> 1)] = digit;
       }
       else {
           // even rpos means we set the highest bits of a char
           result[(rpos >> 1)] |= (digit << 4);
       }
       b /= 10; // make the next digit the new lowest digit
    }
    if (i != 0 || (rpos & 1))
    {
       // add the comma
       --rpos;
       if (rpos & 1) {
           result[(rpos >> 1)] = 0x0F;
       }
       else {
           result[(rpos >> 1)] |= 0xF0;
       }
    }
}
std::cout << result;
}

Trimming the bogus data left at the start portion of the result according to rpos will be left as an exercise for the reader.

The subproblem of BCD conversion has also been discussed before: Unsigned Integer to BCD conversion?

If you want a more efficient algorithm, here's a bunch of lecture slides with conversion from 8-bit ints to BCD: http://edda.csie.dyu.edu.tw/course/fpga/Binary2BCD.pdf

Community
  • 1
  • 1
Olli Etuaho
  • 303
  • 4
  • 9
  • Hi! thanks for your time to write code. How do I write/save the 'result' to a file? – chineerat Feb 27 '11 at 21:19
  • In Unix shell, you can just save the output from the command line using for example "myprogram > outputfile". If you need to do it in the C++ code, I suggest you study C++ file I/O: http://www.cplusplus.com/doc/tutorial/files/ It was an interesting exercise I haven't done before, so no need to thank me for the time. :) – Olli Etuaho Feb 28 '11 at 13:07