I have the code as below:
NSString* str = #"π";
const char* chr = [str UTF8String];
The value for chr is "\xcf\x80" which is a sequence of hexadecimal charter.
To write as RTF file I need to extract the value excluding "\x" from this hexadecimal character.
How should I do to get the data?
Regards
In C the char type is an integral type so you can produce a hexadecimal representation of a character just as you would any other integer value - using the %x format specifier. To convert a complete C string to a string of hexadecimal just iterate over the string and build up the result. Rough code, typed directly into answer so expected errors:
NSString *str = ...
const char *cStr = [str UTF8String];
NSMutableString *hexStr = [NSMutableString new]; // for result
while( *cStr ) // loop while cStr is not pointing at a null char
{
[hexStr appendFormat:#"%02x", *cStr++]; // %02x - zero fill, width 2, hex
}
Related
Can I use std::strlen() on null terminated UTF8 string and expect it to work? That is count the string lenght in bytes not in glyphs/codepoints?
Or does the UTF8 multi byte codepoints just break strlen() in this case?
I'm bit paranoid about it and I'm currently doing following with utf8cpp library:
size_t utf8bytelen(const char * utf8str) {
const char * itr = utf8str;
while(*itr && utf8::unchecked::next(itr));
return std::distance(utf8str, itr);
}
I have to display fractions using the symbols and I can't seem to be able to display these 4.
using
char UCP_VULGAR_FRACTION_ONE_HALF_UTF8 = L'\u00BD';
char UCP_VULGAR_FRACTION_ONE_QUARTER_UTF8 = L'\u00BC';
char UCP_VULGAR_FRACTION_THREE_QUARTERS_UTF8 = L'\u00BE';
I can get 1/2, 1/4 and 3/4 to display just fine (cout<< (char)UCP_VULGAR_FRACTION_ONE_HALF_UTF8), but doing the same for those fractions:
char UCP_VULGAR_FRACTION_ONE_EIGHTH_UTF8 = L'\u215B';
char UCP_VULGAR_FRACTION_THREE_EIGHTHS_UTF8 = L'\u215C';
char UCP_VULGAR_FRACTION_FIVE_EIGHTHS_UTF8 = L'\u215D';
char UCP_VULGAR_FRACTION_SEVEN_EIGHTHS_UTF8 = L'\u215E';
Gets me [, \, ] and ^. What am I doing wrong? I tried g_unichar_to_utf8 with no success...
For UTF-8 you need to store multibyte characters - characters contained in one or more bytes. Typically these are stored in a std::string:
std::string const UCP_VULGAR_FRACTION_ONE_EIGHTH_UTF8 = u8"\u215B";
std::string const UCP_VULGAR_FRACTION_THREE_EIGHTHS_UTF8 = u8"\u215C";
std::string const UCP_VULGAR_FRACTION_FIVE_EIGHTHS_UTF8 = u8"\u215D";
std::string const UCP_VULGAR_FRACTION_SEVEN_EIGHTHS_UTF8 = u8"\u215E";
Or possibly a null terminated char array:
char const* UCP_VULGAR_FRACTION_ONE_EIGHTH_UTF8 = "\u215B";
char const* UCP_VULGAR_FRACTION_THREE_EIGHTHS_UTF8 = "\u215C";
char const* UCP_VULGAR_FRACTION_FIVE_EIGHTHS_UTF8 = "\u215D";
char const* UCP_VULGAR_FRACTION_SEVEN_EIGHTHS_UTF8 = "\u215E";
Use wchar_t instead of char. Also be aware that you can't print wchar_t using std::cout, you need to use wide version of std::cout which is std::wcout. BTW, If you use wcout with cout together, the program will crash most probably. so you may want to store these unicode characters in normal UTF-8 std::string instead of wchar_t, and print them using std::cout
I have a QString where I append data input from the user.
At the end of the QString, I need to append the hexadecimal representation of a "Normal" QString.
For example:
QString Test("ff00112233440a0a");
QString Input("Words");
Test.append(Input);//but here is where Input needs to be the Hex representation of "Words"
//The resulting variable should be
//Test == "ff00112233440a0a576f726473";
How can I convert from ASCII (I think) to it's Hex representation?
Thanks for your time.
You were very close:
Test.append(QString::fromLatin1(Input.toLatin1().toHex()));
Another solution to your problem.
Given a character, you can use the following simple function to compute its hex representation.
// Call this function twice -- once with the first 4 bits and once for the last
// 4 bits of a char to get the hex representation of a char.
char toHex(char c) {
// Assume that the input is going to be 0-F.
if ( c <= 9 ) {
return c + '0';
} else {
return c + 'A' - 10;
}
}
You can use it as:
char c;
// ... Assign a value to c
// Get the hex characters for c
char h1 = toHex(c >> 4);
char h2 = toHex(c & 0xF);
I wrote a very simple encryption program to practice c++ and i came across this weird behavior. When i convert my char* array to a string by setting the string equal to the array, then i get a wrong string, however when i create an empty string and add append the chars in the array individually, it creates the correct string. Could someone please explain why this is happening, i just started programming in c++ last week and i cannot figure out why this is not working.
Btw i checked online and these are apparently both valid ways of converting a char array to a string.
void expandPassword(string* pass)
{
int pHash = hashCode(pass);
int pLen = pass->size();
char* expPass = new char[264];
for (int i = 0; i < 264; i++)
{
expPass[i] = (*pass)[i % pLen] * (char) rand();
}
string str;
for (int i = 0; i < 264; i++)
{
str += expPass[i];// This creates the string version correctly
}
string str2 = expPass;// This creates much shorter string
cout <<str<<"\n--------------\n"<<str2<<"\n---------------\n";
delete[] expPass;
}
EDIT: I removed all of the zeros from the array and it did not change anything
When copying from char* to std::string, the assignment operator stops when it reaches the first NULL character. This points to a problem with your "encryption" which is causing embedded NULL characters.
This is one of the main reasons why encoding is used with encrypted data. After encryption, the resulting data should be encoded using Hex/base16 or base64 algorithms.
a c-string as what you are constructing is a series of characters ending with a \0 (zero) ascii value.
in the case of
expPass[i] = (*pass)[i % pLen] * (char) rand();
you may be inserting \0 into the array if the expression evaluates to 0, as well as you do not append a \0 at the end of the string either to assure it being a valid c-string.
when you do
string str2 = expPass;
it can very well be that the string gets shorter since it gets truncated when it finds a \0 somewhere in the string.
This is because str2 = expPass interprets expPass as a C-style string, meaning that a zero-valued ("null") byte '\0' indicates the end of the string. So, for example, this:
char p[2];
p[0] = 'a';
p[1] = '\0';
std::string s = p;
will cause s to have length 1, since p has only one nonzero byte before its terminating '\0'. But this:
char p[2];
p[0] = 'a';
p[1] = '\0';
std::string s;
s += p[0];
s += p[1];
will cause s to have length 2, because it explicitly adds both bytes to s. (A std::string, unlike a C-style string, can contain actual null bytes — though it's not always a good idea to take advantage of that.)
I guess the following line cuts your string:
expPass[i] = (*pass)[i % pLen] * (char) rand();
If rand() returns 0 you get a string terminator at position i.
I am doing some serial port communcation to a computer controlled pump and the createfile function I used to communicate requires the com port name to be parsed as a wchar_t pointer.
I am also using QT to create a form and aquire the com port name as a QString.
This QString is converted to a char array and pointed to as follows:
char* Dialog::GetPumpSerialPortNumber(){
QString mystring;
mystring = ui->comboBox_2->currentText();
char * mychar;
mychar = mystring.toLatin1().data();
return mychar;
I now need to set my port number which is stored as a wchar_t* in my pump object. I do this by calling the following function:
void pump::setPortNumber(wchar_t* portNumber){
this->portNumber = portNumber;
}
Thus how do I change my char* (mychar) into a wchar_t* (portNumber)?
Thanks.
If you're talking about just needing a char array to a wchar_t array, here's a solution for you:
static wchar_t* charToWChar(const char* text)
{
size_t size = strlen(text) + 1;
wchar_t* wa = new wchar_t[size];
mbstowcs(wa,text,size);
return wa;
}
An enhancement to leetNightshade's answer could be
size_t unistrlen(const char *s) {
size_t sz = 0;
const char *sc;
for (sc = s; *sc != '\0'; sc+=(
((*sc&0x80)==0x80) ? 2 :/*1st byte of 2-byte character*/
((*sc&0xc0)==0xc0) ? 3 :/*1st byte of 3-byte character*/
((*sc&0xe0)==0xe0) ? 4 :/*1st byte of 4-byte character*/
((*sc&0xf0)==0xf0) ? 1 :/*2nd, 3rd, or 4th byte of multi-byte character*/
1) /*single byte character*/)
if ((*sc&0xf0)!=0xf0) sz++;
return sz;
}
wchar_t* charToWChar(const char* text) {
size_t size = unistrlen(text) + 1;
wchar_t* wa = new wchar_t[size];
mbstowcs(wa,text,size);
return wa;
}
Where unistrlen will return how many characters (single or multi bytes characters) in your string unlike strlen which returns the length byte by byte and that might waste some memory if your string contains some multi-byte characters.
You can use the toWCharArray function of QString to have your wchar_t* value and return a wchar_t* from your GetPumpSerialPortNumber function.
I've found a helpful article in MSDN - How to: Convert Between Various String Types. I guess it should be useful.
QString::toWCharArray ( wchar_t * array ) ?