読者です 読者をやめる 読者になる 読者になる

labunix's blog

labunixのラボUnix

bashで8bit整数を10進,2進,8進,16進の順に並べてみる

■bashで8bit整数を10進,2進,8進,16進の順に並べてみる
 正負の0は同一とする。
 最左ビットが1の時はマイナスという条件を忘れていたので修正。

$ for n in `seq 0x00 0xff`;do \
    BIT=`echo "ibase=10;obase=2 ;$n" | bc`; \
    OCT=`echo "ibase=10;obase=8 ;$n" | bc`; \
    HEX=`echo "$n" | awk '{printf "%02x\n",$1}'`; \
    echo "$n,$BIT,$OCT,$HEX" | tr [A-Z] [a-z] | \
    awk -F\, '{printf "%04d,0b%08d,0o%03d,0x%02s\n",$1,$2,$3,$4}'
  done
0000,0b00000000,0o000,0x00
0001,0b00000001,0o001,0x01
0002,0b00000010,0o002,0x02
0003,0b00000011,0o003,0x03
0004,0b00000100,0o004,0x04
0005,0b00000101,0o005,0x05
0006,0b00000110,0o006,0x06
0007,0b00000111,0o007,0x07
0008,0b00001000,0o010,0x08
0009,0b00001001,0o011,0x09
0010,0b00001010,0o012,0x0a
0011,0b00001011,0o013,0x0b
0012,0b00001100,0o014,0x0c
0013,0b00001101,0o015,0x0d
0014,0b00001110,0o016,0x0e
0015,0b00001111,0o017,0x0f
0016,0b00010000,0o020,0x10
0017,0b00010001,0o021,0x11
0018,0b00010010,0o022,0x12
0019,0b00010011,0o023,0x13
0020,0b00010100,0o024,0x14
0021,0b00010101,0o025,0x15
0022,0b00010110,0o026,0x16
0023,0b00010111,0o027,0x17
0024,0b00011000,0o030,0x18
0025,0b00011001,0o031,0x19
0026,0b00011010,0o032,0x1a
0027,0b00011011,0o033,0x1b
0028,0b00011100,0o034,0x1c
0029,0b00011101,0o035,0x1d
0030,0b00011110,0o036,0x1e
0031,0b00011111,0o037,0x1f
0032,0b00100000,0o040,0x20
0033,0b00100001,0o041,0x21
0034,0b00100010,0o042,0x22
0035,0b00100011,0o043,0x23
0036,0b00100100,0o044,0x24
0037,0b00100101,0o045,0x25
0038,0b00100110,0o046,0x26
0039,0b00100111,0o047,0x27
0040,0b00101000,0o050,0x28
0041,0b00101001,0o051,0x29
0042,0b00101010,0o052,0x2a
0043,0b00101011,0o053,0x2b
0044,0b00101100,0o054,0x2c
0045,0b00101101,0o055,0x2d
0046,0b00101110,0o056,0x2e
0047,0b00101111,0o057,0x2f
0048,0b00110000,0o060,0x30
0049,0b00110001,0o061,0x31
0050,0b00110010,0o062,0x32
0051,0b00110011,0o063,0x33
0052,0b00110100,0o064,0x34
0053,0b00110101,0o065,0x35
0054,0b00110110,0o066,0x36
0055,0b00110111,0o067,0x37
0056,0b00111000,0o070,0x38
0057,0b00111001,0o071,0x39
0058,0b00111010,0o072,0x3a
0059,0b00111011,0o073,0x3b
0060,0b00111100,0o074,0x3c
0061,0b00111101,0o075,0x3d
0062,0b00111110,0o076,0x3e
0063,0b00111111,0o077,0x3f
0064,0b01000000,0o100,0x40
0065,0b01000001,0o101,0x41
0066,0b01000010,0o102,0x42
0067,0b01000011,0o103,0x43
0068,0b01000100,0o104,0x44
0069,0b01000101,0o105,0x45
0070,0b01000110,0o106,0x46
0071,0b01000111,0o107,0x47
0072,0b01001000,0o110,0x48
0073,0b01001001,0o111,0x49
0074,0b01001010,0o112,0x4a
0075,0b01001011,0o113,0x4b
0076,0b01001100,0o114,0x4c
0077,0b01001101,0o115,0x4d
0078,0b01001110,0o116,0x4e
0079,0b01001111,0o117,0x4f
0080,0b01010000,0o120,0x50
0081,0b01010001,0o121,0x51
0082,0b01010010,0o122,0x52
0083,0b01010011,0o123,0x53
0084,0b01010100,0o124,0x54
0085,0b01010101,0o125,0x55
0086,0b01010110,0o126,0x56
0087,0b01010111,0o127,0x57
0088,0b01011000,0o130,0x58
0089,0b01011001,0o131,0x59
0090,0b01011010,0o132,0x5a
0091,0b01011011,0o133,0x5b
0092,0b01011100,0o134,0x5c
0093,0b01011101,0o135,0x5d
0094,0b01011110,0o136,0x5e
0095,0b01011111,0o137,0x5f
0096,0b01100000,0o140,0x60
0097,0b01100001,0o141,0x61
0098,0b01100010,0o142,0x62
0099,0b01100011,0o143,0x63
0100,0b01100100,0o144,0x64
0101,0b01100101,0o145,0x65
0102,0b01100110,0o146,0x66
0103,0b01100111,0o147,0x67
0104,0b01101000,0o150,0x68
0105,0b01101001,0o151,0x69
0106,0b01101010,0o152,0x6a
0107,0b01101011,0o153,0x6b
0108,0b01101100,0o154,0x6c
0109,0b01101101,0o155,0x6d
0110,0b01101110,0o156,0x6e
0111,0b01101111,0o157,0x6f
0112,0b01110000,0o160,0x70
0113,0b01110001,0o161,0x71
0114,0b01110010,0o162,0x72
0115,0b01110011,0o163,0x73
0116,0b01110100,0o164,0x74
0117,0b01110101,0o165,0x75
0118,0b01110110,0o166,0x76
0119,0b01110111,0o167,0x77
0120,0b01111000,0o170,0x78
0121,0b01111001,0o171,0x79
0122,0b01111010,0o172,0x7a
0123,0b01111011,0o173,0x7b
0124,0b01111100,0o174,0x7c
0125,0b01111101,0o175,0x7d
0126,0b01111110,0o176,0x7e
0127,0b01111111,0o177,0x7f
0128,0b10000000,0o200,0x80
0129,0b10000001,0o201,0x81
0130,0b10000010,0o202,0x82
0131,0b10000011,0o203,0x83
0132,0b10000100,0o204,0x84
0133,0b10000101,0o205,0x85
0134,0b10000110,0o206,0x86
0135,0b10000111,0o207,0x87
0136,0b10001000,0o210,0x88
0137,0b10001001,0o211,0x89
0138,0b10001010,0o212,0x8a
0139,0b10001011,0o213,0x8b
0140,0b10001100,0o214,0x8c
0141,0b10001101,0o215,0x8d
0142,0b10001110,0o216,0x8e
0143,0b10001111,0o217,0x8f
0144,0b10010000,0o220,0x90
0145,0b10010001,0o221,0x91
0146,0b10010010,0o222,0x92
0147,0b10010011,0o223,0x93
0148,0b10010100,0o224,0x94
0149,0b10010101,0o225,0x95
0150,0b10010110,0o226,0x96
0151,0b10010111,0o227,0x97
0152,0b10011000,0o230,0x98
0153,0b10011001,0o231,0x99
0154,0b10011010,0o232,0x9a
0155,0b10011011,0o233,0x9b
0156,0b10011100,0o234,0x9c
0157,0b10011101,0o235,0x9d
0158,0b10011110,0o236,0x9e
0159,0b10011111,0o237,0x9f
0160,0b10100000,0o240,0xa0
0161,0b10100001,0o241,0xa1
0162,0b10100010,0o242,0xa2
0163,0b10100011,0o243,0xa3
0164,0b10100100,0o244,0xa4
0165,0b10100101,0o245,0xa5
0166,0b10100110,0o246,0xa6
0167,0b10100111,0o247,0xa7
0168,0b10101000,0o250,0xa8
0169,0b10101001,0o251,0xa9
0170,0b10101010,0o252,0xaa
0171,0b10101011,0o253,0xab
0172,0b10101100,0o254,0xac
0173,0b10101101,0o255,0xad
0174,0b10101110,0o256,0xae
0175,0b10101111,0o257,0xaf
0176,0b10110000,0o260,0xb0
0177,0b10110001,0o261,0xb1
0178,0b10110010,0o262,0xb2
0179,0b10110011,0o263,0xb3
0180,0b10110100,0o264,0xb4
0181,0b10110101,0o265,0xb5
0182,0b10110110,0o266,0xb6
0183,0b10110111,0o267,0xb7
0184,0b10111000,0o270,0xb8
0185,0b10111001,0o271,0xb9
0186,0b10111010,0o272,0xba
0187,0b10111011,0o273,0xbb
0188,0b10111100,0o274,0xbc
0189,0b10111101,0o275,0xbd
0190,0b10111110,0o276,0xbe
0191,0b10111111,0o277,0xbf
0192,0b11000000,0o300,0xc0
0193,0b11000001,0o301,0xc1
0194,0b11000010,0o302,0xc2
0195,0b11000011,0o303,0xc3
0196,0b11000100,0o304,0xc4
0197,0b11000101,0o305,0xc5
0198,0b11000110,0o306,0xc6
0199,0b11000111,0o307,0xc7
0200,0b11001000,0o310,0xc8
0201,0b11001001,0o311,0xc9
0202,0b11001010,0o312,0xca
0203,0b11001011,0o313,0xcb
0204,0b11001100,0o314,0xcc
0205,0b11001101,0o315,0xcd
0206,0b11001110,0o316,0xce
0207,0b11001111,0o317,0xcf
0208,0b11010000,0o320,0xd0
0209,0b11010001,0o321,0xd1
0210,0b11010010,0o322,0xd2
0211,0b11010011,0o323,0xd3
0212,0b11010100,0o324,0xd4
0213,0b11010101,0o325,0xd5
0214,0b11010110,0o326,0xd6
0215,0b11010111,0o327,0xd7
0216,0b11011000,0o330,0xd8
0217,0b11011001,0o331,0xd9
0218,0b11011010,0o332,0xda
0219,0b11011011,0o333,0xdb
0220,0b11011100,0o334,0xdc
0221,0b11011101,0o335,0xdd
0222,0b11011110,0o336,0xde
0223,0b11011111,0o337,0xdf
0224,0b11100000,0o340,0xe0
0225,0b11100001,0o341,0xe1
0226,0b11100010,0o342,0xe2
0227,0b11100011,0o343,0xe3
0228,0b11100100,0o344,0xe4
0229,0b11100101,0o345,0xe5
0230,0b11100110,0o346,0xe6
0231,0b11100111,0o347,0xe7
0232,0b11101000,0o350,0xe8
0233,0b11101001,0o351,0xe9
0234,0b11101010,0o352,0xea
0235,0b11101011,0o353,0xeb
0236,0b11101100,0o354,0xec
0237,0b11101101,0o355,0xed
0238,0b11101110,0o356,0xee
0239,0b11101111,0o357,0xef
0240,0b11110000,0o360,0xf0
0241,0b11110001,0o361,0xf1
0242,0b11110010,0o362,0xf2
0243,0b11110011,0o363,0xf3
0244,0b11110100,0o364,0xf4
0245,0b11110101,0o365,0xf5
0246,0b11110110,0o366,0xf6
0247,0b11110111,0o367,0xf7
0248,0b11111000,0o370,0xf8
0249,0b11111001,0o371,0xf9
0250,0b11111010,0o372,0xfa
0251,0b11111011,0o373,0xfb
0252,0b11111100,0o374,0xfc
0253,0b11111101,0o375,0xfd
0254,0b11111110,0o376,0xfe
0255,0b11111111,0o377,0xff

