STM32 Nucleo(mbed互換ボード)について

blog.cloudninja.asia

mbedを始めましょう!("Let's get started!" in Japanese) | mbed


手元にあるボードは Coretex-M4 80MHz, SRAM 128kB, Flash 1MBというシロモノ。
STM32L476RG Ultra-low-power with FPU ARM Cortex-M4 MCU 80 MHz with 1 Mbyte Flash, LCD, USB OTG - STMicroelectronics

STM32シリーズが多すぎて型番ごとにどう違うかよくわからんと思ったら、見やすい図があった
http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/LN1847?sc=stm32nucleo

Javaでバイト列をgzip/gunzipしてみる

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;


class Test {
    public static void main(String[] args) {
        try {
            String s = "abc";
            byte[] gz = gzip(s.getBytes());
            byte[] bytes = gunzip(gz);
            String s2 = new String(bytes);

            System.out.println(s2);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    private static byte[] gzip(byte[] bytes) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip_out = new GZIPOutputStream(out);
        gzip_out.write(bytes);
        gzip_out.close();
        out.close();
        byte[] ret = out.toByteArray();
        return ret;
    }
    
    private static byte[] gunzip(byte[] gzip_bytes) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(gzip_bytes);
        GZIPInputStream gzip_in = new GZIPInputStream(in);
        int len;
        byte[] buffer = new byte[1024];
        while ((len = gzip_in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }       
        gzip_in.close();
        in.close();
        out.close();
        byte[] ret = out.toByteArray();
        return ret;
    }
}

JavaScriptで文字列のUTF-8バイト数を得る関数を書く

UnicodeのコードポイントとUTF-8 エンコードのバイト列の対照表

 Code Points            1st Byte  2nd Byte  3rd Byte  4th Byte

   U+0000..U+007F       00..7F
   U+0080..U+07FF       C2..DF    80..BF
   U+0800..U+0FFF       E0        A0..BF    80..BF
   U+1000..U+CFFF       E1..EC    80..BF    80..BF
   U+D000..U+D7FF       ED        80..9F    80..BF
   U+D800..U+DFFF       ******* ill-formed *******
   U+E000..U+FFFF       EE..EF    80..BF    80..BF
  U+10000..U+3FFFF      F0        90..BF    80..BF    80..BF
  U+40000..U+FFFFF      F1..F3    80..BF    80..BF    80..BF
 U+100000..U+10FFFF     F4        80..8F    80..BF    80..BF

上記は perlunicode - Perl における Unicode サポート - perldoc.jp から拾ったのだけど、最近の仕様と合っているのか The Unicode Consortium のドキュメントをあたってみた。

Table 3-7 の表が一致するので大丈夫と思う。

JavaScriptで文字列のUTF-8バイト数を得る関数

対照表から下記のように書ける。

function get_utf8_bytes(str) {
    var count = 0;
    for (var i = 0; i < str.length; ++i) {
        var cp = str.charCodeAt(i);

        if (cp <= 0x007F) {
            // U+0000 - U+007F
            count += 1;
        } else if (cp <= 0x07FF) {
            // U+0080 - U+07FF
            count += 2;
        } else if (cp <= 0xD7FF) {
            // U+0800 - U+D7FF
            count += 3;
        } else if (cp <= 0xDFFF) {
            // U+10000 - U+10FFFF
            //
            // 0xD800 - 0xDBFF (High Surrogates)
            // 0xDC00 - 0xDFFF (Low Surrogates)
            count += 2;
        } else if (cp <= 0xFFFF) {
            // U+E000 - U+FFFF
            count += 3;
        } else {
            // undefined code point in UTF-16
            // do nothing
        }
    }
    return count;
}