I have CStrings in my program that contain BYTE information like the following:
L"0x45"
I want to turn this into a BYTE type with value 0x45. How do I do this? All examples I can find are trying to get the literal byte value of the string itself, but I want to take the value contained within the CString and convert THAT to a BYTE. How do I achieve this?
You can use the wcstoul() conversion function, specifying base 16.
e.g.:
#define UNICODE
#define _UNICODE
#include <stdlib.h> // for wcstoul()
#include <iostream> // for console output
#include <atlstr.h> // for CString
int main()
{
CString str = L"0x45";
static const int kBase = 16; // Convert using base 16 (hex)
unsigned long ul = wcstoul(str, nullptr, kBase);
BYTE b = static_cast<BYTE>(ul);
std::cout << static_cast<unsigned long>(b) << std::endl;
}
C:\Temp>cl /EHsc /W4 /nologo test.cpp
Output:
69
As an alternative, you can also consider using new C++11's std::stoi():
#define UNICODE
#define _UNICODE
#include <iostream> // for console output
#include <string> // for std::stoi()
#include <atlstr.h> // for CString
int main()
{
CString str = L"0x45";
static const int kBase = 16; // Convert using base 16 (hex)
int n = std::stoi(str.GetString(), nullptr, kBase);
BYTE b = static_cast<BYTE>(n);
std::cout << static_cast<unsigned long>(b) << std::endl;
}
NOTE
In this case, since std::stoi() expects a const std::wstring& argument, you must explicitly get the const wchar_t* pointer for the CString instance, either using CString::GetString() as I did (and I prefer), or using static_cast<const wchar_t*>(str).
Then, a temporary std::wstring will be built to be passed to std::stoi() for the conversion.
Related
#include <iostream>
using namespace std;
int main() {
std::wstring str = L"\u00A2";
std::wcout << str;
return 0;
}
Whys this doesn't work? And how solve this?
It doesn't work because in the default C locale, there is no character which corresponds to U+00A2.
If you're using a standard ubuntu install, it is most likely that your user locale uses a larger character set than US-ASCII, quite possibly Unicode encoded with UTF-8. So you just need to switch to the locale specified in the environment, as follows:
#include <iostream>
/* locale is needed for std::setlocale */
#include <locale>
#include <string>
int main() {
/* The following switches to the locale specified
* by the LC_ALL environment variable.
*/
std::setlocale (LC_ALL, "");
std::wstring str = L"\u00A2";
std::wcout << str;
return 0;
}
If you use std::string instead of std::wstring and std::cout instead of std::wcout, then you don't need the setlocale because no translation is needed (provided the console expects UTF-8).
I am working with C++ in eclipse CDT and I am trying to convert string to uint64_t by using strtoull but everytime I get below error message -
..\src\HelloTest.cpp:39:42: error: strtoull was not declared in this scope
Below is my C++ example
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string str = "1234567";
uint64_t hashing = strtoull(str, 0, 0);
cout << hashing << endl;
}
return 0;
}
Is there anything wrong I am doing?
Why your solution doesn't work has already been pointed out by others. But there hasn't been a good alternative suggested yet.
Try this for C++03 strtoull usage instead:
#include <string>
#include <cstdlib>
int main()
{
std::string str = "1234";
// Using NULL for second parameter makes the call easier,
// but reduces your chances to recover from error. Check
// the docs for details.
unsigned long long ul = std::strtoull( str.c_str(), NULL, 0 );
}
Or, since C++11, do it directly from std::string via stoull (which is just a wrapper for the above, but saves on one include and one function call in your code):
#include <string>
int main()
{
std::string str = "1234";
// See comment above.
unsigned long long ul = std::stoull( str, nullptr, 0 );
}
Never use char[] or pointers if you have a working alternative. The dark side of C++, they are. Quicker, easier, more seductive. If once you start down the dark path, forever will it dominate your destiny, consume you it will. ;-)
the structure for strtoull is: strtoull(const char *, char * *, int)
You have given it a std::string as pointed out by #juanchopanza
This is the solution I came up with is
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
char str[] = "1234567";
unsigned long long ul;
char* new_pos;
charDoublePointer = 0;
ul = strtoull(str, &new_pos, 0);
cout << ul << endl;
return 0;
}
The output I got was: 1234567
Straight from the eclipse console.
Also at the end of your program you have return 0 out of scope with an extra curly brace.
#include "stdafx.h"
#include <string>
#include <windows.h>
using namespace std;
int main()
{
string FilePath = "C:\\Documents and Settings\\whatever";
CreateDirectory(FilePath, NULL);
return 0;
}
Error: error C2664: 'CreateDirectory' : cannot convert parameter 1 from 'const char *' to 'LPCTSTR'
How do I make this conversion?
The next step is to set today's date as a string or char and concatenate it with the filepath. Will this change how I do step 1?
I am terrible at data types and conversions, is there a good explanation for 5 year olds out there?
std::string is a class that holds char-based data. To pass a std::string data to API functions, you have to use its c_str() method to get a char* pointer to the string's actual data.
CreateDirectory() takes a TCHAR* as input. If UNICODE is defined, TCHAR maps to wchar_t, otherwise it maps to char instead. If you need to stick with std::string but do not want to make your code UNICODE-aware, then use CreateDirectoryA() instead, eg:
#include "stdafx.h"
#include <string>
#include <windows.h>
int main()
{
std::string FilePath = "C:\\Documents and Settings\\whatever";
CreateDirectoryA(FilePath.c_str(), NULL);
return 0;
}
To make this code TCHAR-aware, you can do this instead:
#include "stdafx.h"
#include <string>
#include <windows.h>
int main()
{
std::basic_string<TCHAR> FilePath = TEXT("C:\\Documents and Settings\\whatever");
CreateDirectory(FilePath.c_str(), NULL);
return 0;
}
However, Ansi-based OS versions are long dead, everything is Unicode nowadays. TCHAR should not be used in new code anymore:
#include "stdafx.h"
#include <string>
#include <windows.h>
int main()
{
std::wstring FilePath = L"C:\\Documents and Settings\\whatever";
CreateDirectoryW(FilePath.c_str(), NULL);
return 0;
}
If you're not building a Unicode executable, calling c_str() on the std::string will result in a const char* (aka non-Unicode LPCTSTR) that you can pass into CreateDirectory().
The code would look like this:
CreateDirectory(FilePath.c_str(), NULL):
Please note that this will result in a compile error if you're trying to build a Unicode executable.
If you have to append to FilePath I would recommend that you either continue to use std::string or use Microsoft's CString to do the string manipulation as that's less painful that doing it the C way and juggling raw char*. Personally I would use std::string unless you are already in an MFC application that uses CString.
I'm trying to convert UTF-16 encoded strings to UCS-4
If I understand correctly, C++11 provides this conversion through codecvt_utf16.
My code is something like:
#include <iostream>
#include <locale>
#include <memory>
#include <codecvt>
#include <string>
using namespace std;
int main()
{
u16string s;
s.push_back('h');
s.push_back('e');
s.push_back('l');
s.push_back('l');
s.push_back('o');
wstring_convert<codecvt_utf16<wchar_t>, wchar_t> conv;
wstring ws = conv.from_bytes(reinterpret_cast<const char*> (s.c_str()));
wcout << ws << endl;
return 0;
}
Note: the explicit push_backs to get around the fact that my version of clang (Xcode 4.2) doesn't have unicode string literals.
When the code is run, I get terminate exception. Am I doing something illegal here? I was thinking it should work because the const char* that I passed to wstring_convert is UTF-16 encoded, right? I have also considered endianness being the issue, but I have checked that it's not the case.
Two errors:
1) from_bytes() overload that takes the single const char* expects a null-terminated byte string, but your very second byte is '\0'.
2) your system is likely little-endian, so you need to convert from UTF-16LE to UCS-4:
#include <iostream>
#include <locale>
#include <memory>
#include <codecvt>
#include <string>
using namespace std;
int main()
{
u16string s;
s.push_back('h');
s.push_back('e');
s.push_back('l');
s.push_back('l');
s.push_back('o');
wstring_convert<codecvt_utf16<wchar_t, 0x10ffff, little_endian>,
wchar_t> conv;
wstring ws = conv.from_bytes(
reinterpret_cast<const char*> (&s[0]),
reinterpret_cast<const char*> (&s[0] + s.size()));
wcout << ws << endl;
return 0;
}
Tested with Visual Studio 2010 SP1 on Windows and CLang++/libc++-svn on Linux.
ok so i wrote a program which scaans all directories an sub directories to find sepcific file extensions(i pass a string for the extension type i want) then it returns a vector loade with all the filenames with the specific filetype extensions. then i have another class which has a function that print out all of the files in the vector and will then will iterate the vector and run a program in that vector that the user chooses. This is my problem. Getting the file from the vvector to run. i am using visual studios on windows 7
using boost filesystem V3. this is my current function:
#define BOOST_FILESYSTEM_NO_DEPRECATED
#ifndef NotePad__h
#define NotePad__h
#include boost/filesystem.hpp
#include iostream
#include io.h
#include stdlib.h
#include stdio.h
#include cstdlib
#include Windows.h
#include atlstr.h
#include string
#include cstring
;
namespace fs = boost::filesystem;
class NpLaunch
{
public:
void Launch (const std::vector<fs::path>& v)
{
int count=0;
std::cout << "launched in notePad.h" << std::endl;
for(auto i = v.begin(); i!= v.end(); ++i)
{
//string s;
//string val = (string) itr;
std::cout << count << ". " << *i << std::endl;
++count;
std::string s = i->c_str();
//std::system(i->c_str());
}
}
};
#endif
and this is the error im getting:
Error 1 error C2440: 'initializing' : cannot convert from 'const >boost::filesystem3::path::value_type *' to 'std::basic_string<_Elem,_Traits,_Ax>' >c:\users\admin\documents\visual studio 2010\projects\launcher\launcher\notepad.h 31
On Windows, path::value_type is a wchar_t, and thus path::string_type is equivilent to std::wstring, and the path::c_str() method returns a wchar_t*. You cannot assign a wchar_t* to a std::string, that is what the compiler error is trying to tell you.
To assign a path object to a std::string, you have to perform a character conversion from wchar_t to char. The path::string() method does that for you, eg:
std::string s = i->string();
Otherwise, use a std::wstring instead, which you can assign to using either the path::native() or path::wstring() method, eg:
std::wstring s = i->native();
std::wstring s = i->wstring();