Background
X9.37 is a standard format used in electronic check exchange (when a depository institution sends an image cash letter file as a deposit to the Federal Reserve Bank, or when the Federal Reserve Bank transfers, presents or returns an image cash letter file to a depository institution).
This file format supports both ASCII and EBCDIC encoding.
A sample file can be downloaded here.
I see all kinds of junk (special) characters when I open this file in Notepad++. I tried changing the encoding to all formats listed in Notepad++ menu, but can't seem to view the text in a human-readable format.
There is more information regarding file format transformation in Page 4 of 18 in this white paper. For your quick reference:
... generalized tools do not understand the X9.37 file format. However, they can be coerced into browsing and editing X9.37 files when they can be sensitive to the four byte field zero lengths, which very conveniently define the beginning of each X9.37 record that occurs within the overall x9 file.
...editors must understand the character set being used to represent the actual data so it can be visually displayed. The easiest way to do this is to create an editing environment that supports either EBCDIC or ASCII data. Alternatively, you can transform your x9 file to a standard character set (eg, EBCDIC) before you begin your editing process.
I couldn't quite figure out how to coerce Notepad++ into revealing the contents of the sample file.
However, I was able to view the file via X9Assist tool by X9Ware. The sample file above when viewed in X9Assist looks like so:
i.e. there is (text) data in that file that contains some header info and details related to one or more checks being transmitted (the amount, routing number, etc) which I need to extract into Salesforce along with the image of the check itself (see screenshot above for clarity).
What did I try?
I attached this file to a record in Salesforce Lightning and did something in lines of:
ContentVersion.VersionData.toString();
Above threw BLOB is not a valid UTF-8 string
exception, which made sense.
So I looked at this, this, this and this to see what others have recommended in such a situation. I tried a recommendation from one of the posts there:
public static String blobToString(Blob input, String inCharset){
String hex = EncodingUtil.convertToHex(input);
final Integer bytesCount = hex.length() >> 1;
String[] bytes = new String[bytesCount];
for(Integer i = 0; i < bytesCount; ++i)
bytes[i] = hex.mid(i << 1, 2);
return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);
}
and called it like so:
blobToString(ContentVersion.VersionData, 'ASCII');
Above printed gibberish in debug logs:
Then I tried blobToString(ContentVersion.VersionData, 'ISO-8859-1');
with similar results.
However, blobToString(ContentVersion.VersionData, 'EBCDIC');
returned ERROR: System.StringException: Encoding EBCDIC is not supported
Can someone shed some light on how such a file format can be digested in Salesforce (if at all)? Open to any workarounds as well. Thank you.