Need some Ruby translated [closed] - ruby

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
plist_values['HashData'].join("").unpack('m')[0].each_byte do |b|
hash_decoded << sprintf("%02X", b)
end
I need to translate it to other language. As I understood, it puts all contents of 'HashData' array into a string, then decodes it from Base64, But what's next?
Can you write me a step-by step explanation what it does?
Thanks in Advance!

It decodes a base64 value from plist_values (the one with key 'HashData') and converts it to printable hex.

join("") concatenates all the strings in the array (or what each element in the array returns when calling to_s).
unpack('m') from the docs decodes the string (and it assumes it is base64 encoded).
sprintf("%02X", b) from the docs returns the hexadecimal representation with upper case letters.
hash_decoded << .. appends the hex representation to the string
The bottom line is that you get a string that represents the hexadecimal version (with upper case letters) of the joined strings in plist_values['HashData'].

Related

Log Write Output to File in CAPL [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is there an easy way to take what I write to the write window and log it to a file? Or do I need to separately create an array of chars manually and open a file to write the char[] to? I would love to be able to write to file using regular expressions at the very least but I'm not finding much helpful info from the docs.
Looks like writeToLogEx(char format[], ...) can do what I want but it outputs to a Logging Block in the measurement setup. So I'll have some header and footer data that I don't want as well as CAN traffic if I don't put up a channel block.
Vector's Example:
char timeBuffer[64];
getLocalTimeString(timeBuffer);
writeToLogEx("===> %s",timeBuffer);
Regular Expression Options:
"%ld","%d" decimal display
"%lx","%x" hexadecimal display
"%lX","%X" hexadecimal display (upper case)
"%lu","%u" unsigned display
"%lo","%o" octal display
"%s" display a string
"%g","%lf" floating point display
"%c" display a character
"%%" display %-character
"%I64d" decimal display of a 64 bit value
"%I64x" hexadecimal display of a 64 bit value
"%I64X" hexadecimal display of a 64 bit value (upper case)
"%I64u" unsigned display of a 64 bit value

Regex issue with a string and all numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In Ruby, I would like to create a regular expression that matches the following:
building/liberty-green/6d
(the word building and some number somewhere after it)
Currently, I have /building/ and need to add \d (any digit) to it, but I don't know how.
You need /building\/[\w-]+\/\w+/. For example:
irb(main):001:0> /building\/[\w-]+\/\w+/.match("building/liberty-green/6d")
=> #<MatchData "building/liberty-green/6d">
That expression will match any string that:
Starts with /building/
Then follows with one or more word characters or dashes (eg. foo-bar, foo, bar-1)
Then follows with a /
Finally ends with one or more word characters (eg. foo, 6d, 12345)
Note that \w includes digits.

How to convert "商品编号" to "\u5546\u54c1\u7f16\u53f7" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
"\u5546\u54c1\u7f16\u53f7" is displayed as "商品编号".
"\u5546\u54c1\u7f16\u53f7" # => "商品编号"
What is the character encoding in "\u5546\u54c1\u7f16\u53f7"? How can I convert "商品编号" to "\u5546\u54c1\u7f16\u53f7"?
The \uHHHH (where HHHH is in hex) notation is simply a way to reference Unicode characters by number. This is usually used when:
You don't know how to get things like 商 out of your keyboard.
You're working in an environment that can't display all the Unicode that you need.
When you say "\u5546\u54c1\u7f16\u53f7" and see "商品编号", it simply means that you're working in a modern terminal that is Unicode aware and has a good font.
In most cases it should matter which representation you use, it all ends up as the same bytes inside the machine. However, if you must get the \u version for some reason, then you can say things like this (assuming that your encoding starts out right):
ascii_friendly = str.chars.map { |c| '\u%4.4x' % c.ord }.join
Then when you print ascii_friendly to the screen, a file, or say a JSON stream, you'll see things like
\u5546\u54c1\u7f16\u53f7
Note that the \u5546 in there is not the single Unicode 商, it is the six characters \, u, 5, 5, 4, and 6. If your target is JSON, then the \u escapes will be interpreted properly when the JSON is parsed but if your target is anything else, it will just see the six characters rather than the single Unicode character you're looking for.

SSN Regular Expressions in Ruby [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm just starting to learn Regular Expressions. My code works, but I'd like to make it better.
# Ensure all of the Social Security numbers use dashes for delimiters.
# Example: 480.01.4430 and 480014430 would both be 480-01-4430.
def format_ssns(string)
string.gsub!(/\./, '-') if string =~ /(\d{9})|(\d{3})\D(\d{2})\D(\d{4})/
string.insert(3, '-') if string =~ /(\d{9})/
string.insert(6, '-') if string =~ /(\d{3})\D(\d{6})/
p string
end
format_ssns("234601422, 350.80.0744, 013-60-8762")
This covers all 3:
string = "234601422, 350.80.0744, 013-60-8762"
string.gsub /\b(\d{3})\D?(\d{2})\D?(\d{4})\b/, '\1-\2-\3'
#=> "234-60-1422, 350-80-0744, 013-60-8762"
how about s.gsub!(/\D/, '').insert(3, '-').insert(6, '-')

Convert string using Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I convert the string russ(ai)(edocn)cup to russiancodecup using Ruby?
By using gsub with a block, you can replace any match of a regular expression by the result of this block.
s = "russ(ai)(edocn)cup"
s.gsub(/\(([^)]*)\)/) {$1.reverse} # => "russiancodecup"
Here the regular expression will match any non-) character between brackets. Then it will send reverse to $1 which is gonna be the content between brackets.
$0 will be the complete match and $n, the nth "submatch". (anybody for the correct word ?)

Resources