How can initialize bits of unsigned char? - visual-c++

I want to initialize bits of a variable of unsigned char with a string of binary digits, but I don't know how to do it.

From your comments, it seems like you want to parse an ASCII string of binary digits. This is how you do that:
#include <stdlib.h>
const char* binary = "10001001";
char* next;
unsigned char value = strtoul(binary, &next, 2);
if (*next) { /* conversion failed */ }

unsigned char s[] = "How now brown cow.\n";

Related

Difference between char* argv[] and string argv[] in C

What's the difference between:
int main(int argc, char* argv[])
and
int main(int argc, string argv[])
How does char* argv[] behave when compared to string argv[] and why do we need string argv[] ?
Why is string argv[] not a pointer variable in the argument here:
int main(int argc, string argv[])
{
string str = argv[1];
printf("%s\n",str);
}
where as if I use char I must use a pointer:
int main(int argc, char* argv[])
{
string str = argv[1];
printf("%s\n",str);
}
I found out what char* argv[] means and string argv[] means. They both mean the same.
To illustrate why, Let's look into the below example:
string S = "Hi!"
what exactly happens in the background while using strings?
Let's say for example:
char* S = "Hi!";
Here we create a pointer variable S that's gonna store the address of a char.
"Hi!" is stored somewhere in memory that we do not know. The address of the first character of "Hi!" is stored in S(which is the same as saying variable S points to H). In simple terms, S is storing an address(the address of character 'H'). Thus we can access all the elements one by one till we find '/0' since we know that a string ends when it finds a '/0'
If I abstract away all the details the above code can also be written as:
string S = "Hi!"
which is the same as saying char* S = "Hi!"
What if I wanna access each character then:
printf("%c", *(S)); // dereferencing the first character which is H
printf("%c", *(S+1)); // dereferencing the second character which is i
printf("%c", *(S+2)); // dereferencing the third character which is !
If I were to access each character in strings then I would say:
printf("%c", S[0]);
printf("%c", S[1]);
printf("%c", S[2]);
where both methods do the same thing. The latter is more readable than the former which is why it is commonly used.
So we can conclude that char* S is the same as string S. Since when we use strings, in the background, it uses a char pointer to access each element of the string that's stored in memory.
They both do the same thing. Using String abstracts all the details.
This solves my doubt as to why char argv[] the same as string argv[].
String argv[] does the same thing as char argv[] does but it hides the implementation and makes it easier for readability.

Combining a static char* and a constant string

I want to be able to append a constant string to the end of another string in the form of a char*, and then use the resulting string as an argument for open(). Here's what it looks like:
file1.cpp
#include "string.h"
file2 foo;
char* word = "some";
foo.firstWord = word; //I want the file2 class to be able to see "some"
file2.h
#include <fstream>
#include <iostream>
#define SECONDWORD "file.txt"
class file2{
public:
file2();
static char* firstWord;
static char* fullWord;
private:
ofstream stream;
}
file2.cpp
#include "file2.h"
char* file2::firstWord;
char* file2::fullWord;
fullWord = firstWord + SECONDWORD; //so fullWord is now "somefile.txt" ,I know this doesn't work, but basically I am trying to figure out this part
file2::file2(){
stream.open(fullWord);
}
So I am not very well versed in C++, so any help would be appreciated!
C++-style solution might be the following.
#include <string>
char* a = "file";
char* b = ".txt";
...
stream.open((std::string(a) + b).c_str());
What happens here? First, std::string(a) creates a temporary std::string object. Than b value is added to it. At last, c_str() method returns a c-style string which contains a + b.

Convert cv::mat to string of bytes in OpenCV

