0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

プログラムを文字列に変換

Last updated at Posted at 2022-12-03

テンプレートプログラムを一部変えて自動生成するプログラムを作成する際に,テンプレートプログラムを文字列に変換したくなりました

変換するツールがないか調べた所,次のようなサイトを見つけました

サイトにアクセスしプログラムを貼り付けQuoted Stringを選択すると一つの文字列として出力されました

Screenshot from 2022-12-03 18-16-37.png

pythonに変数として入れて,printしてみると正常にプログラムがプリントできました

Screenshot from 2022-12-03 18-17-19.png

Screenshot from 2022-12-03 18-17-36.png

ちなみに変換プログラムは次のようになっていt

convert.js
function fileLoaded() {
    document.getElementById("result").value = "";
    document.getElementById("result").style.display = "none";
    document.getElementById("size").innerText = "Please Choose a file first";
    if (!bytes) return;

    var fileTypeSelect = document.getElementById("fileType");
    var fileType = fileTypeSelect.options[fileTypeSelect.selectedIndex].value;

    if (bytes.length>(20*1024)) {
      document.getElementById("size").innerText = "File too long - must be less than 20kB";
      return;
    }

    var dqStr = "";
    var tmpStr = "";
    var lastCh = 0;
    for (var i=0;i<bytes.length;i++) {
      var ch = bytes[i];
      // templated string
      if (ch==92) tmpStr += "\\\\"; // escaping slash
      else if (ch==96) tmpStr += "\\\`"; // template quote
      else if (lastCh==36 && ch==126) tmpStr += "\\{" // ${
      else tmpStr += String.fromCharCode(ch);
      // double-quoted string
      if (ch==34) dqStr += "\\\"";
      else if (ch==9) dqStr += "\\t";
      else if (ch==10) dqStr += "\\n";
      else if (ch==13) dqStr += "\\r";
      else if (ch==92) dqStr += "\\\\";
      else if (ch>=32 && ch<127)
        dqStr += String.fromCharCode(ch);
      else { // hex code
        if (ch<64 && (i+1>=bytes.length || (bytes[i+1]<48/*0*/ || bytes[i+1]>55/*7*/)))
          dqStr += "\\"+ch.toString(8/*octal*/); // quick compactness hack
        else
          dqStr += "\\x"+(ch+256).toString(16).substr(-2); // hex
      }
      lastCh = ch;
    }

    var finalStr = "";
    switch (fileType) {
      case "b64" :
        finalStr = 'atob("'+btoa(String.fromCharCode.apply(null, bytes))+'")';
        break;
      case "b64c" :
        finalStr = 'E.toString(require("heatshrink").decompress(atob("'+btoa(String.fromCharCode.apply(null, heatshrink.compress(bytes)))+'")))';
        break;
      case "b64b" :
        finalStr = 'var data = new Uint8Array('+bytes.length+');\r\n';
        var BSIZE = 128;
        for (var i=0;i<bytes.length;i+=BSIZE) {
          let d = btoa(String.fromCharCode.apply(null, bytes.slice(i,i+BSIZE)));
          finalStr += 'data.set(atob("'+d+'"),'+i+');\r\n';
        }
        break;
        case "b64f" :
          finalStr = 'var addr=....; // '+bytes.length+' bytes\n';
          var BSIZE = 256;
          for (var i=0;i<bytes.length;i+=BSIZE) {
            let d = btoa(String.fromCharCode.apply(null, bytes.slice(i,i+BSIZE)));
            finalStr += 'require("Flash").write(atob("'+d+'"), addr+'+i+');\r\n';
          }
          break;
      case "quoted" :
        finalStr = '"'+dqStr+'"';
        break;
      case "templated" :
        finalStr = '"'+tmpStr+'"';
        break;
      default: throw new Error("Unknown type!");
    }
    document.getElementById("size").innerText = finalStr.length+" Characters";
    document.getElementById("result").style.display = "";
    document.getElementById("result").value = finalStr;
  }
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?