Converting a binary value to hexadecimal in Ruby
How about:
>> "0x%02x" % "0000111".to_i(2) #=> "0x07"
>> "0x%02x" % "010001111".to_i(2) #=> "0x8f"
Edit: if you don't want the output to be 0x..
but just 0..
leave out the first x
in the format string.
How do I convert a 32-bit binary string to float in ruby
Does this work?
t = <binary-string-returned-here>
result = (t[2..3] + t[0..1]).unpack('F')[0]
ruby - How can I generate an array of every combination of letters and numbers of a given length?
alphanum = [*?a..?z, *?0..?9]
length.times.flat_map { |l|
alphanum.repeated_permutation(l + 1).map(&:join)
}
Note that length > 3
will give you a lot of results.
EDIT: As meagar says, this is very memory-intensive. An Enumerator-based answer (not as pretty, but won't kill your memory):
e = Enumerator.new do |y|
length.times do |l|
alphanum.repeated_permutation(l + 1).each do |p|
y << p.join
end
end
end
Convert Hex Chars into a String of Bits (Python or Ruby)
In Ruby:
yourbits = "026219AAF80F440206025019AAF816A944274480"[-2,2].to_i(16).to_s(2)
=> "10000000"
puts yourbits
10000000
=> nil
Explaining each step -- first, slice the string to get the last 2 characters:
"026219AAF80F440206025019AAF816A944274480"[-2,2]
=> '80'
Convert it to Integer:
"80".to_i(16)
=> 128
Back to string againg, in a binary representation:
128.to_s(2)
=> '10000000'
There's probably a better (or less confusing) way of doing it, but I can't imagine it right now.
Code golf - hex to (raw) binary conversion
edit Checkers has reduced my C solution to 46 bytes, which was then reduced to 44 bytes thanks to a tip from BillyONeal plus a bugfix on my part (no more infinite loop on bad input, now it just terminates the loop). Please give credit to Checkers for reducing this from 77 to 46 bytes:
main(i){while(scanf("%2x",&i)>0)putchar(i);}
And I have a much better Ruby solution than my last, in 42 38 bytes (thanks to Joshua Swank for the regexp suggestion):
STDIN.read.scan(/\S\S/){|x|putc x.hex}
original solutions
C, in 77 bytes, or two lines of code (would be 1 if you could put the #include
on the same line). Note that this has an infinite loop on bad input; the 44 byte solution with the help of Checkers and BillyONeal fixes the bug, and simply stops on bad input.
#include <stdio.h>
int main(){char c;while(scanf("%2x",&c)!=EOF)putchar(c);}
It's even just 6 lines if you format it normally:
#include <stdio.h>
int main() {
char c;
while (scanf("%2x",&c) != EOF)
putchar(c);
}
Ruby, 79 bytes (I'm sure this can be improved):
STDOUT.write STDIN.read.scan(/[^\s]\s*[^\s]\s*/).map{|x|x.to_i(16)}.pack("c*")
These both take input from STDIN and write to STDOUT
How can I reliably transpose an array of chars to an array of uint64_t and back again
In this answer I will address the actual question:
having problems obtaining 64 bits blocks from the input file
...rather than the one addressed so far:
after reading my file into a buffer, my CharArrayToInt64() function replaces large portions of the input buffer with F.
I recommend (IMHO) that you read the file directly into a uint64_t
array / buffer, rather than copy the data from a byte array.
While an unsigned char
array you might encounter memory alignment concerns, a uint64_t
array is guaranteed to be correctly memory aligned and you only need to worry about padding...
...Oh, and please use the unsigned
versions, as indicated by others on this thread, this is what's causing the shifting issues.
Once both arrays (encryption and decryption) use the same unsigned type, your code will look much cleaner.
Good luck.
How to add up all objects (sum) of an array (with decimal) and display result in UILabel?
Your sum
variable is an int
type, so the decimal portion will be truncated (not rounded- simply dropped off). Instead, sum
should be a double
or float
type.
float sum=0;
for(int x=0; x < [priceArray count]; x++)
{
sum += [[priceArray objectAtIndex:x] floatValue];
}
budgetLabel.text = [NSString stringWithFormat:@"$%.2f", sum];
Related Topics
Make Rails Ignore Daylight Saving Time When Displaying a Date
How to Specify the Location of the Chromedriver Binary
How to Split a String by Commas Except Inside Parenthesis, Using a Regular Expression
How to Get the Final Url After Redirects Using Ruby
How to Upload a File with Watir and Ie
Wrapping Text into Lines at Word Boundaries
Eliminate Consecutive Duplicates of List Elements
Rvm and Osx Lion - Rvm 'Forgets' Gemsets on System Restart
How to Handle Method Order in Ruby
Dynamic Active Record Store Accessors Based Off a User Form
How to Store an Instance Variable Across Multiple Actions in a Controller
Automatically Open a File as Binary with Ruby
Execjs and Could Not Find a JavaScript Runtime
How to Decode Q-Encoded Strings in Ruby
How Does Activerecord Define Methods Compared to Attr_Accessor