I need to implement a function which takes an image and return a file say a text file containing a string of bytes.
What I have done yet is :
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main( int argc, char** argv )
{
cv::Mat image;
image = cv::imread("imaje.bmp");
if(image.empty())
return 0;
cv::imshow("Image", image);
cv::waitKey();
return 0;
}
Now I need to convert tha cv:Mat image to an array of bytes. Please guide me how to proceed???
Thanks in advance ... :)
I know, it's a bit late to answer... but it may be useful for other people.
You can convert your cv::Mat to a string by doing std::string my_cv_mat(src.begin<unsigned char>(), src.end<unsigned char>());
Then you can get a char* using the .c_str() method of the string. As char and byte have the same size I guess that you just have to cast the char* to byte*.

Program to see the bytes from a file internally

Do you know if exist one program or method to see (secuences of)bytes from a text,html file?
Not to see characters, rather see the complete sequence of bytes.
recommendations?
yes, it is called hex editor... Hundreds of those exist out there.
Here are some: http://en.wikipedia.org/wiki/Comparison_of_hex_editors
A common hex editor allows you to view any file's byte sequence.
If you just want to see the existing bytes (without changing them) you can use a hex-dump program, which is much smaller and simpler than a hex editor. For example, here's one I wrote several years ago:
/* public domain by Jerry Coffin
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
unsigned long offset = 0;
FILE *input;
int bytes, i, j;
unsigned char buffer[16];
char outbuffer[60];
if ( argc < 2 ) {
fprintf(stderr, "\nUsage: dump filename [filename...]");
return EXIT_FAILURE;
}
for (j=1;j<argc; ++j) {
if ( NULL ==(input=fopen(argv[j], "rb")))
continue;
printf("\n%s:\n", argv[j]);
while (0 < (bytes=fread(buffer, 1, 16, input))) {
sprintf(outbuffer, "%8.8lx: ", offset+=16);
for (i=0;i<bytes;i++) {
sprintf(outbuffer+10+3*i, "%2.2X ",buffer[i]);
if (!isprint(buffer[i]))
buffer[i] = '.';
}
printf("%-60s %*.*s\n", outbuffer, bytes, bytes, buffer);
}
fclose(input);
}
return 0;
}

cant convert parameter from char[#] to LPWSTR

When I compile this code in Visual C++, I got the below error. Can help me solve this issue..
DWORD nBufferLength = MAX_PATH;
char szCurrentDirectory[MAX_PATH + 1];
GetCurrentDirectory(nBufferLength, szCurrentDirectory);
szCurrentDirectory[MAX_PATH +1 ] = '\0';
Error message:
Error 5 error C2664: 'GetCurrentDirectoryW' : cannot convert parameter 2 from 'char [261]' to 'LPWSTR' c:\car.cpp
Your program is configured to be compiled as unicode. Thats why GetCurrentDirectory is GetCurrentDirectoryW, which expects a LPWSTR (wchar_t*).
GetCurrentDirectoryW expects a wchar_t instead of char array. You can do this using TCHAR, which - like GetCurrentDirectory - depends on the unicode setting and always represents the appropriate character type.
Don't forget to prepend your '\0' with an L in order to make the char literal unicode, too!
It seems you have define UNICODE, _UNICODE compiler flags. In that case, you need to change the type of szCurrentDirectory from char to TCHAR.
Headers:
#include <iostream>
#include <fstream>
#include <direct.h>
#include <string.h>
#include <windows.h> //not sure
Function to get current directory:
std::string getCurrentDirectoryOnWindows()
{
const unsigned long maxDir = 260;
wchar_t currentDir[maxDir];
GetCurrentDirectory(maxDir, currentDir);
std::wstring ws(currentDir);
std::string current_dir(ws.begin(), ws.end());
return std::string(current_dir);
}
To call function:
std::string path = getCurrentDirectoryOnWindows(); //Output like: C:\Users\NameUser\Documents\Programming\MFC Program 5
To make dir (Folder) in current directory:
std::string FolderName = "NewFolder";
std::string Dir1 = getCurrentDirectoryOnWindows() + "\\" + FolderName;
_mkdir(Dir1.c_str());
This works for me in MFC C++.

Resources