user2942227
user2942227

Reputation: 1023

example EBCDIC file for Java program to convert EBCDIC to ASCII

I am trying to convert EBCDIC file to ASCII using following code :

InputStreamReader rdr = new InputStreamReader(new FileInputStream(<your file>),java.nio.Charset.forName("ibm500"));    
    while((String line = rdr.readLine()) != null) {
        System.out.println(line);

I am trying to find a sample file in EBCDIC format to send it as an input to this program. Can anyone please point me to a sample file. can't find anything online.

Upvotes: 1

Views: 11254

Answers (2)

Hallgrim
Hallgrim

Reputation: 15513

Here is an array with the 255 EBCDIC characters:

var ebcdic = [
'NUL', // Null
'SOH', // Start of Heading
'STX', // Start of Text
'ETX', // End of Text
'PF',  // Punch Off
'\t',  // Horizontal Tab
'LC',  // Lower Case
'DEL', // Delete
' ', ' ',
'SMM', // Start of Manual Message
'\v',  // Vertical Tab
'\f',  // Form Feed
'CR',  // Carriage Return
'SO',  // Shift Out
'SI',  // Shift In
'DLE', // Data Link Escape
'DC1', // Device Control 1
'DC2', // Device Control 2
'TM',  // Tape Mark
'RES', // Restore
'\n',  // New Line
'\h',  // Backspace
'IL',  // Idle
'CAN', // Cancel
'EM',  // End of Medium
'CC',  // Cursor Control
'CU1', // Customer Use 1
'IFS', // Interchange File Separator
'IGS', // Interchange Group Separator
'IRS', // Interchange Record Separator
'IUS', // Interchange Unit Separator
'DS',  // Digit Select
'SOS', // Start of Significance
'FS',  // Field Separator
' ',
'BYP', // Bypass
'\n',  // Line Feed
'ETB', // End of Transmission Block
'ESC', // Escape
' ', ' ',
'SM',  // Set Mode
'CU2', // Customer Use 2
' ',
'ENQ', // Enquiry
'ACK', // Acknowledge
'BEL', // Bell
' ', ' ',
'SYN', // Synchronous Idle
' ',
'PN',  // Punch On
'RS',  // Reader Stop
'UC',  // Upper Case
'EOT', // End of Transmission
' ', ' ', ' ',
'CU3', // Customer Use 3
'DC4', // Device Control 4
'NAK', // Negative Acknowledge
' ',
'SUB', // Substitute
' ', // Space
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
'¢', '.', '<', '(', '+', '|', '&', 
    ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
'!', '$', '*', ')', ';', '¬', '-', '/', 
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
',', '%', '_', '>', '?', 
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
':', '#', '@', '\'','=', '\"',' ',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 
' ', ' ', ' ', ' ', ' ', ' ', ' ', 
'j',  'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', 
'`', // Grave Accent
' ', ' ', ' ', ' ', ' ', ' ', ' ',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 
' ', ' ', ' ', ' ', ' ', ' ', ' ', 
'J',  'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
' ', ' ', ' ', ' ', ' ', ' ', 
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
' ', ' ', ' ', ' ', ' ', ' '
]

You can then convert a buffer with EBCDIC encoded text like this:

var result = ''
for (var i = 0; i < buffer.length; i++) {
    result += ebcdic[buffer[i]]
}

Upvotes: 1

David Conrad
David Conrad

Reputation: 16369

You can use the iconv utility on Unix to convert between character encodings. It is also available for Windows (it's an optional package you can install in Cygwin, for example). You can also use the dd command to convert character encodings.

dd if=ascii.txt of=ebcdic.txt conv=ebcdic

You should also be able to use Java to do the conversion in the other direction from the way you're currently doing it. Just read the file as ASCII and write it as EBCDIC.

Upvotes: 4

Related Questions