0
0

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.

Onyxでu8を表示するときはcastしよう

Posted at

前回に続いてOnyxネタです。

u8 を表示すると数値そのものではなく、コードポイントとして解釈 されてしまいます1

main.onyx
use core {printf}

main :: () {
    v: u8 = 100;
    printf("{}\n", v);
}
結果
d

表示するときは u32 等の別の型にキャストしましょう。

main.onyx
printf("{}\n", cast(u32) v);
100

背景

OnyxはWASMをターゲットにしているため、プリミティブ型もWASMに準拠しています。用意されている数値型は以下の通りです。

  • u8
  • u16
  • u32
  • u64
  • i8
  • i16
  • i32
  • i64
  • f32
  • f64

いずれの場合も printf に対応していますが、 u8 の場合のみ d が表示されます。これは、 str 型が []u8 のエイリアスとして定義されているためです。
printf 上では、u8 は整数ではなく文字として扱われていたというわけです。

// NOTE: onyxでは型そのものも値(コンパイル時定数)として扱えるため表示可能
printf("{}\n", str); // [] u8
  1. この例のようにきれいに文字が出ればすぐ気づけたのですが、最初にハマった時は謎の文字化けが表示され途方に暮れていました...

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?