Question

Our application is having a module written in C/C++ which will take an ascii file and convert it into an ebcdic file which will be later ftped to the mainframe.

Now we are asked to rewrite this C/C++ code in java. We are having 4 different output types, one as normal character, then packed decimal, mainframe binary and float. Each will have a separate input ascii length and the corresponding output ebcdic length.

When doing this, we are not able to convert one particular type of mainframe binary in which it will convert an input of 11 bytes to an output of 4 bytes of mainframe binary. One sample input is ' 451841' and the output in ebcdic file when opened in unix using the less command is '^@^Få^A'.

The C code is like below:

/* This routine converts UNIX character data into mainframe binary    */
/* representation.  It is called from the switch statement when       */
/* input field type is "6"                                            */

int UNIX_mf_binary( ) 
{
unsigned char* out_rec;
int out_position;
int int_work_area;
long int long_int_work_area;
int char_work_area;
char temp_area[11];     // This will be having the value of '     451841'
unsigned char* c_integer_position;
unsigned int i_integer_position;
  long_int_work_area = atol(temp_area);
  i_integer_position = &long_int_work_area;
  c_integer_position = i_integer_position;

  out_rec[out_position] = *c_integer_position; 
  c_integer_position = i_integer_position + 1;
  out_position = out_position + 1;

  out_rec[out_position] = *c_integer_position; 
  c_integer_position = i_integer_position + 2;
  out_position = out_position + 1;

  out_rec[out_position] = *c_integer_position; 
  c_integer_position = i_integer_position + 3;
  out_position = out_position + 1;

  out_rec[out_position] = *c_integer_position; 
  c_integer_position = i_integer_position + 4;
  out_position = out_position + 1;

}

And the java code in which we have rewritten this part looks like:

 String bin = "     451841";
 int b; 
 b = Integer.parseInt(bin.trim());
 stringBuffer.append((char) (0x000000FF & b));
 stringBuffer.append((char) (0x0000FF00 & (b)));
 stringBuffer.append((char) (0x00FF0000 & (b)));
 stringBuffer.append((char) (0xFF000000 & (b)));
 fos.write(stringBuffer.toString().getBytes());

But with the java code the value is coming something like: ^@^@^@^A

We are using jre version 1.5. Can somebody help me understand how to rewrite this C code in java version 1.5? One more thing, even though the temp_area[11] is of 11 char long, the max value we are having in that is of 6 digit.

Was it helpful?

Solution

You have failed to shift the 8-bit values down after masking:

stringBuffer.append((char) (0xFF000000 & (b)));

should be:

stringBuffer.append((char) ((b >> 24) & 0xff));

Note that by shifting first, we can omit the unwieldy large constants and always just do 8-bit masking.

OTHER TIPS

When you AND the value of b, you need to shift it down to the last 8 bits.

stringBuffer.append((char) ((0x0000FF00 & (b)) >> 8));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top