Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1832
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1832
EBCDIC
Por Edson Alves, Faculdade UnB Gama Brazil
Timelimit: 3
EBCDIC (Extended Binary Coded Decimal Interchange Code) is a 8-bits character encoding schema developed by IBM in sixties. It is based on punched cards encoding, and was used in IBM mainframes. Though it uses a larger encoding interval than ASCII 7-bits range, EBCDIC is less user friendly that ASCII because the alphanumeric characters are not contiguous as in the former schema.
The following image shows the EBCDIC encoding table. White cells area unused values, and two or more uppercase characters represents non-printable chars. BLANK is the whitespace character.
Write a program that convert a EBCDIC-encoded text to ASCII encoding.
Input
The input consists in several test cases. Each case is composed by a single line with the 3 digit octal values of each EBCDIC character, separated by a single space.
You may assume that the messages will only correspond to alphanumeric or whitespace characters.
Output
For each test case the output must be the message in ASCII encoding, followed by a newline character.
Input Samples | Output Samples |
343 205 247 243 226 324 205 225 242 201 207 205 224 100 361 324 205 225 242 201 207 205 224 100 362 306 211 224 |
Texto Mensagem 1 Mensagem 2 Fim |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <:bits/stdc++.h>
using namespace std;
int oct(string x)
{
int ans = 0;
int sz = x.size();
for (int i = 0; i < sz; ++i)
ans += (x[i] - '0') * (int)pow(8, sz - i - 1);
return ans;
}
int main()
{
string in, s2;
ios_base :: sync_with_stdio(0);
cin.tie(0);
while (getline(cin, in))
{
stringstream ss;
ss << in;
while (ss >> s2)
{
int o = oct(s2);
switch(o)
{
case 64: cout << ' '; break;
case 129: cout << 'a'; break;
case 130: cout << 'b'; break;
case 131: cout << 'c'; break;
case 132: cout << 'd'; break;
case 133: cout << 'e'; break;
case 134: cout << 'f'; break;
case 135: cout << 'g'; break;
case 136: cout << 'h'; break;
case 137: cout << 'i'; break;
case 145: cout << 'j'; break;
case 146: cout << 'k'; break;
case 147: cout << 'l'; break;
case 148: cout << 'm'; break;
case 149: cout << 'n'; break;
case 150: cout << 'o'; break;
case 151: cout << 'p'; break;
case 152: cout << 'q'; break;
case 153: cout << 'r'; break;
case 162: cout << 's'; break;
case 163: cout << 't'; break;
case 164: cout << 'u'; break;
case 165: cout << 'v'; break;
case 166: cout << 'w'; break;
case 167: cout << 'x'; break;
case 168: cout << 'y'; break;
case 169: cout << 'z'; break;
case 193: cout << 'A'; break;
case 194: cout << 'B'; break;
case 195: cout << 'C'; break;
case 196: cout << 'D'; break;
case 197: cout << 'E'; break;
case 198: cout << 'F'; break;
case 199: cout << 'G'; break;
case 200: cout << 'H'; break;
case 201: cout << 'I'; break;
case 209: cout << 'J'; break;
case 210: cout << 'K'; break;
case 211: cout << 'L'; break;
case 212: cout << 'M'; break;
case 213: cout << 'N'; break;
case 214: cout << 'O'; break;
case 215: cout << 'P'; break;
case 216: cout << 'Q'; break;
case 217: cout << 'R'; break;
case 226: cout << 'S'; break;
case 227: cout << 'T'; break;
case 228: cout << 'U'; break;
case 229: cout << 'V'; break;
case 230: cout << 'W'; break;
case 231: cout << 'X'; break;
case 232: cout << 'Y'; break;
case 233: cout << 'Z'; break;
default: cout << (char)((o - 240) + '0');
}
}
cout << '\n';
}
}
beecrowd | 1832
Copy The Code &
Try With Live Editor
Input
324 205 225 242 201 207 205 224 100 361
324 205 225 242 201 207 205 224 100 362
306 211 224
Output
Mensagem 1
Mensagem 2
Fim
#2 Code Example with Javascript Programming
Code -
Javascript Programming
"use strict"
const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8")
.split("\n")
.map(line => line.split(" "))
const SHORT_EBCDIC_TABLE = Object.freeze({
"064": " ",
"129": "a",
"130": "b",
"131": "c",
"132": "d",
"133": "e",
"134": "f",
"135": "g",
"136": "h",
"137": "i",
"145": "j",
"146": "k",
"147": "l",
"148": "m",
"149": "n",
"150": "o",
"151": "p",
"152": "q",
"153": "r",
"162": "s",
"163": "t",
"164": "u",
"165": "v",
"166": "w",
"167": "x",
"168": "y",
"169": "z",
"192": "{",
"193": "A",
"194": "B",
"195": "C",
"196": "D",
"197": "E",
"198": "F",
"199": "G",
"200": "H",
"201": "I",
"208": "}",
"209": "J",
"210": "K",
"211": "L",
"212": "M",
"213": "N",
"214": "O",
"215": "P",
"216": "Q",
"217": "R",
"226": "S",
"227": "T",
"228": "U",
"229": "V",
"230": "W",
"231": "X",
"232": "Y",
"233": "Z",
"240": "0",
"241": "1",
"242": "2",
"243": "3",
"244": "4",
"245": "5",
"246": "6",
"247": "7",
"248": "8",
"249": "9",
})
const ConvertBase = (num) => ({
from: (baseFrom) => ({
to: (baseTo) => parseInt(num, baseFrom).toString(baseTo)
})
})
function main() {
const responses = []
for (const octals of input) {
if (octals.length === 0) break
const ASCIIChars = octals
.map(oct => ConvertBase(oct).from(8).to(10).padStart(3, "0")) // octToDec
.map(dec => SHORT_EBCDIC_TABLE[dec] ?? "") // DecToChar
responses.push(ASCIIChars.join(""))
}
console.log(responses.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
324 205 225 242 201 207 205 224 100 361
324 205 225 242 201 207 205 224 100 362
306 211 224
Output
Mensagem 1
Mensagem 2
Fim