expand_number2008年04月10日 17時20分43秒

FreeBSD には expand_number という関数がある。

% man expand_number
NAME
     expand_number -- format a number from human readable form

LIBRARY
     System Utilities Library (libutil, -lutil)

SYNOPSIS
     #include 

     int
     expand_number(char *buf, int64_t *num);

DESCRIPTION
     The expand_number() function unformats the buf string and stores a signed
     64-bit quantity at address pointed out by the num argument.

     The expand_number() function follows the SI power of two convention.

     The prefixes are:

           Prefix    Description    Multiplier
           k         kilo           1024
           M         mega           1048576
           G         giga           1073741824
           T         tera           1099511627776
           P         peta           1125899906842624
           E         exa            1152921504606846976

SI 接頭辞を用いた文字列表記の数字を 64 ビット整数型に変換する。FreeBSD 6.3 から登場したらしい。

この様な変換はあちらこちらのプログラムで行なわれるので、似たようなコードが沢山散見される。今までライブラリになっていなかったのが不思議なくらいだ。

余談だが、比較的新しい geom のライブラリを使っている geom class はこれらの表記が使える。