Convert subnet mask to cidr notation (Scala) - scala

Here's what I have so far:
//converts IP to a decimal or decimal to IP (working)
private def longToIPv4(ip:Long): String = (for(a<-3 to 0 by -1) yield ((ip>>(a*8))&0xff).toString).mkString(".")
private def IPv4ToLong(ip: String): Long = ip.split("\\.").reverse.zipWithIndex.map(a => a._1.toInt * math.pow(256, a._2).toLong).sum
//convert subnet to cidr (to do)
if(ip.matches("""(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})""")){
val pattern = """(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})""".r
val pattern(o1, o2, o3, o4, o5, o6, o7, o8) = ip
}
I'm able to parse the subnet into octets and convert IP to decimal and vice versa, now I just need to convert subnet to cidr notation.

According to your example in the comment, I assumed that the conversions is as elaborated here. I wouldn't do it with regex. You can try something like:
def cidrToSubnet(cidr: Int): String = {
require(cidr > 0, "CIDR is out of range! It must be between 1 (inclusive) to 32 (inclusive).")
require(cidr < 33, "CIDR is out of range! It must be between 1 (inclusive) to 32 (inclusive).")
val ipInInt = if (cidr == 32) (Int.MaxValue << 1) + 1 else Integer.MAX_VALUE << (32 - cidr)
ipInInt
.toBinaryString
.grouped(8)
.map(bitString => Integer.parseInt(bitString, 2))
.mkString(".")
}
def subnetToCidr(subnet: String): Int = {
32 - subnet.split('.')
.map(Integer.parseInt)
.reverse
.zipWithIndex
.map {
case (value, index) =>
value << index * 8
}
.sum
.toBinaryString
.count(_ == '0')
}
Then running:
1.to(32).foreach(i => {
println(i + ": cidrToSubnet(i): " + cidrToSubnet(i) + " subnetToCidr(cidrToSubnet(i): " + subnetToCidr(cidrToSubnet(i)))
})
outputs:
1: cidrToSubnet(i): 128.0.0.0 subnetToCidr(cidrToSubnet(i): 1
2: cidrToSubnet(i): 192.0.0.0 subnetToCidr(cidrToSubnet(i): 2
3: cidrToSubnet(i): 224.0.0.0 subnetToCidr(cidrToSubnet(i): 3
4: cidrToSubnet(i): 240.0.0.0 subnetToCidr(cidrToSubnet(i): 4
5: cidrToSubnet(i): 248.0.0.0 subnetToCidr(cidrToSubnet(i): 5
6: cidrToSubnet(i): 252.0.0.0 subnetToCidr(cidrToSubnet(i): 6
7: cidrToSubnet(i): 254.0.0.0 subnetToCidr(cidrToSubnet(i): 7
8: cidrToSubnet(i): 255.0.0.0 subnetToCidr(cidrToSubnet(i): 8
9: cidrToSubnet(i): 255.128.0.0 subnetToCidr(cidrToSubnet(i): 9
10: cidrToSubnet(i): 255.192.0.0 subnetToCidr(cidrToSubnet(i): 10
11: cidrToSubnet(i): 255.224.0.0 subnetToCidr(cidrToSubnet(i): 11
12: cidrToSubnet(i): 255.240.0.0 subnetToCidr(cidrToSubnet(i): 12
13: cidrToSubnet(i): 255.248.0.0 subnetToCidr(cidrToSubnet(i): 13
14: cidrToSubnet(i): 255.252.0.0 subnetToCidr(cidrToSubnet(i): 14
15: cidrToSubnet(i): 255.254.0.0 subnetToCidr(cidrToSubnet(i): 15
16: cidrToSubnet(i): 255.255.0.0 subnetToCidr(cidrToSubnet(i): 16
17: cidrToSubnet(i): 255.255.128.0 subnetToCidr(cidrToSubnet(i): 17
18: cidrToSubnet(i): 255.255.192.0 subnetToCidr(cidrToSubnet(i): 18
19: cidrToSubnet(i): 255.255.224.0 subnetToCidr(cidrToSubnet(i): 19
20: cidrToSubnet(i): 255.255.240.0 subnetToCidr(cidrToSubnet(i): 20
21: cidrToSubnet(i): 255.255.248.0 subnetToCidr(cidrToSubnet(i): 21
22: cidrToSubnet(i): 255.255.252.0 subnetToCidr(cidrToSubnet(i): 22
23: cidrToSubnet(i): 255.255.254.0 subnetToCidr(cidrToSubnet(i): 23
24: cidrToSubnet(i): 255.255.255.0 subnetToCidr(cidrToSubnet(i): 24
25: cidrToSubnet(i): 255.255.255.128 subnetToCidr(cidrToSubnet(i): 25
26: cidrToSubnet(i): 255.255.255.192 subnetToCidr(cidrToSubnet(i): 26
27: cidrToSubnet(i): 255.255.255.224 subnetToCidr(cidrToSubnet(i): 27
28: cidrToSubnet(i): 255.255.255.240 subnetToCidr(cidrToSubnet(i): 28
29: cidrToSubnet(i): 255.255.255.248 subnetToCidr(cidrToSubnet(i): 29
30: cidrToSubnet(i): 255.255.255.252 subnetToCidr(cidrToSubnet(i): 30
31: cidrToSubnet(i): 255.255.255.254 subnetToCidr(cidrToSubnet(i): 31
32: cidrToSubnet(i): 255.255.255.255 subnetToCidr(cidrToSubnet(i): 32
Code run at Scastie.

To me it looks like you want to convert something like 1.2.3.0/255.255.255.0 to 1.2.3.0/24
With the The IPAddress Java library, it is trivial because it parses address/mask notation and can print addresses in many formats, the default format being CIDR notation. Disclaimer: I am the project manager.
import inet.ipaddr.IPAddressString
def convert(arg: String) {
val ipaddStr = new IPAddressString(arg)
println(ipaddStr.getAddress)
}
convert("1.2.3.0/255.255.255.0")
Output:
1.2.3.0/24

Related

how can I replace letter to certain number [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 4 years ago.
Improve this question
I have a huge data like the following
NDDDDTSVCLGTRQCSWFAGCTNRTWNSSA 0
VCLGTRQCSWFAGCTNRTWNSSAVPLIGLP 0
LTWSGNDTCLYSCQNQTKGLLYQLFRNLFC 0
CQNQTKGLLYQLFRNLFCSYGLTEAHGKWR 0
ITNDKGHDGHRTPTWWLTGSNLTLSVNNSG 0
GHRTPTWWLTGSNLTLSVNNSGLFFLCGNG 0
FLCGNGVYKGFPPKWSGRCGLGYLVPSLTR 0
KGFPPKWSGRCGLGYLVPSLTRYLTLNASQ 0
QSVCMECQGHGERISPKDRCKSCNGRKIVR 1
I want to use the following key to replace the letter with numbers
A 1
R 2
N 3
D 4
B 5
C 6
E 7
Q 8
Z 9
G 10
H 11
I 12
L 13
K 14
M 15
F 16
P 17
S 18
T 19
W 20
Y 21
V 22
at first I want to remove all the numbers close to letter and then replace the letters , so lets look at the first like
NDDDDTSVCLGTRQCSWFAGCTNRTWNSSA
will have this
3 4 4 4 4 19 18 22 6 19 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 18 1
and for the rest of lines the same as many lines as I have
perl -e'
use autodie;
my %charmap = (
A => 1, R => 2, N => 3, D => 4, B => 5, C => 6, E => 7, Q => 8,
Z => 9, G => 10, H => 11, I => 12, L => 13, K => 14, M => 15, F => 16,
P => 17, S => 18, T => 19, W => 20, Y => 21, V => 22,
);
while (<>) {
s{(.)}{ ($charmap{$1} // $1) . " " }ge;
print;
}
' file
Or just
perl -pe'
BEGIN { #charmap{ split //, "ARNDBCEQZGHILKMFPSTWYV" } = 1..22 }
s{(.)}{ ($charmap{$1} // $1) . " " }ge;
' file
With any awk in any shell on any UNIX box:
$ cat tst.awk
BEGIN {
chars = "ARNDBCEQZGHILKMFPSTWYV"
for (i=1; i<=length(chars); i++) {
char = substr(chars,i,1)
map[char] = i
}
}
{
out = ""
chars = $1
for (i=1; i<=length(chars); i++) {
char = substr(chars,i,1)
out = (out == "" ? "" : out " ") (char in map ? map[char] : char)
}
print out
}
$ awk -f tst.awk file
3 4 4 4 4 19 18 22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 18 1
22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 18 1 22 17 13 12 10 13 17
13 19 20 18 10 3 4 19 6 13 21 18 6 8 3 8 19 14 10 13 13 21 8 13 16 2 3 13 16 6
6 8 3 8 19 14 10 13 13 21 8 13 16 2 3 13 16 6 18 21 10 13 19 7 1 11 10 14 20 2
12 19 3 4 14 10 11 4 10 11 2 19 17 19 20 20 13 19 10 18 3 13 19 13 18 22 3 3 18 10
10 11 2 19 17 19 20 20 13 19 10 18 3 13 19 13 18 22 3 3 18 10 13 16 16 13 6 10 3 10
16 13 6 10 3 10 22 21 14 10 16 17 17 14 20 18 10 2 6 10 13 10 21 13 22 17 18 13 19 2
14 10 16 17 17 14 20 18 10 2 6 10 13 10 21 13 22 17 18 13 19 2 21 13 19 13 3 1 18 8
8 18 22 6 15 7 6 8 10 11 10 7 2 12 18 17 14 4 2 6 14 18 6 3 10 2 14 12 22 2
Alternative Perl solution:
#!/usr/bin/perl
use strict;
use warnings;
my %key = (
A => 1, R => 2, N => 3, D => 4, B => 5,
C => 6, E => 7, Q => 8, Z => 9, G => 10,
H => 11, I => 12, L => 13, K => 14, M => 15,
F => 16, P => 17, S => 18, T => 19, W => 20,
Y => 21, V => 22,
);
while (<STDIN>) {
my($text) = /^(\w+)/;
print join(' ',
map { $key{$_} }
split(//, $text)
), "\n";
}
exit 0;
Output with your given text:
$ perl dummy.pl <dummy.txt
3 4 4 4 4 19 18 22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 18 1
22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 18 1 22 17 13 12 10 13 17
13 19 20 18 10 3 4 19 6 13 21 18 6 8 3 8 19 14 10 13 13 21 8 13 16 2 3 13 16 6
6 8 3 8 19 14 10 13 13 21 8 13 16 2 3 13 16 6 18 21 10 13 19 7 1 11 10 14 20 2
12 19 3 4 14 10 11 4 10 11 2 19 17 19 20 20 13 19 10 18 3 13 19 13 18 22 3 3 18 10
10 11 2 19 17 19 20 20 13 19 10 18 3 13 19 13 18 22 3 3 18 10 13 16 16 13 6 10 3 10
16 13 6 10 3 10 22 21 14 10 16 17 17 14 20 18 10 2 6 10 13 10 21 13 22 17 18 13 19 2
14 10 16 17 17 14 20 18 10 2 6 10 13 10 21 13 22 17 18 13 19 2 21 13 19 13 3 1 18 8
8 18 22 6 15 7 6 8 10 11 10 7 2 12 18 17 14 4 2 6 14 18 6 3 10 2 14 12 22 2
On second thought...
As OP wants to obfuscate clear text then the more appropriate solution IMHO should be something like this:
$ bash <dummy.txt -c "$(echo /Td6WFoAAATm1rRGBMCtAbgBIQEWAAAAAAAAACsG0SbgALcApV0AOBlKq3igoJRmX9TqJifIRDIcDLdDtNRSv+tJBsifrrsdnlllNt2qqnlz0/uBmSnlO0FTKjKH/HXplJm9LaV7kXiNp/ZWDsyVqoV8EPjIEHHkXXd6jKahyq7tcCA4NGTHp/pwmk8jith6j/dcX67QCKmL0UtZUz9BqVWefD41lbrTNazbD8IP6zMLmAVxJav51SSTHzsUqhUfqhVmLsUg8sJkgloAAAAAAOMYtQXt21WNAAHJAbgBAABTvtYRscRn+wIAAAAABFla | base64 -d | xzcat)"
3 4 4 4 4 19 18 22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 18 1
22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 18 1 22 17 13 12 10 13 17
13 19 20 18 10 3 4 19 6 13 21 18 6 8 3 8 19 14 10 13 13 21 8 13 16 2 3 13 16 6
6 8 3 8 19 14 10 13 13 21 8 13 16 2 3 13 16 6 18 21 10 13 19 7 1 11 10 14 20 2
12 19 3 4 14 10 11 4 10 11 2 19 17 19 20 20 13 19 10 18 3 13 19 13 18 22 3 3 18 10
10 11 2 19 17 19 20 20 13 19 10 18 3 13 19 13 18 22 3 3 18 10 13 16 16 13 6 10 3 10
16 13 6 10 3 10 22 21 14 10 16 17 17 14 20 18 10 2 6 10 13 10 21 13 22 17 18 13 19 2
14 10 16 17 17 14 20 18 10 2 6 10 13 10 21 13 22 17 18 13 19 2 21 13 19 13 3 1 18 8
8 18 22 6 15 7 6 8 10 11 10 7 2 12 18 17 14 4 2 6 14 18 6 3 10 2 14 12 22 2
another awk
$ awk 'NR==FNR {a[$1]=$2; next}
{n=length($1);
for(i=1;i<=n;i++)
printf "%s", a[substr($1,i,1)] (i==n?ORS:OFS)}' mapfile datafile
3 4 4 4 4 19 18 22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 18 1
22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 18 1 22 17 13 12 10 13 17
13 19 20 18 10 3 4 19 6 13 21 18 6 8 3 8 19 14 10 13 13 21 8 13 16 2 3 13 16 6
6 8 3 8 19 14 10 13 13 21 8 13 16 2 3 13 16 6 18 21 10 13 19 7 1 11 10 14 20 2
12 19 3 4 14 10 11 4 10 11 2 19 17 19 20 20 13 19 10 18 3 13 19 13 18 22 3 3 18 10
10 11 2 19 17 19 20 20 13 19 10 18 3 13 19 13 18 22 3 3 18 10 13 16 16 13 6 10 3 10
16 13 6 10 3 10 22 21 14 10 16 17 17 14 20 18 10 2 6 10 13 10 21 13 22 17 18 13 19 2
14 10 16 17 17 14 20 18 10 2 6 10 13 10 21 13 22 17 18 13 19 2 21 13 19 13 3 1 18 8
8 18 22 6 15 7 6 8 10 11 10 7 2 12 18 17 14 4 2 6 14 18 6 3 10 2 14 12 22 2
however, there is no provision of missing mappings that are not specified, i.e. if you have chars not listed on the mapfile they will be ignored.
If the goal is encryption I'll propose a different approach:
First let's generate a mapping (or encryption key)
$ key=$(printf "%s\n" {A..Z} | shuf | paste -sd' ' | tr -d ' ')
$ echo "$key"
CNYSGFRDKQTOXJVLEWBAHZPMUI
now you can encrypt/decrypt your file contents, simply
$ tr [A-Z] "$key" < datafile > file.encrypted
and to reverse
$ tr "$key" [A-Z] < file.encrypted > file.decrypted
obviously, you need to save the key.

Postgres Case update syntax error

I've done this update script:
UPDATE etude
SET id_enseigne
(CASE WHEN id_enseigne= 1 THEN 6
ELSE CASE WHEN id_enseigne= 1 THEN 6
ELSE CASE WHEN id_enseigne= 2 THEN 26
ELSE CASE WHEN id_enseigne= 3 THEN 2122
ELSE CASE WHEN id_enseigne= 4 THEN 1960
ELSE CASE WHEN id_enseigne= 5 THEN 84
ELSE CASE WHEN id_enseigne= 6 THEN 103
ELSE CASE WHEN id_enseigne= 7 THEN 56
ELSE CASE WHEN id_enseigne= 8 THEN 108
ELSE CASE WHEN id_enseigne= 9 THEN 68
ELSE CASE WHEN id_enseigne= 10 THEN 489
ELSE CASE WHEN id_enseigne= 11 THEN 1124
ELSE CASE WHEN id_enseigne= 13 THEN 502
ELSE CASE WHEN id_enseigne= 14 THEN 1298
ELSE 0 END)
But I get this error, I don't understand why, because it is supposed to be simple:
ERROR: syntax error at or near "("
LINE 3: (CASE WHEN id_enseigne= 1 THEN 6
^
********** Erreur **********
ERROR: syntax error at or near "("
État SQL :42601
Caractère : 30
This other case syntax is more convenient:
update etude
set id_enseigne = case id_enseigne
when 1 then 6
when 2 then 26
when 3 then 2122
when 4 then 1960
when 5 then 84
when 6 then 103
when 7 then 56
when 8 then 108
when 9 then 68
when 10 then 489
when 11 then 1124
when 13 then 502
when 14 then 1298
else 0
end
You're missing the assignment operator. You could keep or lose the parenthesis though.
UPDATE etude
SET id_enseigne = CASE ...
-- Here -----------^

Why my Swift code explodes when comparing upper to lower case?

I'm actually learning swift and I'm testing the switch control flow. I try this code on IBM Swift Sandbox:
let const = "Z"
switch const {
case "a" ,
"A": print("La primera letra del abecedario")
case "z": print("La ultima letra del abecedario")
default: print("NPI de que letra se trate")
}
It all works fine, tested with const = {"a","A","f","z"}. But when const is "Z" i get this error:
terminate called after throwing an instance of 'std::system_error'
what(): Resource temporarily unavailable
0 swift-build-tool 0x000000000050bade llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 46
1 swift-build-tool 0x000000000050ca49
2 swift-build-tool 0x000000000050cd53
3 libpthread.so.0 0x00007f960ca22330
4 libc.so.6 0x00007f960bc5fc37 gsignal + 55
5 libc.so.6 0x00007f960bc63028 abort + 328
6 libstdc++.so.6 0x00007f960c56a535 __gnu_cxx::__verbose_terminate_handler() + 341
7 libstdc++.so.6 0x00007f960c5686d6
8 libstdc++.so.6 0x00007f960c568703
9 libstdc++.so.6 0x00007f960c568922
10 libstdc++.so.6 0x00007f960c5ba800 std::__throw_system_error(int) + 128
11 libstdc++.so.6 0x00007f960c5bbd68 std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) + 584
12 swift-build-tool 0x0000000000494d6d
13 swift-build-tool 0x0000000000494b0d
14 swift-build-tool 0x0000000000494a01 llbuild::buildsystem::createLaneBasedExecutionQueue(llbuild::buildsystem::BuildExecutionQueueDelegate&, int) + 49
15 swift-build-tool 0x0000000000490ba8 llbuild::buildsystem::BuildSystemFrontendDelegate::createExecutionQueue() + 264
16 swift-build-tool 0x00000000004b98e9
17 swift-build-tool 0x00000000004b9823 llbuild::buildsystem::BuildSystem::build(llvm::StringRef) + 67
18 swift-build-tool 0x000000000049150d llbuild::buildsystem::BuildSystemFrontend::build(llvm::StringRef) + 1405
19 swift-build-tool 0x000000000048e086
20 swift-build-tool 0x000000000048dc7a main + 170
21 libc.so.6 0x00007f960bc4af45 __libc_start_main + 245
22 swift-build-tool 0x000000000048db04
swift-build: error: Child process exited with signal
Trying your code in the Sandbox now, it looks like it works just fine with const = "Z". This was an error on the Sandbox end, it looks like. We were having issues with a handful of our servers over the holidays, and we're working on getting them fixed. Your code seems to be correct.

exc_bad_access when advancing simulation on device

This error only occurs on the actual device. When running on the simulator in Xcode 6, it does not crash.
I am creating outer space and have random shooting stars streak across the sky.
In the scene, [goToSpace] is called which sets up the starfield by using three SKEmitterNodes to simulate depth. Then a timer is initialized which sends stars shooting.
In the initialization of the starfield, it is necessary to advance the simulation of the three SKEmitterNodes to get a full sky of stars. When the line [emitterNode1 advanceSimulationTime: lifetime] is called, I get exc_bad_access. I followed tutorials to enable NSZombie which did not help.
I can't for the life of my track down why I am getting exc_bad_access. I use Parse.com's Crash Reporting which gives this stack:
> Stack Trace Thread 0 (crashed) 0 libsystem_platform.dylib
> OSSpinLockLock # 0x4 1 SpriteKit SKSpinLockSync(int*, void ()
> block_pointer) # 0x20 2 SpriteKit
> -[SKTexture loadImageData] # 0xec 3 SpriteKit
> -[SKTexture size] # 0x28 4 SpriteKit SKCEmitterSprite::update(double) # 0x117c 5 CorpBoard # 0x48d44 6 CorpBoard # 0xfbc50 7 CorpBoard #
> 0xfa6f0 8 CorpBoard # 0xfa070 9 UIKit
> -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] # 0x21c 10 UIKit
> -[UITableView _updateVisibleCellsNow:isRecursive:] # 0x934 11 UIKit
> -[UITableView layoutSubviews] # 0xa8 12 UIKit
> -[UIView(CALayerDelegate) layoutSublayersOfLayer:] # 0x238 13 QuartzCore
> -[CALayer layoutSublayers] # 0xa4 14 QuartzCore CA::Layer::layout_if_needed(CA::Transaction*) # 0x13c 15 QuartzCore
> CA::Layer::layout_and_display_if_needed(CA::Transaction*) # 0x1c 16
> QuartzCore CA::Context::commit_transaction(CA::Transaction*) # 0x110
> 17 QuartzCore CA::Transaction::commit() # 0x1b0 18 QuartzCore
> CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned
> long, void*) # 0x4c 19 CoreFoundation
> __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ # 0x1c 20 CoreFoundation
> __CFRunLoopDoObservers # 0x164 21 CoreFoundation
> __CFRunLoopRun # 0x340 22 CoreFoundation CFRunLoopRunSpecific # 0x188 23 GraphicsServices GSEventRunModal # 0xa4 24 UIKit UIApplicationMain
> # 0x5cc 25 CorpBoard # 0x5be80 26 libdyld.dylib start # 0x0 Thread 1 0
> libsystem_kernel.dylib kevent64 # 0x8 1 libdispatch.dylib
> _dispatch_mgr_invoke # 0x110 2 libdispatch.dylib
> _dispatch_mgr_thread # 0x30 Thread 2 0 libsystem_kernel.dylib
> __workq_kernreturn # 0x8 1 libsystem_pthread.dylib
> _pthread_wqthread # 0x3dc 2 libsystem_pthread.dylib start_wqthread # 0x0 Thread 3 0 libsystem_kernel.dylib
> __workq_kernreturn # 0x8 1 libsystem_pthread.dylib
> _pthread_wqthread # 0x3dc 2 libsystem_pthread.dylib start_wqthread # 0x0 Thread 4 0 libsystem_kernel.dylib
> __workq_kernreturn # 0x8 1 libsystem_pthread.dylib
> _pthread_wqthread # 0x3dc 2 libsystem_pthread.dylib start_wqthread # 0x0 Thread 5 0 libsystem_kernel.dylib
> __workq_kernreturn # 0x8 1 libsystem_pthread.dylib
> _pthread_wqthread # 0x3dc 2 libsystem_pthread.dylib start_wqthread # 0x0 Thread 6 0 libsystem_kernel.dylib
> __workq_kernreturn # 0x8 1 libsystem_pthread.dylib
> _pthread_wqthread # 0x3dc 2 libsystem_pthread.dylib start_wqthread # 0x0 Thread 7 0 libsystem_kernel.dylib mach_msg_trap # 0x8 1
> libsystem_kernel.dylib mach_msg # 0x44 2 CoreFoundation
> __CFRunLoopServiceMachPort # 0xc4 3 CoreFoundation
> __CFRunLoopRun # 0x3a8 4 CoreFoundation CFRunLoopRunSpecific # 0x188 5 Foundation
> -[NSRunLoop(NSRunLoop) runMode:beforeDate:] # 0x138 6 Foundation
> -[NSRunLoop(NSRunLoop) run] # 0x5c 7 CorpBoard # 0x206a28 8 Foundation
> __NSThread__main__ # 0x42c 9 libsystem_pthread.dylib
> _pthread_body # 0xa0 10 libsystem_pthread.dylib
> _pthread_start # 0x9c 11 libsystem_pthread.dylib thread_start # 0x0 Thread 8 0 libsystem_kernel.dylib mach_msg_trap # 0x8 1
> libsystem_kernel.dylib mach_msg # 0x44 2 CoreFoundation
> __CFRunLoopServiceMachPort # 0xc4 3 CoreFoundation
> __CFRunLoopRun # 0x3a8 4 CoreFoundation CFRunLoopRunSpecific # 0x188 5 CFNetwork
> +[NSURLConnection(Loader) _resourceLoadLoop:] # 0x1b4 6 Foundation
> __NSThread__main__ # 0x42c 7 libsystem_pthread.dylib
> _pthread_body # 0xa0 8 libsystem_pthread.dylib
> _pthread_start # 0x9c 9 libsystem_pthread.dylib thread_start # 0x0 Thread 9 0 libsystem_kernel.dylib
> __select # 0x8 1 CoreFoundation
> __CFSocketManager # 0x290 2 libsystem_pthread.dylib
> _pthread_body # 0xa0 3 libsystem_pthread.dylib
> _pthread_start # 0x9c 4 libsystem_pthread.dylib thread_start # 0x0 Thread 10 0 libsystem_kernel.dylib mach_msg_trap # 0x8 1
> libsystem_kernel.dylib mach_msg # 0x44 2 CoreFoundation
> __CFRunLoopServiceMachPort # 0xc4 3 CoreFoundation
> __CFRunLoopRun # 0x3a8 4 CoreFoundation CFRunLoopRunSpecific # 0x188 5 CoreFoundation CFRunLoopRun # 0x6c 6 CoreMotion # 0x4298c 7
> libsystem_pthread.dylib
> _pthread_body # 0xa0 8 libsystem_pthread.dylib
> _pthread_start # 0x9c 9 libsystem_pthread.dylib thread_start # 0x0 Thread 11 0 libsystem_kernel.dylib
> __workq_kernreturn # 0x8 1 libsystem_pthread.dylib
> _pthread_wqthread # 0x3dc 2 libsystem_pthread.dylib start_wqthread # 0x0
Related code:
NSTimer *space;
-(void)goToSpace {
[self shootingStar];
double lifetime;
SKEmitterNode *emitterNode1 = [self starFieldEmitter:[SKColor lightGrayColor] starSpeedY:1 starsPerSecond:.1 starScaleFactor:0.08];
lifetime = self.frame.size.height * [[UIScreen mainScreen] scale] / 1;
[emitterNode1 advanceSimulationTime:lifetime];
emitterNode1.zPosition = -10;
[self addChild:emitterNode1];
SKEmitterNode *emitterNode2 = [self starFieldEmitter:[SKColor lightGrayColor] starSpeedY:.8 starsPerSecond:.08 starScaleFactor:0.06];
emitterNode2.zPosition = -11;
lifetime = self.frame.size.height * [[UIScreen mainScreen] scale] / .8;
[emitterNode2 advanceSimulationTime:lifetime];
[self addChild:emitterNode2];
SKEmitterNode *emitterNode3 = [self starFieldEmitter:[SKColor grayColor] starSpeedY:.5 starsPerSecond:.5 starScaleFactor:0.03];
emitterNode3.zPosition = -12;
lifetime = self.frame.size.height * [[UIScreen mainScreen] scale] / .5;
[emitterNode3 advanceSimulationTime:lifetime];
[self addChild:emitterNode3];
space = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(shootingStar) userInfo:nil repeats:YES];
}
-(SKEmitterNode *)starFieldEmitter:(SKColor *)color starSpeedY:(CGFloat)starSpeedY starsPerSecond:(CGFloat)starsPerSecond starScaleFactor:(CGFloat) starScaleFactor {
SKEmitterNode *emitterNode = [SKEmitterNode node];
CGFloat lifetime = self.frame.size.height * [[UIScreen mainScreen] scale] / starSpeedY;
emitterNode.particleTexture = [SKTexture textureWithImage:[UIImage imageNamed:#"stars"]];
emitterNode.particleBirthRate = starsPerSecond;
emitterNode.particleColor = [SKColor lightGrayColor];
emitterNode.particleSpeed = starSpeedY * -1;
emitterNode.particleScale = starScaleFactor;
emitterNode.particleColorBlendFactor = 1;
emitterNode.particleLifetime = lifetime;
int rndValue = 1 + arc4random() % (45 - 1);
emitterNode.particleRotation = rndValue;
emitterNode.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height);
emitterNode.particlePositionRange = CGVectorMake(self.frame.size.width, self.frame.size.height);
return emitterNode;
}
-(BOOL) getYesOrNo {
int tmp = (arc4random() % 30)+1;
if(tmp % 5 == 0)
return YES;
return NO;
}
-(void)shootingStar {
BOOL left = [self getYesOrNo];
int xPos = 0;
int height = self.frame.size.height;
int yPos = 1 + arc4random() % (height - 1);
if (left) {
xPos = -50;
} else {
xPos = self.frame.size.width + 50;
}
SKEmitterNode *shootingstar = [SKEmitterNode node];
shootingstar = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:#"spark" ofType:#"sks"]];
CGPoint xy = CGPointMake(xPos, yPos);
shootingstar.position = xy;
shootingstar.name = #"shootingStar";
shootingstar.zPosition = -2.0;
shootingstar.targetNode = self.scene;
[self addChild:shootingstar];
//make random size to simulate distance
float val = [self randFloatBetween:.1 and:.5];
SKAction *scale = [SKAction scaleBy:val duration:0];
[shootingstar runAction: scale completion:nil];
//now set speed depending on size (smaller = farther = slower)
int dur = 0;
if (val < .2) dur = 5;
else if (val < .35) dur = 3;
else dur = 2;
int moveY = -500 + arc4random() % (500 - -500);
int moveX;
if (left) {
moveX = 500;
} else {
moveX = -500;
}
SKAction *move = [SKAction moveByX:moveX y:moveY duration:dur];
int waitDuration = 2 + arc4random() % (5 - 2);
SKAction *wait = [SKAction waitForDuration:waitDuration];
SKAction *sequence = [SKAction sequence:#[wait, move]];
[shootingstar runAction:sequence completion:^{
[shootingstar removeFromParent];
//[self shootingStar];
}];
}
-(float) randFloatBetween:(float)low and:(float)high {
float diff = high - low;
return (((float) rand() / RAND_MAX) * diff) + low;
}
I was able to resolve my issue by referencing a couple answers from: this similar question
In my case, the image I was using for the stars, Stars.png, was in my project directory. I moved it to Images.xcassets and added #2x and #3x versions of the image. This solved my problem, though I don't fully understand why.

mathematic of computers

I can not figure out the sequence of numbers between hexadecimal
288 and 2AO i really need help.
288 + 1 = 289
289 + 1 = 28A
...
28F + 1 = 290
290 + 1 = 291
...
29F + 1 = 2A0
You might want to know that even Windows calc.exe provides a HEX mode and that Google itself can do it :)
Read this for info on base-16 numeral system
Decimal:
$ seq 0x288 0x2A0
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
Hex:
# printf "%x\n" `seq 0x288 0x2A0`
288
289
28a
28b
28c
28d
28e
28f
290
291
292
293
294
295
296
297
298
299
29a
29b
29c
29d
29e
29f
2a0
Let's start with something simpler. What's the sequence of numbers between 32 and 45, in the 10 base, as you're used to?
After 32, there's 33, 34, 35... 39. And then, since the digits in base-10 are between 0 and 9, you advance to 40. The rightmost digit wraps back to 0, and the digit on its left becomes one bigger, thus giving you 40. From there you continue - 41,42,43,44,45.
Now, in other bases, its simply a matter of a different amount of digits. Let's take the same question (32->45), but in base 6. Base 6 has six digits - 0,1,2,3,4,5. So you go from 32 to 33, 34, 35, and here, just like you jumped from 39 to 40, you stop. There is no 36 in base 6 - you go from 5 to 0, and then you increment the left digit - hence 40. From there it's 41,42,43,44,45.
Now, with bases which are less than 10 (like base 6 above), it's easy - there are less digits. But what about base 11? base 64? or in your case, base 16? How would you represent the eleventh digit?
Here, the convention is simple. The digits turn into letters. These are the digits for base 16, the hexadecimal base:
0 1 2 3 4 5 6 7 8 9 A B C D E F
So the eleventh digit is A. The sixteenth digit is F. Let's go back to my first example but do it in the hexadecimal base. You start with 32. Go to 33, 34... 39, and then you proceed within the 30s with 3A, 3B, 3C, 3D, 3E, 3F, and here you wrap back to 0 - and jump to 40. Here is the complete sequence:
32,33,34,35,36,37,38,39,3A,3B,3C,3D,3E,3F,40,41,42,43,44,45
From here you should be able to solve 288-2A0 by yourself.
Good luck!
This C program will output the values:
#include <stdio.h>
int main() {
int i;
for(i=0x288; i<=0x2A0; i++)
printf("%X ", i);
printf("\n");
return 0;
}
Output: 288 289 28A 28B 28C 28D 28E 28F 290 291 292 293 294 295 296 297 298 299 29A 29B 29C 29D 29E 29F 2A0
Is this what you want?