Related
[51, -42, 119, -85, -64, 126, 22, 127, -72, 72, 48, -66, -18, 45, 99, -119]
This is the BytesArray that I want to print in String.
When I searched on the internet, I found that
String(Bytes, Charsets.UTF_8)
would convert it to String.
However, I get �؉���Q�t, and doesn't seem to be converted in right way.
Why is it?
I want it to be String in Alphabet characters and numbers
Firstly, you are specifying an array of signed bytes (indicated by negative numbers):
51, -42, 119, -85, -64, 126, 22, 127, -72, 72, 48, -66, -18, 45, 99, -119
Let's take a look at what this would hypothetically look like if it were unsigned (I used this tool for the conversion):
51, 214, 119, 171, 192, 126, 22, 127, 184, 72, 48, 190, 238, 45, 99, 137
Assuming by "Alphabet characters and numbers", you mean the English alphabet, then asciitable will help you identify each character's decimal value, but as a rough guide:
"0"-"9" = 48-57
"A"-"Z" = 65-90
"a"-"z" = 97-122
Consider the following code sample:
/**
* You can edit, run, and share this code.
* play.kotlinlang.org
*/
fun main() {
val bytes = byteArrayOf(51, -42, 119, -85, -64, 126, 22, 127, -72, 72, 48, -66, -18, 45, 99, -119)
val string = bytes.toString(Charsets.US_ASCII)
println(string)
}
As you can see, some of the values in the unsigned array fall outside of the range for English alphabetic characters and numbers, which is why you end up with a string, something like this "3�w��~�H0��-c�" depending on the charset you choose.
For reference:
Charset
Result
Charsets.US_ASCII
3�w��~�H0��-c�
Charsets.UTF_8
3�w��~�H0��-c�
Charsets.UTF_16
㏖瞫쁾ᙿ롈ゾ掉
Charsets.UTF_32
����
Charsets.ISO_8859_1
3Öw«À~¸H0¾î-c
So, it really depends on exactly which encoding the array is using, and exactly what it is you're expecting the resulting string to be.
You can play with the code above, here.
I am trying to create a pollution rose plot as described in the link Plotting Windrose: making a pollution rose with concentration set to color
Example in the reply is working but when I used my data then it is giving a weird plot. Any advice where I am going wrong? Thank you.
import matplotlib.pyplot as plt
import numpy as np
wd = [90.,297.,309.,336.,20.,2.,334.,327.,117.,125.,122.,97.,95.,97.,103.,106.,125.,148.,147.,140.,141.,145.,144.,151.,161.]
ws = [15,1.6,1.8,1.7,2.1,1.6,2.1,1.4,3,6.5,7.1,8.2,10.2,10.2,10.8,10.2,11.4,9.7,8.6,7.1,6.4,5.5,5,5,6]
oz = [10.,20.,30.,40.,50.,60.,70.,80.,90.,100.,110.,120.,90.,140.,100.,106.,125.,148.,147.,140.,141.,145.,144.,151.,161.]
pi_fac = 22/(7*180.)
wd_rad = [w * pi_fac for w in wd]
ws_r = np.linspace(min(ws),max(ws),16)
WD,WS = np.meshgrid(wd_rad,ws_r)
C = oz + np.zeros((len(ws_r),len(wd)),dtype=float)
C = np.ma.masked_less_equal(C,10)
fig, ax = plt.subplots(subplot_kw={"projection":"polar"})
ax.pcolormesh(WD,WS,C,vmin=10, vmax=170) # I tried different vmin and vmax too
plt.show()
The linked post assumes you have a regular grid for directions and for speeds, but your input seems to be quite unordered combinations.
To create a plot with colored regions depending on the oz values, you could try tricontourf. tricontourf takes in X, Y and Z values that don't need to lie on a grid and creates a contour plot. Although it is meant for rectangular layouts, it might also work for your case. It will have a discontinuity though, when crossing from 360º to 0º.
The plot of this example also draws a colorbar to show which range of oz values correspond to which color. vmin and vmax can change this mapping of colors.
import matplotlib.pyplot as plt
import numpy as np
wd = [90, 297, 309, 336, 20, 2, 334, 327, 117, 125, 122, 97, 95, 97, 103, 106, 125, 148, 147, 140, 141, 145, 144, 151, 161]
ws = [15, 1.6, 1.8, 1.7, 2.1, 1.6, 2.1, 1.4, 3, 6.5, 7.1, 8.2, 10.2, 10.2, 10.8, 10.2, 11.4, 9.7, 8.6, 7.1, 6.4, 5.5, 5, 5, 6]
oz = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 90, 140, 100, 106, 125, 148, 147, 140, 141, 145, 144, 151, 161]
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
cont = ax.tricontourf(np.radians(np.array(wd)), ws, oz, cmap='hot')
plt.colorbar(cont)
plt.show()
With ax.scatter(np.radians(np.array(wd)), ws, c=oz, cmap='hot', vmax=250) you could create a scatter plot to get an idea how the input looks like when colored.
You might want to incorporate Python's windrose library to get polar plots to resemble a windrose.
Another approach, which might be closer to the one intended by the linked question, would be to use scipy's interpolate.griddata to map the data to a grid. To get rid of the areas without data, an 'under' color of 'none' can be used, provided that vmin is higher than zero.
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
wd = [90, 297, 309, 336, 20, 2, 334, 327, 117, 125, 122, 97, 95, 97, 103, 106, 125, 148, 147, 140, 141, 145, 144, 151, 161]
ws = [15, 1.6, 1.8, 1.7, 2.1, 1.6, 2.1, 1.4, 3, 6.5, 7.1, 8.2, 10.2, 10.2, 10.8, 10.2, 11.4, 9.7, 8.6, 7.1, 6.4, 5.5, 5, 5, 6]
oz = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 90, 140, 100, 106, 125, 148, 147, 140, 141, 145, 144, 151, 161]
wd_rad = np.radians(np.array(wd))
oz = np.array(oz, dtype=np.float)
WD, WS = np.meshgrid(np.linspace(0, 2*np.pi, 36), np.linspace(min(ws), max(ws), 16 ))
Z = interpolate.griddata((wd_rad, ws), oz, (WD, WS), method='linear')
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
cmap = plt.get_cmap('hot')
cmap.set_under('none')
img = ax.pcolormesh(WD, WS, Z, cmap=cmap, vmin=20)
plt.colorbar(img)
plt.show()
what are the interpretation values in the"" points assignment. all I want is a "plane jane" line where I can manipulate the length and width. Any help will greatly appreciated.
var line = new Kinetic.Line({
x: 100,
y: 50,
points: [73, 70, 340, 23, 450, 60, 500, 20],
stroke: 'red',
tension: 1
});
The points array is a series of x,y coordinates:
// [73, 70, 340, 23, 450, 60, 500, 20],
{x:73,y:70},
{x:340,y:23},
{x:450,y:60},
{x:500,y:20}
This is your "plain jane" line:
// draw a black line from 25,25 to 100,50 and width of 5
var line = new Kinetic.Line({
points:[25,25, 100,50],
y:100,
stroke: 'black',
strokeWidth: 5
});
I made a multi-stop gradient mix-in that simply puts #arguments for all the vendor prefixes for *-linear-gradient. It works, put I'm annoyed when I define a gradient with many stops, I have to put everything on one line when using the mixin, like this:
.gradientMultiple(~'top, rgba(255, 255, 255, 1) 0%, rgba(254, 254, 254, 1) 16%, rgba(252, 252, 252, 1) 36%, rgba(239, 239, 239, 1) 66%, rgba(18, 34, 106, 1) 66%, rgba(13, 31, 136, 1) 84%');
I'd like to put the function parameter on multiple lines like this for readability, but it generates a parse error:
.gradientMultiple(~'top,
rgba(255, 255, 255, 1) 0%,
rgba(254, 254, 254, 1) 16%,
rgba(252, 252, 252, 1) 36%,
rgba(239, 239, 239, 1) 66%,
rgba(18, 34, 106, 1) 66%,
rgba(13, 31, 136, 1) 84%
');
Here's the mixin definition:
.gradientMultiple ( ... ) {
background-image: -webkit-linear-gradient(#arguments);
background-image: -moz-linear-gradient(#arguments);
background-image: -ms-linear-gradient(#arguments);
background-image: -o-linear-gradient(#arguments);
background-image: linear-gradient(#arguments);
}
.gradientMultiple(top,
rgba(255, 255, 255, 1) 0%,
rgba(254, 254, 254, 1) 16%,
rgba(252, 252, 252, 1) 36%,
rgba(239, 239, 239, 1) 66%,
rgba( 18, 34, 106, 1) 66%,
rgba( 13, 31, 136, 1) 84%;);
Also see:
0
1
2
and finally the lead remark of 3
For use cases when the mixin forces you to supply a string, or you don't want LESS to compute the argument value, you can use a bit of javascript:
(disclaimer: JS evaluation is now deprecated and possibly will be removed in LESS 3/4, so use this only when working with legacy code where no other alternative is available)
.keyframes(~`
"translate, " +
"50% { transform: translateX(calc(50% - 25px)) } " +
"100% { transform: translateX(0px) } "
`);
I have a subview with (in addition to other standard Cocoa Touch controls) 6 buttons and labels inside of it.
I'm rotating & resizing these buttons and labels on the rotation event with this messy code.
- (void) updateLayoutForNewOrientation: (UIInterfaceOrientation) orientation {
if (UIInterfaceOrientationIsPortrait(orientation)) {
button1.frame = CGRectMake(20, 59, 130, 80);
button2.frame = CGRectMake(170, 59, 130, 80);
button3.frame = CGRectMake(20, 176, 130, 80);
button4.frame = CGRectMake(170, 176, 130, 80);
button5.frame = CGRectMake(20, 293, 130, 80);
button6.frame = CGRectMake(170, 293, 130, 80);
label1.frame = CGRectMake(20, 147, 130, 21);
label2.frame = CGRectMake(170, 147, 130, 21);
label3.frame = CGRectMake(20, 264, 130, 21);
label4.frame = CGRectMake(170, 264, 130, 21);
label5.frame = CGRectMake(20, 381, 130, 21);
label6.frame = CGRectMake(170, 381, 130, 21);
} else {
button1.frame = CGRectMake(20, 59, 130, 60);
button2.frame = CGRectMake(20, 155, 130, 60);
button3.frame = CGRectMake(177, 59, 130, 60);
button4.frame = CGRectMake(177, 155, 130, 60);
button5.frame = CGRectMake(328, 59, 130, 60);
button6.frame = CGRectMake(328, 155, 130, 60);
label1.frame = CGRectMake(20, 127, 130, 21);
label2.frame = CGRectMake(20, 223, 130, 21);
label3.frame = CGRectMake(177, 127, 130, 21);
label4.frame = CGRectMake(177, 223, 130, 21);
label5.frame = CGRectMake(328, 127, 130, 21);
label6.frame = CGRectMake(328, 223, 130, 21);
}
}
That's a bit messy, but works fine and I have a precise control of element position in the view.
By the way I'm wondering if having two different views and flipping them on rotation is more efficient regarding "cpu power" and memory consumption (I think that having a single view instead of two is better for memory, but I could be wrong, I'm pretty new to ios programming).
Thanks for any suggestion!
Right now I am working on a similar code what you wrote. The project it is very big, hard to follow what where to modify. It was written for iOS 3 and now the client needed a few modification. It wouldn't so big the problem, if the iOS wouldn't change the pushModal and would be deprecated in iOS6... In my case needed to rewrite the GUI skeleton, and I had Portait / Landscape requirements. Check a few links if you would like it: question1
question2 question3 question4
For a not previsible iOS change it will be to hard to overview that code, especially after 2 year. I wouldn't repeat that programmer mistake who has worked before.
In your case that is my I would choose from different solutions ( but this for sure not) :
Solution 1: Create different xib for landscape and portait.
Has the advantage of the modularity: later if you don't need Landscape for various reason it is easy to unlink from your code. Has the disadvantage of creating a new view ( well it can be cached) but still different objects and need synchronizations of data, between model-View2-controller.
Solution2: use only 1 xib file and layout automatically the components by setting up they properties at size inspector ( 5th tab ). Very hard to configure
Solution3: inside that xib file create component-landscape and layout those components to mach the expected design, size, and at Runtime read they size and set it, as you so now, but that can be edited visually.
Storyboard or xib, it is almost the same for me, there can be 2 UIViewController and pop / push the Portait/Landscape to not fill the stack with rotations :)
Speed ofc it is the best if you don't rotate at all, but if you still want, maybe the 2 component in 1 xib, because not need to change the visibility 2-4 times, wich will trigger a lot od function calls: viewwillappear, wiewdidapear, viewwilldisapers and so on.
You ask:
By the way I'm wondering if having two different views and flipping them on rotation is more efficient regarding "cpu power" and memory consumption ...
I believe that the opposite is true. It's far more efficient to have a single view for which you simply invoke your method from viewWillLayoutSubviews (in iOS 5, in iOS 4 I generally use willAnimateRotationToInterfaceOrientation). The efficiency gain is modest, but if efficiency is your goal, I think a single view is the way to go. You might want to use the separate views if they're radically different and thus your code would become hard to manage with a single view, but otherwise I stick with one view. Personally, I also think it's a stronger user interface to have the controls animate into place as you rotate the device.
Personally, when I have content that reorganizes on orientation changes like this, I try to refrain from using hard coded coordinates, but rather use the dimensions of the view to algorithmically determine the the layout of my controls (e.g. how many per row) and from there, determine their coordinates. This way, it takes care of not only landscape versus portrait, but also makes Universal apps (especially if there's ever other devices with different screen sizes in the future) easier, too. I think we can also look forward to iOS 6 also offering some nice enhancements to laying out controls (though it will be a while before we will feel comfortable developing apps that require iOS 6).