$ for n in `seq 0x80 0xff` `seq 0x00 0x7f`;do \
    NUM=$n; \
    BIT=`echo "ibase=10;obase=2 ;$n" | bc`; \
    OCT=`echo "ibase=10;obase=8 ;$n" | bc`; \
    HEX=`echo "$n" | awk '{printf "%02x\n",$1}'`; \

    if [ "$n" -ge "128" ];then
          let NUM="$n-256"
          OCT=`echo "ibase=10;obase=8 ;$n+256" | bc`; \
    fi

    echo "$NUM,$BIT,$OCT,$HEX" | tr [A-Z] [a-z] | \
    awk -F\, '{printf "%04d,0b%08d,0o%03d,0x%02s\n",$1,$2,$3,$4}'; \
   done | sed s/"^0"/" "/g
-128,0b10000000,0o600,0x80
-127,0b10000001,0o601,0x81
-126,0b10000010,0o602,0x82
-125,0b10000011,0o603,0x83
-124,0b10000100,0o604,0x84
-123,0b10000101,0o605,0x85
-122,0b10000110,0o606,0x86
-121,0b10000111,0o607,0x87
-120,0b10001000,0o610,0x88
-119,0b10001001,0o611,0x89
-118,0b10001010,0o612,0x8a
-117,0b10001011,0o613,0x8b
-116,0b10001100,0o614,0x8c
-115,0b10001101,0o615,0x8d
-114,0b10001110,0o616,0x8e
-113,0b10001111,0o617,0x8f
-112,0b10010000,0o620,0x90
-111,0b10010001,0o621,0x91
-110,0b10010010,0o622,0x92
-109,0b10010011,0o623,0x93
-108,0b10010100,0o624,0x94
-107,0b10010101,0o625,0x95
-106,0b10010110,0o626,0x96
-105,0b10010111,0o627,0x97
-104,0b10011000,0o630,0x98
-103,0b10011001,0o631,0x99
-102,0b10011010,0o632,0x9a
-101,0b10011011,0o633,0x9b
-100,0b10011100,0o634,0x9c
-099,0b10011101,0o635,0x9d
-098,0b10011110,0o636,0x9e
-097,0b10011111,0o637,0x9f
-096,0b10100000,0o640,0xa0
-095,0b10100001,0o641,0xa1
-094,0b10100010,0o642,0xa2
-093,0b10100011,0o643,0xa3
-092,0b10100100,0o644,0xa4
-091,0b10100101,0o645,0xa5
-090,0b10100110,0o646,0xa6
-089,0b10100111,0o647,0xa7
-088,0b10101000,0o650,0xa8
-087,0b10101001,0o651,0xa9
-086,0b10101010,0o652,0xaa
-085,0b10101011,0o653,0xab
-084,0b10101100,0o654,0xac
-083,0b10101101,0o655,0xad
-082,0b10101110,0o656,0xae
-081,0b10101111,0o657,0xaf
-080,0b10110000,0o660,0xb0
-079,0b10110001,0o661,0xb1
-078,0b10110010,0o662,0xb2
-077,0b10110011,0o663,0xb3
-076,0b10110100,0o664,0xb4
-075,0b10110101,0o665,0xb5
-074,0b10110110,0o666,0xb6
-073,0b10110111,0o667,0xb7
-072,0b10111000,0o670,0xb8
-071,0b10111001,0o671,0xb9
-070,0b10111010,0o672,0xba
-069,0b10111011,0o673,0xbb
-068,0b10111100,0o674,0xbc
-067,0b10111101,0o675,0xbd
-066,0b10111110,0o676,0xbe
-065,0b10111111,0o677,0xbf
-064,0b11000000,0o700,0xc0
-063,0b11000001,0o701,0xc1
-062,0b11000010,0o702,0xc2
-061,0b11000011,0o703,0xc3
-060,0b11000100,0o704,0xc4
-059,0b11000101,0o705,0xc5
-058,0b11000110,0o706,0xc6
-057,0b11000111,0o707,0xc7
-056,0b11001000,0o710,0xc8
-055,0b11001001,0o711,0xc9
-054,0b11001010,0o712,0xca
-053,0b11001011,0o713,0xcb
-052,0b11001100,0o714,0xcc
-051,0b11001101,0o715,0xcd
-050,0b11001110,0o716,0xce
-049,0b11001111,0o717,0xcf
-048,0b11010000,0o720,0xd0
-047,0b11010001,0o721,0xd1
-046,0b11010010,0o722,0xd2
-045,0b11010011,0o723,0xd3
-044,0b11010100,0o724,0xd4
-043,0b11010101,0o725,0xd5
-042,0b11010110,0o726,0xd6
-041,0b11010111,0o727,0xd7
-040,0b11011000,0o730,0xd8
-039,0b11011001,0o731,0xd9
-038,0b11011010,0o732,0xda
-037,0b11011011,0o733,0xdb
-036,0b11011100,0o734,0xdc
-035,0b11011101,0o735,0xdd
-034,0b11011110,0o736,0xde
-033,0b11011111,0o737,0xdf
-032,0b11100000,0o740,0xe0
-031,0b11100001,0o741,0xe1
-030,0b11100010,0o742,0xe2
-029,0b11100011,0o743,0xe3
-028,0b11100100,0o744,0xe4
-027,0b11100101,0o745,0xe5
-026,0b11100110,0o746,0xe6
-025,0b11100111,0o747,0xe7
-024,0b11101000,0o750,0xe8
-023,0b11101001,0o751,0xe9
-022,0b11101010,0o752,0xea
-021,0b11101011,0o753,0xeb
-020,0b11101100,0o754,0xec
-019,0b11101101,0o755,0xed
-018,0b11101110,0o756,0xee
-017,0b11101111,0o757,0xef
-016,0b11110000,0o760,0xf0
-015,0b11110001,0o761,0xf1
-014,0b11110010,0o762,0xf2
-013,0b11110011,0o763,0xf3
-012,0b11110100,0o764,0xf4
-011,0b11110101,0o765,0xf5
-010,0b11110110,0o766,0xf6
-009,0b11110111,0o767,0xf7
-008,0b11111000,0o770,0xf8
-007,0b11111001,0o771,0xf9
-006,0b11111010,0o772,0xfa
-005,0b11111011,0o773,0xfb
-004,0b11111100,0o774,0xfc
-003,0b11111101,0o775,0xfd
-002,0b11111110,0o776,0xfe
-001,0b11111111,0o777,0xff
 000,0b00000000,0o000,0x00
 001,0b00000001,0o001,0x01
 002,0b00000010,0o002,0x02
 003,0b00000011,0o003,0x03
 004,0b00000100,0o004,0x04
 005,0b00000101,0o005,0x05
 006,0b00000110,0o006,0x06
 007,0b00000111,0o007,0x07
 008,0b00001000,0o010,0x08
 009,0b00001001,0o011,0x09
 010,0b00001010,0o012,0x0a
 011,0b00001011,0o013,0x0b
 012,0b00001100,0o014,0x0c
 013,0b00001101,0o015,0x0d
 014,0b00001110,0o016,0x0e
 015,0b00001111,0o017,0x0f
 016,0b00010000,0o020,0x10
 017,0b00010001,0o021,0x11
 018,0b00010010,0o022,0x12
 019,0b00010011,0o023,0x13
 020,0b00010100,0o024,0x14
 021,0b00010101,0o025,0x15
 022,0b00010110,0o026,0x16
 023,0b00010111,0o027,0x17
 024,0b00011000,0o030,0x18
 025,0b00011001,0o031,0x19
 026,0b00011010,0o032,0x1a
 027,0b00011011,0o033,0x1b
 028,0b00011100,0o034,0x1c
 029,0b00011101,0o035,0x1d
 030,0b00011110,0o036,0x1e
 031,0b00011111,0o037,0x1f
 032,0b00100000,0o040,0x20
 033,0b00100001,0o041,0x21
 034,0b00100010,0o042,0x22
 035,0b00100011,0o043,0x23
 036,0b00100100,0o044,0x24
 037,0b00100101,0o045,0x25
 038,0b00100110,0o046,0x26
 039,0b00100111,0o047,0x27
 040,0b00101000,0o050,0x28
 041,0b00101001,0o051,0x29
 042,0b00101010,0o052,0x2a
 043,0b00101011,0o053,0x2b
 044,0b00101100,0o054,0x2c
 045,0b00101101,0o055,0x2d
 046,0b00101110,0o056,0x2e
 047,0b00101111,0o057,0x2f
 048,0b00110000,0o060,0x30
 049,0b00110001,0o061,0x31
 050,0b00110010,0o062,0x32
 051,0b00110011,0o063,0x33
 052,0b00110100,0o064,0x34
 053,0b00110101,0o065,0x35
 054,0b00110110,0o066,0x36
 055,0b00110111,0o067,0x37
 056,0b00111000,0o070,0x38
 057,0b00111001,0o071,0x39
 058,0b00111010,0o072,0x3a
 059,0b00111011,0o073,0x3b
 060,0b00111100,0o074,0x3c
 061,0b00111101,0o075,0x3d
 062,0b00111110,0o076,0x3e
 063,0b00111111,0o077,0x3f
 064,0b01000000,0o100,0x40
 065,0b01000001,0o101,0x41
 066,0b01000010,0o102,0x42
 067,0b01000011,0o103,0x43
 068,0b01000100,0o104,0x44
 069,0b01000101,0o105,0x45
 070,0b01000110,0o106,0x46
 071,0b01000111,0o107,0x47
 072,0b01001000,0o110,0x48
 073,0b01001001,0o111,0x49
 074,0b01001010,0o112,0x4a
 075,0b01001011,0o113,0x4b
 076,0b01001100,0o114,0x4c
 077,0b01001101,0o115,0x4d
 078,0b01001110,0o116,0x4e
 079,0b01001111,0o117,0x4f
 080,0b01010000,0o120,0x50
 081,0b01010001,0o121,0x51
 082,0b01010010,0o122,0x52
 083,0b01010011,0o123,0x53
 084,0b01010100,0o124,0x54
 085,0b01010101,0o125,0x55
 086,0b01010110,0o126,0x56
 087,0b01010111,0o127,0x57
 088,0b01011000,0o130,0x58
 089,0b01011001,0o131,0x59
 090,0b01011010,0o132,0x5a
 091,0b01011011,0o133,0x5b
 092,0b01011100,0o134,0x5c
 093,0b01011101,0o135,0x5d
 094,0b01011110,0o136,0x5e
 095,0b01011111,0o137,0x5f
 096,0b01100000,0o140,0x60
 097,0b01100001,0o141,0x61
 098,0b01100010,0o142,0x62
 099,0b01100011,0o143,0x63
 100,0b01100100,0o144,0x64
 101,0b01100101,0o145,0x65
 102,0b01100110,0o146,0x66
 103,0b01100111,0o147,0x67
 104,0b01101000,0o150,0x68
 105,0b01101001,0o151,0x69
 106,0b01101010,0o152,0x6a
 107,0b01101011,0o153,0x6b
 108,0b01101100,0o154,0x6c
 109,0b01101101,0o155,0x6d
 110,0b01101110,0o156,0x6e
 111,0b01101111,0o157,0x6f
 112,0b01110000,0o160,0x70
 113,0b01110001,0o161,0x71
 114,0b01110010,0o162,0x72
 115,0b01110011,0o163,0x73
 116,0b01110100,0o164,0x74
 117,0b01110101,0o165,0x75
 118,0b01110110,0o166,0x76
 119,0b01110111,0o167,0x77
 120,0b01111000,0o170,0x78
 121,0b01111001,0o171,0x79
 122,0b01111010,0o172,0x7a
 123,0b01111011,0o173,0x7b
 124,0b01111100,0o174,0x7c
 125,0b01111101,0o175,0x7d
 126,0b01111110,0o176,0x7e
 127,0b01111111,0o177,0x7f

$ for n in `seq 0x80 0xff` `seq 0x00 0x7f`;do \
    NUM=$n; \
    BIT=`echo "ibase=10;obase=2 ;$n" | bc`; \
    OCT=`echo "ibase=10;obase=8 ;$n" | bc`; \
    HEX=`echo "$n" | awk '{printf "%02x\n",$1}'`; \
    if [ "$n" -ge "128" ];then \
      let NUM="$n-256"; \
      OCT=`echo "ibase=10;obase=8 ;$n+256" | bc`; \
    fi; \
    echo "$NUM,$BIT,$OCT,$HEX" | tr [A-Z] [a-z] | \
    awk -F\, '{printf "%04d,0b%08d,0o%03d,0x%02s\n",$1,$2,$3,$4}'; \
   done | sed s/"^0"/" "/g | grep "^[- ]127"
-127,0b10000001,0o601,0x81
 127,0b01111111,0o177,0x7f

$ echo $(((2#10000001+2#01111111)%256))
0
$ echo $(((8#601+8#177)%256))
0
$ echo $(((16#81+16#7f)%256))
0