Converting user-supplied strings/text to EBCDIC hexadecimal characters

I am currently trying to explore how to convert user-supplied plaintext to hexadecimal characters in ASCII and EBCDIC. The website I’m developing requires the user to supply a string (256 chars min), select ASCII or EBCDIC radio buttons, and convert the user-supplied string to the selected encoding choice in hexadecimal values.
Currently, I have been able to convert user-supplied strings into ASCII in hexadecimal characters.
However, I find difficulty in being able to convert that same string into EBCDIC hexadecimal characters due to its limited usage. Below is the code I use for both ASCII and EBCDIC:

    if (ascii.checked) {
        for (let i = 0; i < str.value.length; i++) 
		{
			document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "0" + str.value.charCodeAt(i).toString(16);
            //document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + str.value.charCodeAt(i);
			//console.log(toString(16));
        }
    }
    else if (ebcdic.checked) {
		for (let i = 0; i < str.value.length; i++) 
		{
			//loop to check all values entered
            //Code to convert text to EBCDIC, need help with this
		    document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + str.value.charCodeAt(i);
		}
    }

Here is the full code which has encoding.js, style.css, and 1.html:

encoding.js:

function myFunction() 
{
    //Get both elements
    const ascii = document.getElementById('ascii')
    const ebcdic = document.getElementById('ebcdic')

    let str = document.getElementById("text_id");
    let a = "ASCII Code is == >  ";

    // Below checks to see if the user selects writed more than 255 chars
    if (str.value.length < 256) {
        console.log("null");
        return null;
        // prints and returns null if the user entered a string less than 256 characters
    }

    // Below checks to see if the user selects a radio button
    let radio_selected = false;
    document.querySelectorAll('input[type="radio"]').forEach(function (radio) {
        if (radio.checked) {
            radio_selected = true;
        }
    })
    if (!radio_selected) {
        console.log("The radio has not been checked, please select a button");
        return;
    }
    
    //If one of the elements is checked it triggers a condition, if the other is cheked it triggers the other condition
    if (ascii.checked) {
        for (let i = 0; i < str.value.length; i++) 
		{
			document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "0" + str.value.charCodeAt(i).toString(16);
            //document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + str.value.charCodeAt(i);
			//console.log(toString(16));
        }
    }
    else if (ebcdic.checked) {
		for (let i = 0; i < str.value.length; i++) 
		{
			//loop to check all values entered
            //Code to convert text to EBCDIC, need help with this
		    document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + str.value.charCodeAt(i);
		}
    }
}

1.html:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>Converter for ASCII or EBCDIC</title>
      <link href="style.css" rel="stylesheet" type="text/css"/>
      <script src="encoding.js" defer></script>
   </head>

   <body style="text-align:center;">
	<label for="text">Enter a text that is at least 256 characters long</label><br>
	<input type="text" id="text_id" name="text" minlength="256">

	<p>Select the following:</p>
	<div>
		<input id="ascii" type="radio" name="encoding" value="ascii">
		<label for="ascii">ASCII</label>
	</div>
	<div>
		<input id="ebcdic" type="radio" name="encoding" value="ebcdic">
		<label for="ebcdic">EBCDIC</label>
	</div>
	<button onclick="myFunction()" type="button">"Run!"</button>
	<label for="button">"Run!"</label>
	<p id="demo" style="color:red;">
   </body>
</html>

style.css:

body 
{
  font: 12pt Arial;   
}

input[type=radio] + label::before 
{
   border-radius: 10px;   
}
input {
  border: 2px solid currentcolor;
}
input:invalid {
  border: 2px dashed red;
}
input:invalid:focus {
  background-image: linear-gradient(pink, lightgreen);
}
encod

Welcome to the Julia language community.

I just had to answer, as this may be the most obscure question I’ve seen on Julia discourse, no offense.

May I ask, why? Is it just curiosity with programming, or dare I say school assignment?

It seem what you may be after is:

Note, Julia only supports UTF-8, as it’s the default for the future (but you can also do 8-bit byte strings with no, or user-defined meaning). And well UTF-16 to/from UTF-8 (for e.g. Windows). Intentionally other encodings are left out, of the standard library, including Latin1 etc. let alone obscure like EBCDIC.

Note, ASCII is supported by (7-bit) ASCII being a subset of UTF-8. “Extended ASCII”

If you need any of that, you must use a package, such as that one (there might be more).

[If there’s anything more obscure, it might be UTF-EBCDIC.]

You wouldn’t be able to convert everything from a webpage to EBCDIC (or ASCII), if you allow any UTF-8 input as it seems you’re doing.

There are some other threads such as this one that might be helpful:

You CAN do web programming in Julia (your question wasn’t how), but I see the code you use is for a different language, likely JavaScript? And you can use it with, or avoid it, but if it works for you great. Then the question is likely warranted elsewhere. I’m not sure, but did you post to this community by accident?

FYI: EBCDIC-platforms, i.e. mainframes, are not supported by Julia. I realize you’re not targeting those?! Just having a valid question on converting the encoding, that you can do on any platform.

For information, EBCDIC (IBM 3270 Char Set Ref Ch 10, GA27-2837-9, April 1987) is used inside the SEGY format to encode a certain Textual File Header (optionally ASCII can be used). SEGY is a standard data format used by the seismic industry.

This page provides conversion tables between ASCII and EBCDIC.
After reformatting the tables, these may be parsed in Julia with parse.(Int, hexadecstring, base= 16).

This may not be the right “flavor” of EBCDIC for you, but there are similar conversion tables on the web for other variants.

1 Like

See also this discussion on discourse, which provided a simple conversion function: Decoding EBCDIC array to string - #3 by stevengj

You can easily modify it to convert in other direction. Using the ebcdic_table defined in that post, do:

const ebcdic_inv_table = Dict(c => UInt8(b-1) for (b,c) in pairs(ebcdic_table))

string_to_ebcdic(s::String) = [ebcdic_inv_table[c] for c in s]
2 Likes