Last active
October 22, 2016 12:31
-
-
Save KentaYamada/f6b9d78b4cf61cb8241a05b1fa4dd603 to your computer and use it in GitHub Desktop.
文字列からバイトデータに変換
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <stdexcept> | |
| #include <sstream> | |
| #include <string> | |
| #include <vector> | |
| typedef struct { | |
| std::string text; | |
| char delimiter; | |
| } data_t; | |
| std::vector<unsigned char> str2bytes(std::string &msg, const char delimiter) | |
| { | |
| std::vector<unsigned char> bytes; | |
| std::string text(""); | |
| std::istringstream iss(msg); | |
| while(getline(iss, text, delimiter)) { | |
| std::istringstream tmp(text); | |
| unsigned int num; | |
| tmp >> std::hex >> num; | |
| bytes.push_back(num); | |
| } | |
| return bytes; | |
| } | |
| int main() | |
| { | |
| std::vector<data_t> data; | |
| data.push_back({"1 a ff", ' '}); | |
| data.push_back({"2,b,fe", ','}); | |
| std::vector<data_t>::iterator itd; | |
| for(itd = data.begin(); itd != data.end(); ++itd) { | |
| std::vector<unsigned char> bytes = str2bytes(itd->text, itd->delimiter); | |
| std::vector<unsigned char>::iterator byte; | |
| for(byte = bytes.begin(); byte != bytes.end(); ++byte) { | |
| std::cout << ((int)*byte) << std::endl; | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment