※当サイトの記事には、広告・プロモーションが含まれます。

HEXについて、もうちょっと考えてみました

2019年も3月に入りましたね、花粉が飛散し放題のいまの日本に疑問を持つ、それが一番大事~、な訳はない、どうもボクです。

togetter.com

⇧  「なにもわからない」って言葉が、言葉狩りにあってる気が...

というか、Linuxの生みの親が、「ワタシハ リナックス チョットデキル」っていうTシャツを着るって...

www.excite.co.jp

⇧  「ダニング=クルーガー効果」って言葉を知ったのですが、その逆の言葉もあるらしいと。

で、何が言いたいかと言いますと、ありの~♪ ままの~♪ 姿を見せるのよ~♬、って難しいってことですかね。

のっけから、だいぶ脱線してしまいましたが、前回、Base64とかHEXについて調べたのですが、 

ts0818.hatenablog.com

⇧   モヤモヤ感が半端なかったと...

なので、HEXについてもうちょい調査してみました。

 

 

HEXエンコードとHEXデコード

何ていうか、そもそも、HEXエンコード、HEXデコードって言うけれど、一体どんなことをやっているんですか?な情弱なあたくしです。

そこで、原点回帰と言いますか、そも、HEX Stringと呼ばれるものって何ぞや?

By string of hexadecimal digits what they mean is a combination of the digits 0-9 and characters A-F, just like how a binary string is a combination of 0's and 1's. Eg: "245FC" is a hexadecimal string.

c - What is a string of hexadecimal digits? - Stack Overflow

stackoverflow.com

⇧  上記サイト様によりますと、16進数を表現する「0-9」「A-F」の組み合わせで表せられる文字列を、hexadecimal string、つまり、HEX Stringと言うそうな。

hexadecimal については、Wikipediaさんによりますと、

In mathematics and computinghexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols "0"–"9" to represent values zero to nine, and "A"–"F" (or alternatively "a"–"f") to represent values ten to fifteen.

Hexadecimal - Wikipedia

⇧  「base16」ないしは、「hex」とも呼ばれるとあり、利用できるものは「0123456789ABCDEF」 のいずれかであると。

Hexadecimal numerals are widely used by computer system designers and programmers, as they provide a more human-friendly representation of binary-coded values. Each hexadecimal digit represents four binary digits, also known as a nibble, which is half a byte.

Hexadecimal - Wikipedia

⇧  hexadecimal は、バイナリだと4桁になることから、1byteの半分であるnibbleと呼ばれるそうな。つまり、4bitってことですかね。

In computing, a nibble (occasionally nybble or nyble to match the spelling of byte) is a four-bit aggregation, or half an octet. It is also known as half-byte or tetrade.

In a networking or telecommunication context, the nibble is often called a semi-octet, quadbit, or quartet. A nibble has sixteen (24) possible values. A nibble can be represented by a single hexadecimal digit and called a hex digit.

Nibble - Wikipedia

というか、16進数は、何故に4bit?

ちなみに、

qiita.com

⇧  上記サイト様によりますと、byteって概念自体が結構新しいものらしいと。

すみません、脱線しました。16進数は、何故に4bit なのかでした。

2進数 10進数 16進数
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F

という「決め事」を事前にしておくことで、16進数のすべての数は、「0-9」「A-F」のうちの1つ、つまり1桁で表せる、かつ、すべて4bitで表せるというか(というか4bitでしか表せない)というところが重要であると。

 

なんで、hexadecimal なんて生まれたのか?

blog.livedoor.jp

Binary conversion

Most computers manipulate binary data, but it is difficult for humans to work with the large number of digits for even a relatively small binary number. Although most humans are familiar with the base 10 system, it is much easier to map binary to hexadecimal than to decimal because each hexadecimal digit maps to a whole number of bits (410).

Hexadecimal - Wikipedia

⇧  上記サイト様で仰られているように、hexadecimal が、4bitで表せることから、バイナリとマッピングがしやすいってことらしいと。

「2進数 → 10進数」で表現しようとするとしんどいけど、「2進数 → 16進数」だとシンプルにできると。

