I need to read and write a hex encoded SHA1 value in the file.
If I wanted to write C30A then I need to convert C30A into character array whose length is equal to half the length of the hashvalue since a byte can hold two hex characters;
for C30A,
char array[2];
array[0] = 11000011=195;
array[1] = 00001010=10;//which is an ascii for '\n'
If I wanted to write this char array
I have done: filestream<<array;
Now when I want to read it from file how should I read it?
I tried doing
std::string str;
filestream>>str;
But it stops whenever it encounters whitespace characters like '\0','\r','\n' etc.
And It always ends up reading only a portion of the hash, since there will be those character for sure.
What way should I go to read and write in this case?
If there is nothing else in the file, know that a SHA1 digest is 160 bits, or 20 bytes:
string sha1(20, '\0');
filestream.read(sha1.data(), sha1.size());
Related
I have an image which I have to convert to base64. After the conversion, below is its value:
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAGQASwDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1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UWQr+77+lWtM8PqQCqZ49K7fwR4cy6lo+/pXl1KsuXc9ujTXOtCz4X8ErhQYxz7V3+g+CowFyo/Krvh7w7FHGjMvYdq7DR9KhTbkfpXnzqStuexCnG+xX8O+D4omVtoruNJ0KNYlUAcCq2mW1ugABrdsljVRzXG6k+51+zhbYkXw/DLEAzD8qik8GWjnLbefatWB0CAbql8xCBzWaqTvuZulC2xyureArR7clNp/CuC8V/DqFkciMflXssrQSJtZqyNa0uynt2OBn6V0Rqy7nNKjDXQ+XPGfw2jZXxCPyrx7xz8Lk+dvJHftX2B4p8MwSFsLn8K808YeCIponPl/pXbHET01PNnSV3ofE
and so on...
This a quite a big value. I need to put this in a char data[] like below:
char sPostData[21070] = "{ \"image\" : \"<base64 encoded value>\" , \"name\": \"dev\"}";
but it throws this error:
Error C2026 string too big, trailing characters truncated
How can I resolve it?
The Microsoft compiler imposes a limit of 16380 single-byte characters for a string literal. The documentation says
Prior to adjacent strings being concatenated, a string cannot be longer than 16380 single-byte characters.
Break the string into adjacent chunks, something like
char[] = "a whole bunch of characters"
"a whole bunch more characters"
" and even more characters";
According to the documentation for that error, there is a limit of 16380 bytes in a character array (characters for narrow strings, fewer for Unicode).
Character string pointers (const char *) have a different limit, 65535 bytes.
I'm trying to tokenize the input consisting of UTF-8 characters. While some trying the learn utf8 i get an output that i cannot understand. when i input the characher π (pi) i get three different numbers 207 128 10. How can i use them to control which category it is belong to?
ostringstream oss;
oss << cin.rdbuf();
string input = oss.str();
for(int i=0; i<input.size(); i++)
{
unsigned char code_unit = input[i];
cout << (int)code_unit << endl;
}
Thanks in advance.
Characters encoded with UTF-8 may take up more than a single byte (and often do). The number of bytes used to encode a single code point can vary from 1 byte to 6 bytes (or 4 under RFC 3629). In the case of π, the UTF-8 encoding, in binary, is:
11001111 10000000
That is, it is two bytes. You are reading these bytes out individually. The first byte has decimal value 207 and the second has decimal value 128 (if you interpret as an unsigned integer). The following byte that you're reading has decimal value 10 and is the Line Feed character which you're giving when you hit enter.
If you're going to do any processing of these UTF-8 characters, you're going to need to interpret what the bytes mean. What exactly you'll need to do depends on how you're categorising the characters.
I have code where the user inputs two chars into a string variable. I have a function that verifies that the user input is only two chars long, and that it only contains valid hexadecimal digits.
I want to write these digits to a binary file that's 32 bytes long. I tried:
outFile.write((char*)&string[0], 1);
In a loop that runs 32 times (I want to write one byte at a time) to test, but it just writes the ascii code for the char, not the actual char itself. I expected it to write a nybble and skip a nybble, but it wrote a full byte of ascii information instead. So I tried:
outFile.write((unsigned char*)&string[0], 1);
But my compiler complains about it being an invalid cast.
I want to solve this problem without converting the string into a c-style string. In other words, I want string to contain two chars and represent one byte of information. Not four (plus null characters).
You have a string that represents an integer. So convert the string to an integer:
unsigned char byte = (unsigned char)std::stoi(string, 0, 16);
outFile:write(static_cast<const char*>(&byte), 1);
As a workaround for your missing stoi you can do this:
#include <iostream>
#include <sstream>
#include <ios>
char hexnum[]{"2F"}; // or whatever, upper or lowercase hex digits allowed.
std::istringstream input(hexnum);
int num=0;
input >> std::hex >> num;
unsigned char byte = num;
outFile.write(static_cast<const char*>(&byte), 1);
I am having an extremely difficult time trying to solve this. I would like to know how can I store a string input into an array in C++? I would like the array to be of size 12 because the inputs are going to be binary numbers, so for example this is what I want:
The input is going to be a binary number, 10100 for example, and I want to store that binary number into an array so that the array will look like this --> [1][0][1][0][0]. I want to store in an array any binary number, or, any number of 0's and 1's that the user gives.
the simplest solution is to use the c_str() function of c++ string. This will create a null terminated array of characters that's the same as your original string (you can just ignore the last byte)
so if you have the string myString
char * byteArray = myString.c_str()
will produce the above array
keep in mind that you can also just reference strings using []
string myString = "1101"
//option 1
char firstBit = myString[0];
//option 2
const char * primitiveArray = myString.c_str();
char firstBitOther = primitiveArray[0];
I just want to write a simple text file:
ofstream test;
test.clear();
test.open("test.txt",ios::out);
float var = 132.26;
BYTE var2[2];
var2[0] = 45;
var2[1] = 55;
test << var << (BYTE)var2[0] << (BYTE)var2[1];
test.close();
But in the output file I get:
132.26-7
I don't get what the problem is...
I think that the problem might be that BYTE type might be a typedef for char. If this were the case, then whenevernyou try to write out a BYTE to a stream, it will print the ASCII character corresponding to that byte rather than the numeric value of the byte. Notice that the characters - and 7 correspond to ASCII values 45 and 55, for example.
To fix this, you'll want to do two things:
Typecast the BYTEs you're writing to some integral type like int or short before writing them to the file. This forces the stream to write a numeric value rather than a character.
Output some amount of whitespace in-between all of the data you output. Right now everythingnis bleeding together because there are no spaces, which makes things harder to read.
Hope this helps!
BYTE is nothing but an alias for unsigned char. By default, when you output a char in a stream, it is converted to its ASCII character. In the ASCII table, the character 45 is '-' and the character 55 is '7'.
Try this instead:
test << var << (int)var2[0] << (int)var2[1];