0
4

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.

[Apex] Blobと文字列の変換

Posted at

Blobは、バイナリデータを表すApexのプリミティブデータ型。
Base64は、データを64種類の英数字記号で表すエンコード方式。

String → Blob

// 文字列をBlob型にキャストする。
String str = 'Blobと文字列の変換 サンプル';
Blob b = Blob.valueOf(str);

// Base64でStringに変換されたバイナリデータをBlob型に戻す。
String encoded_64 = 'QmxvYuOBqOaWh+Wtl+WIl+OBruWkieaPm+OAgOOCteODs+ODl+ODqw==';
Blob blobData1 = EncodingUtil.base64Decode(encoded_64);

// 16進数のStringに変換されたバイナリデータをBlob型に戻す。
String encoded_16 = '426c6f62e381a8e69687e5ad97e58897e381aee5a489e68f9be38080e382b5e383b3e38397e383ab';
Blob blobData2 = EncodingUtil.convertFromHex(encoded_16);

Blob → String

// Blob型のデータを文字列にキャストする(文字コードがUTF-8でないとエラー)。
String str = inputBlob.toString();

// Blob型のバイナリデータをBase64でエンコードした文字列に変換する。
String encoded_64 = EncodingUtil.base64Encode(inputBlob);

// Blob型のバイナリデータを16進数にエンコードした文字列に変換する。
String encoded_16 = EncodingUtil.convertToHex(inputBlob);

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?