This example shows the conversion of a binary number to decimal, mapping each digit to the decimal value, and adding the results.

(01011110101101010010)2 = 26214410 + 6553610 + 3276810 + 1638410 + 819210 + 204810 + 51210 + 25610 + 6410 + 1610 + 210
  = 38792210

Compare this to the conversion to hexadecimal, where each group of four digits can be considered independently, and converted directly:

(01011110101101010010)2 = 0101  1110  1011  0101  00102
  = 5 E B 5 216
  = 5EB5216

The conversion from hexadecimal to binary is equally direct.

Hexadecimal - Wikipedia

⇧  「2進数 → 10進数」への変換だと、1桁づつ変換していかんとならんけども、「2進数 → 16進数」への変換は、4桁ずつで可能であると。

上の例でいうと、16進数の「5EB52」を、10進数にすると、

5×16414×16311×1625×1612×160 = 327680+57344+2816+80+2
=387922

となると。 

で、自分も勘違いしてたのですが、

> 16進数で0x1234ABCD
と言っている以上、文字ではなく数ですよね。

”A”という文字であれば1バイト(0x41)になるでしょうけど
16進数で”A”という数であれば4ビット(1010)で表現できるといえるでしょう。

#全角文字なら1文字2バイト、UNICODEなら4バイト・・・とかいう突っ込みはこの際置いとくことに。

16進数で0x1234ABCDというデータはなぜ4バイトなのですか?自分は... - Yahoo!知恵袋

detail.chiebukuro.yahoo.co.jp

⇧  Hex String は、文字列であるんだけれど、16進数の数として扱うという厄介な内容であると、というかStringの中身が、16進数かどうかなんてどうやって判断すりゃ良いんですかね?

何が言いたいかと言うと、「0A4F76DE」って文字列があったとして、普通の文字列として扱いたいのか、16進数表記の文字列として扱いたいのか、どう判断すりゃ良いのかってことですかね。

 

んで、結局、HEXエンコード、HEXデコードって何ぞや?

  • HEXエンコード
    • データを、16進数で表せる文字の集まりにする。
  • HEXデコード
    • 16進数で表せる文字の集まりに変換されていたデータを、変換前の状態に戻してあげる。

ってことですかね。 

 

というわけで、HEX String

では、実際にHEXエンコード、HEXデコードを試してみたいと思います。 

memorynotfound.com

⇧  上記サイト様を参考に試してみました。

Eclipseを起動し、適当なJavaプロジェクトを作成し、クラスも作成。 

f:id:ts0818:20190302163335p:plain

こんな感じのソースで。

import java.math.BigInteger;

public class HexTest02 {

	public static void main(String[] args) {
		// 文字列を用意
		String str01 = "403191084928812";
		String str02 = "AJNWQAHDNXZMP==OP#&E";
		StringBuffer bf = new StringBuffer();
		bf.append(str01);
		bf.append(str02);

		String strToHex = toHexString(bf.toString());
		String strFromHex = fromHexString(strToHex);
		System.out.println("文字列:" + bf.toString());
		System.out.println("HEXエンコード:" + strToHex + "\nバイト数:" + ((strToHex.getBytes().length)/2));
		System.out.println("HEXデコード:" + strFromHex + "\nバイト数:" + strFromHex.getBytes().length);

	}

	// HEXエンコード
    public static String toHexString(String input) {
        return String.format("%x", new BigInteger(1, input.getBytes()));
    }

    // HEXデコード
    public static String fromHexString(String hex) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < hex.length(); i+=2) {
            str.append((char) Integer.parseInt(hex.substring(i, i + 2), 16));
        }
        return str.toString();
    }
}

んで、実行してみます。

f:id:ts0818:20190302163729p:plain

f:id:ts0818:20190302163829p:plain

⇧  やっぱり、HEXエンコードされた文字列の内容を見ても、それが、16進数としてあつかうべきなのかどうかは判断できんですかね...「0-9」「A-F」しか使われてないことは分かるけども...

う~ん、今回もスッキリしない感じですが...

今回はこのへんで。