Let's imagine I have
String x = "hello there";
So I can print it from index e.g. 1 as:
Serial.println(x.substring(1));
ello there
I wanna do the same with
char x[] = "hello there";
Any ideas? (Except using loops to print char by char)
You can use the & operator to get the string after the desired index like this:
Serial.println(&x[1]);
Related
I wrote a simple program to print a unicode smile emoji. Unfortunately, something else is printed. Does anyone know what the problem with this code is? Thanks
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string str = u8"\u1F600";
cout << str << endl;
return 0;
}
Compilation and output:
g++ -pedantic -Wall test109.cc && ./a.out
ὠ0
\u escape sequences have the format \u#### (i.e. exactly 4 hex digits). You need \U########:
auto str = u8"\U0001F600";
Or, encoding the UTF8 bytes separately:
auto str2 = u8"\xf0\x9f\x98\x80";
That works.
The \u escape sequence is limited to 4 hex digits max, so "\u1F600" is parsed as two separate characters \u1F60 (ὠ) and 0, which is exactly what you are seeing in your console output.
Codepoint U+1F60 GREEK SMALL LETTER OMEGA WITH PSILI is very different than codepoint U+1F600 GRINNING FACE.
For what you are trying, you need to use the \U escape instead, which allows up to 8 hex digits:
string str = u8"\U0001F600";
Alternatively, you can use one of these instead:
string str = u8"\xF0\x9F\x98\x80"; // UTF-8 codeunits in hex format
string str = u8"\360\237\230\200"; // UTF-8 codeunits in octal format
string str = u8"😀"; // if your compiler/editor allows this
You can use any of the following which works for you.
string str = "\u263A"; // --> ☺
//string str = u8"\xe2\x98\xba"; --> ☺
//string str = u8"\U0001F600"; --> 😀
//string str = u8"😀"; --> 😀
//string str = "\342\230\272" --> ☺
cout << str << endl;
How can I append an int containing an ordinal number to a str in Python 2.7?
Example:
number = 10 # You can assume its smaller than 128
mystr = "abc"
mystr = mystr+number # Gives error
assert mystr == "abc\x0A"
Of course, "mystr%d"%number or mystr+str(number) won't work, they would give "abc10"
I think you want the built-in function chr:
number = 10
mystr = "abc"
mystr = mystr + chr(number)
import struct
mystr = mystr+struct.pack("b",number)
satisfies the assertion.
I have problem with getting out a character or characters of a string in C++.
It was easy in python, for example I could take the first block of a string like this:
x = "Hello Word"
p = x[0]
now the H character would save in p.
As mentioned, surely you mean a char (character?)
std::string x = "Hello Word";
char p = x[0]; //p now contains 'H'
See http://en.cppreference.com/w/cpp/string/basic_string for more detail (thanks for suggestion of link)
Now I have a char array with the mac address stored in:
char mac_addr[6];
And I sprintf it into another char array:
char cmdstr[64];
sprintf(cmdstr, "MAC: %2x:%2x:%2x:%2x:%2x:%2x;",
mac_addr[0], mac_addr[1], mac_addr[2],
mac_addr[3], mac_addr[4], mac_addr[5]);
When I print cmdstr, it looks like this:
MAC: 08:10:76:10:26:21;
Then I send the cmdstr to a remote server, there is a parse function to parse this char array. After the cmdstr is parsed, the six parts separated by a : are stored in a multi char array:
char mac[6][10];
With:
char mac[0] = "08"
char mac[1] = "10"
char mac[2] = "76"
char mac[3] = "10"
char mac[4] = "26"
char mac[5] = "21"
Now, on the server side, how can I re-convert each part of the mac address to char and make it possible that the whole mac address can be stored in a char array like this?
char my_mac[6];
With:
my_mac[0] = '08'
my_mac[1] = '10'
my_mac[2] = '76'
my_mac[3] = '10'
my_mac[4] = '26'
my_mac[5] = '21'
Many thanks!
Edit:
Sorry I think I described something wrongly, the text '08' itself should not stored in my_mac[0]. Actually when I gdb the program, I found that:
(gdb) p mac_addr[0]
$15 = 8 '\b'
(gdb) p mac_addr[1]
$16 = 16 '\020'
(gdb) p mac_addr[2]
$17 = 118 'v'
(gdb) p mac_addr[3]
$18 = 16 '\020'
(gdb) p mac_addr[4]
$19 = 38 '&'
(gdb) p mac_addr[5]
$20 = 33 '!'
I think I should convert the mac[6][10] to a char array just like above.
You can use strtol function in stdlib.h
char *dummy;
for (int i = 0; i < 6; i++) {
my_mac[i] = strtol(mac[i], &dummy, 16);
}
For example I have a haskell list [72,73,74,75], how can i output this list as a string?, all elements in the list are ascii value.
You can combine map, that applies a function to each element of a list, and the chr function, that convert an Int value to its Char equivalent:
> map chr [72,73,74,75]
"HIJK"
You can convert an Int code point to a Char using chr :: Int -> Char; a String is just a list of Chars. Note that this'll work for any Unicode code point, not just ASCII, which is something you should be doing anyway.
You can find functions like this using Hoogle; just type something like Int -> Char, and it'll give you functions that match that type.
You can use 'chr' from the module Char to convert the integer values to characters:
import Char
intListToString l = [ chr x | x <- l ]
main = do
putStrLn $ "the string: " ++ (intListToString [72,73,74,75])
Running the above with 'runghci' gives:
the string: HIJK
Do you want this list as a straight string or a list with commas? Unless you want to convert ASCII char values to their character counterparts (which was already covered), you can do the following:
concatMap show [72,73,74,75]
will give you a "72737475" string and
init $ tail $ show [72,73,74,75]
will give you a "72,73,74,75" string