I am creating a bytearray from a list.
mybytes_array = bytes([255,110,41,128,09])
I then uses regex to find all occurences of
[(m.start(0), m.end(0)) for m in re.finditer(mybytes_array, ba)]
I can have any value instead of 41 that creates a metacharacter for regex. I want to escape that character so that I can match it against ba that is also a bytearray
How can I do that?
I cannot obviously convert to string append backslash and then match against ba. So I am not sure how can I change the mybytes_array so as to search the correct string.
The re package can work on both str and bytes inputs as long as the arguments are of the same type.
You may use re.escape to escape the whole bytes.
Your code will be something like
[(m.start(0), m.end(0)) for m in re.finditer(re.escape(mybytes_array), ba)]
Related
So I'm trying to change the string \t\n into an array of all of word characters in the string. The array I want would look like this: ["t","n"].
So far I've tried:
input = " \t\n"
array = input.scan(/\w/)
I've tried this regular expression on this string on rubular and it matches with all of the word characters as I'd like it to.
However, when using input.scan(/\w) an empty array is returned.
Please forgive my ignorance as I'm still new to this, but why is this?
Here you go! You were really close.
input = " \t\n"
array = input.dump.scan(/\w/)
=> ["t", "n"]
The key is to use String#dump (see: https://ruby-doc.org/core-2.6.5/String.html#method-i-dump)
I am not familiar with ruby but you seem to be having string interpolation confusion.
Per https://www.ruby-forum.com/t/new-line-in-string/176797
input = " \t\n"
Gives you a string with a space, tab, and newline.
You probably want to use single quotes to literally get the string you wrote:
input = ' \t\n'
If you sorely want to stick with double-quotes then I believe this would work:
input = " \\t\\n"
You should read https://blog.appsignal.com/2016/12/21/ruby-magic-escaping-in-ruby.html to learn more about string interpolation in Ruby. I would link you to the official docs but my lack of ruby experience translates to a lack of official doc experience.
So like colleagues explain in comments, the letters which you have in "\t\n" string are not ordinary letters, only something called special characters so I am not sure but there is not easy way to take this characters from this string cause \t is like one character.
With normal string like tn you could do something like this
"tn".split("")
and that give you array which you want.
But on special characters like in the example. you could do something like this
a = "\t\n".split("")
a.map! do |e|
if e == "\t"
"t"
elsif e == "\n"
"n"
end
end
which give you, I believe, results which you want.
You're given an array of strings where each character in the string is lowercase. Each character and the length of each string is randomly generated. Encode the string such that:
1. The encoded output is a single string with minimum possible length
2. You should be able to decode the string later
I am thinking the mention of each character being lowercase is key here. Since there are only 26 lowercase characters, maybe we can encode them using 5 bits instead of 8 bits and then pack them. But I am not sure how to implement this bit packing while looping over the array of strings
For 26 characters and a separator you could use base32. Basically concatenate the strings with a delimiter and then do a base32 decode - should be easy to find code for that. Just do not use those characters that result in 4-5 zeros in binary so that you do not accidentally have the null terminator in the middle of your string.
For decoding you'll do base32 encode and then split the string at delimiters.
I'm having problems getting my code to work:
for (( c=1; c<=$DirsArrCnt; c=c+$OneDirArrCnt )); do
# Replace every occurence of "/" (ASCII d47 o057) in path with "^A" (ASCII 1)
Hold="${DirsArr[$c]}"
DirsArr[c]="${Hold//\057/\001}"
done
Originally I skipped the Hold variable and used the array element directly but took that out thinking it was the problem.
Am I specifying the octal value correctly? I believe 57 is the octal value for "/" right?
I think this is what you want :
DirsArr[c]="${Hold//$'\057'/$'\001'}"
The syntax you use interprets \0 as a literal 0 (i.e. does nothing different compared to not using the backslash). You need the C-style string to have your numeric code interpreted by the shell.
I came across a line like
char* template = "<html><head><title>%i %s</title></head><body><h1>%i %s</h1> </body></html>";
while reading through code to implement a web server.
I'm curious as I've never seen a string like this before - is template specifying a special type of string (I'm just guessing here because it was highlighted on my IDE)? Also, how would strlen() work with something like this?
Thanks
char* template = "<html>...</html>";
is fundamentally no different than
char *s = "hello";
The name template is not special, it's just an ordinary identifier, the name of the variable. (template happens to be a keyword in C++, but this is C.)
It would be better to define it as const, to enforce the fact that string literals cannot be modified, but it's not mandatory.
Note that template itself is not a string. It's a pointer to a string. The string itself (defined by the language as "a contiguous sequence of characters terminated by and including the first null
character") is the sequence starting with "<html>" and ending with "</html>" and the implicit terminating null character.
And in answer to your second question, strlen(template) would work just fine, giving you the length of the string (81 in this case).
I imagine that there is another part of the code that uses this string to format an output string used as a page by the web server. The strlen function will return the length of the string.
Unless there's a null character somewhere in the initializer or an escape sequence using a \ character, which there isn't, there's nothing special about this string. A % is a normal character in a string and doesn't receive special treatment. The strlen function in particular will read %i as two characters, i.e. % and i. Similarly for %s.
In contrast, a \ is a special character for string and denotes an escape sequence. The \ and the character that follows it in the string constant constitute a single character in the string itself. For example, \n means a newline character (ASCII 10) and \t is a tab character (ASCII 8).
This string is most likely used as a format string for printf. This function will read the string and interpret the %i and %s as format string accepting a int and a char * respectively.
char* template = "<html>...</html>";
just create a char array to store data "<html>...</html>",and this array name is template,you can change this name to other name you want.When create char array,compiler will add \0 to the end of array.strlen will calculate the length from array start to \0(\0 is no include).
I think your IDE will highlight this string is because this string is used in other place.
I have this hex string:
\x5c30\x3032\x5f5c\x3337\x345c\x3334\x366f\x5c32\x3633\x5c30\x3136\x5c32\x3132\x5c32\x3234\x4e5c\x3236\x335c\x3231\x335c\x3337\x355c\x3335\x315c\x3232\x365c\x3337
How could I convert it to a NSString or NSData? I though of using C methods, but I'm not experienced in C :(
Looks like Unicode characters (specifically, CJK ideographs) to me.
Use an NSScanner to scan the string. Scan up to a backslash, and add whatever you scanned to a mutable string. Then, scan the backslash and throw it away, and then scan the x and throw that away.
Then, scan four single characters, which will be the digits (NSScanner doesn't have a method to scan a single character, so you will need to get them yourself using characterAtIndex: and then adjust the scanner's scan location accordingly). Perform the appropriate conversion of the hexadecimal digit characters to numbers and the math to assemble a single number from them, and you will have the code point (character value) represented by the escape sequence. Add that single character to your string.
Repeat that until you run out of input string, and you will have converted the input string with all its escape sequences into a string with the unescaped characters.