LoginSignup
0
1

More than 5 years have passed since last update.

charでは0x0がNULL扱いされるのでunsigned charを使う

Last updated at Posted at 2017-05-18
  • 表題のコード
char str[] = {0x61, 0x62, 0x0 ,0x0 ,0x0 ,0x0 ,0x0 ,0x7a};
std::string s2(str);
cout << s2 << endl;

unsigned char u8[] = {0x61, 0x62, 0x0 ,0x0 ,0x0 ,0x0 ,0x0 ,0x7a};
std::string txt(u8,u8+8);
cout << txt << endl;
  • 出力
ab
abz

評価コード全体

#include <iostream>
#include <sstream>
#include <bitset>
#include <string>
#include <climits>
using namespace std;


const char* hex_char_to_bin(char c)
{
    // TODO handle default / error
    switch(toupper(c))
    {
        case '0': return "0000";
        case '1': return "0001";
        case '2': return "0010";
        case '3': return "0011";
        case '4': return "0100";
        case '5': return "0101";
        case '6': return "0110";
        case '7': return "0111";
        case '8': return "1000";
        case '9': return "1001";
        case 'A': return "1010";
        case 'B': return "1011";
        case 'C': return "1100";
        case 'D': return "1101";
        case 'E': return "1110";
        case 'F': return "1111";
    }
}

std::string hex_str_to_bin_str(const std::string& hex)
{
    // TODO use a loop from <algorithm> or smth
    std::string bin;
    for(size_t i = 0; i != hex.length(); ++i)
       bin += hex_char_to_bin(hex[i]);
    return bin;
}

int main() {
    char c = 123;
    cout << hex << int(c) << endl;

    string s = "0x7b";
    stringstream ss;
    ss << hex << s;
    unsigned n;
    ss >> n;
    bitset<8> b(n);
    cout <<b.to_string() << endl;

    cout << hex_str_to_bin_str("7b") << endl;


    for(char c = 'a'; c <= 'z'; c++){
        std::bitset<sizeof(char) * CHAR_BIT> binary(c); //sizeof() returns bytes, not bits!
        std::cout << "Letter: " << c << "\t";
        std::cout << "Hex: " << std::hex << (int)c << "\t";
        std::cout << "Binary: " << binary << std::endl;
    }

    int x = 123;

    const char * xx = reinterpret_cast<const char*>(&x);
    cout << xx << endl;

    char str[] = {0x61, 0x62, 0x0 ,0x0 ,0x0 ,0x0 ,0x0 ,0x7a};
    std::string s2(str);
    cout << s2 << endl;

    unsigned char u8[] = {0x61, 0x62, 0x0 ,0x0 ,0x0 ,0x0 ,0x0 ,0x7a};
    std::string txt(u8,u8+8);
    cout << txt << endl;

    return 0;
}

評価コード出力

7b
01111011
01111011
Letter: a   Hex: 61 Binary: 01100001
Letter: b   Hex: 62 Binary: 01100010
Letter: c   Hex: 63 Binary: 01100011
Letter: d   Hex: 64 Binary: 01100100
Letter: e   Hex: 65 Binary: 01100101
Letter: f   Hex: 66 Binary: 01100110
Letter: g   Hex: 67 Binary: 01100111
Letter: h   Hex: 68 Binary: 01101000
Letter: i   Hex: 69 Binary: 01101001
Letter: j   Hex: 6a Binary: 01101010
Letter: k   Hex: 6b Binary: 01101011
Letter: l   Hex: 6c Binary: 01101100
Letter: m   Hex: 6d Binary: 01101101
Letter: n   Hex: 6e Binary: 01101110
Letter: o   Hex: 6f Binary: 01101111
Letter: p   Hex: 70 Binary: 01110000
Letter: q   Hex: 71 Binary: 01110001
Letter: r   Hex: 72 Binary: 01110010
Letter: s   Hex: 73 Binary: 01110011
Letter: t   Hex: 74 Binary: 01110100
Letter: u   Hex: 75 Binary: 01110101
Letter: v   Hex: 76 Binary: 01110110
Letter: w   Hex: 77 Binary: 01110111
Letter: x   Hex: 78 Binary: 01111000
Letter: y   Hex: 79 Binary: 01111001
Letter: z   Hex: 7a Binary: 01111010
{
ab
abz
0
1
1

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
1