Using Windows, CodeBlocks 17.12, GCC 5.1 .
I have a file that I created in C++ and I added a byte order mark to the start of the file.
I read from that file and I am able to read the first byte of that file giving me the first part of that BOM (which I know is there since I put it there).
I would like to place what I read into a std::string to ALSO show up on my CLI output.
Like this:
std::string SS22;
// Get the SS22
// Output the SS22.
I do not just want an output. I want it to be from a std::string.
Here is my code:
Here is my current output via CLI.
Reading Some_utf8_Text_ With_ByteOrderM ark.txt
Contents are: [Byte Order Mark BOM] then [hello - πüôπéôπü½πü íπü» - abc]
The BOM is an unsigned char BOM01[3]{ 0xef, 0xbb, 0xbf }
So, I should see as the first byte: [0xef]
Now testing to get the Hex of the first byte in two different ways:
test 1 Hex = [ef]
test 2 Hex = [ef]
Attempting to convert the first byte to binary:
test 3 Binary = [11110111]
That should have been [11110111]
Now how do I place that in a std::string?
Click the Exit button.
I got the ef which is correct.
I got the 11110111 which is correct.
How do I put the ef into a std::string and then output it to the CLI?
How do I put the 11110111 into a std::string and then output it to the CLI?
Thank you.
What am I doing wrong in the code?
What the currect code to use for this?
Thank you.
Later, I plan to convert the second string from a std::string to binary and save it to a new file and see if I can read that new file.
Thank you.
I have a file that I created in C++ and I added a byte order mark to the start of the file.
I read from that file and I am able to read the first byte of that file giving me the first part of that BOM (which I know is there since I put it there).
I would like to place what I read into a std::string to ALSO show up on my CLI output.
Like this:
std::string SS22;
// Get the SS22
// Output the SS22.
I do not just want an output. I want it to be from a std::string.
Here is my code:
Code:
#ifndef _UNICODE #define _UNICODE // used by Windows headers, #endif // _UNICODE #ifndef UNICODE #define UNICODE // used by C-runtime/MFC headers. #endif // UNICODE #include <stdio.h> #include <iostream> #include <cstddef> #include <bitset> #include <vector> #include <iostream> #include <cstdio> #include <fstream> #include <sstream> #include <iomanip> #include <string.h> //#include <locale> using namespace std; // Since I am using C++11 and I want to use std::byte using namespace std; // Since I am using C++11 and I want to use std::byte namespace std { // define std::byte enum class byte : unsigned char {}; }; int main() { std::string FileName; FileName = "Some_utf8_Text_With_ByteOrderMark.txt"; std::cout << "\n\n\nReading " << FileName << "\n"; std::cout << "Contents are: [Byte Order Mark BOM] then [hello - こんにちは - abc]\n"; std::cout << "The BOM is an unsigned char BOM01[3]{ 0xef, 0xbb, 0xbf }\n"; std::cout << "So, I should see as the first byte: [0xef]\n\n"; const char * CCP_FileName = FileName.c_str(); std::FILE *fp; unsigned char data; std::ifstream("VerifiedToHave_utf8_ByteOrderMark03.txt"); fp = std::fopen("VerifiedToHave_utf8_ByteOrderMark03.txt", "r"); fread(&data, 1, 1, fp); fclose(fp); //The BOM is an unsigned char BOM01[3]{ 0xef, 0xbb, 0xbf } std::cout << "Now testing to get the Hex of the first byte in two different ways:\n"; std::cout << " test 1 Hex = [" << std::hex << static_cast<int>(static_cast<unsigned char>(data)) << "]\n"; // [ef] std::cout << " test 2 Hex = [" << std::hex << static_cast<unsigned int>(static_cast<unsigned char>(data)) << "]\n"; // [ef] // Hex ef = binary 11101111 // Hex bb = binary 10111011 // Hex bf = binary 10111111 std::cout << "Attempting to convert the first byte to binary:\n"; std::cout << " test 3 Binary = ["; bool BolBit[8]; // fill data for(int i = 0; i < 8; i++) { BolBit[i] = ((data >> i) & 0x01); std::cout << BolBit[i]; } std::cout << "]\nThat should have been [11110111]\n\n"; // 11110111 std::cout << "Now how do I place that in a std::string?\n\n"; // I tried this but it gives me an error that I have not managed to fix. // char s24[sizeof( data ) + sizeof( ( char )'\0' )] = { '\0' }; // // memcpy (void*, const void*, size_t); // // memcpy( s24, data, sizeof( data ) ); // // error: invalid conversion from 'unsigned char' to 'const void*' [-fpermissive] // // // puts( s24 ); cin.clear(); cout << "Click the Exit button.\n"; string s; cin >> s; }
Here is my current output via CLI.
Reading Some_utf8_Text_ With_ByteOrderM ark.txt
Contents are: [Byte Order Mark BOM] then [hello - πüôπéôπü½πü íπü» - abc]
The BOM is an unsigned char BOM01[3]{ 0xef, 0xbb, 0xbf }
So, I should see as the first byte: [0xef]
Now testing to get the Hex of the first byte in two different ways:
test 1 Hex = [ef]
test 2 Hex = [ef]
Attempting to convert the first byte to binary:
test 3 Binary = [11110111]
That should have been [11110111]
Now how do I place that in a std::string?
Click the Exit button.
I got the ef which is correct.
I got the 11110111 which is correct.
How do I put the ef into a std::string and then output it to the CLI?
How do I put the 11110111 into a std::string and then output it to the CLI?
Thank you.
What am I doing wrong in the code?
What the currect code to use for this?
Thank you.
Later, I plan to convert the second string from a std::string to binary and save it to a new file and see if I can read that new file.
Thank you.