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 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > 16進表記をchar型に変換する > int val = StrToInt(L"$" + src); ch = Char(val);

Last updated at Posted at 2015-09-02
動作確認
C++ Builder XE4
Rad Studio 10.2 Tokyo Update 2 (追記: 2017/12/26)

16進数での表記(030など)をchar型に変換したい。
ASCIIコード表

以下を参考にした。
SO

実装してみたのが以下。

static AnsiString getChar(String src)
{
	src = src.Trim(); // remove L" "
	char ch;

	if (src.Length() > 0) {
		int val = StrToInt(L"$" + src);
		ch = Char(val);
		return AnsiString(ch);
	} else {
		return L"";
	}
}

void __fastcall TForm1::Test_getChar()
{
	String rcvd;
	AnsiString ascii = L"";

	rcvd = L"30 ";
	ascii = ascii + getChar(rcvd);
	rcvd = L"35 ";
	ascii = ascii + getChar(rcvd);
	rcvd = L"65 ";
	ascii = ascii + getChar(rcvd);

	Memo1->Lines->Add(ascii); // "05e"が追加される

}

引っかかったのは30 のように文字列の最後に0x20が入った時、そのままStrToInt()で処理しようとしたらエラーが出たこと。
Trim()にて0x20を取り除くようにすれば動作するようになった。

上記は非表示文字(エスケープ文字? コントロール文字?)については未対応。

AnsiStringで処理しているのは"-"という文字に対応する16進数をUnicodeStringで処理した時に文字化けのようになったことから。このあたりは勉強不足。

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?