Related
In TWebModule1.WebModule1WebActionItem1Action(), using Request.ContentFields.Values['content'] or any name, I cannot get any values from the POST, but if I use Request.ContentFields.DelimitedText, I will get the values. Where did I go wrong?
This is the values DelimitedText returned:
------WebKitFormBoundaryHkgxkABfO4DL8g8Q,"Content-Disposition: form-data; name=""name""",NEWTEST,------WebKitFormBoundaryHkgxkABfO4DL8g8Q,"Content-Disposition: form-data; name=""content""","{""resolution""",D,",""symbol_type""",stock,",""exchange""",IDX,",""listed_exchange""",IDX,",""symbol""",BBCA,",""short_name""",BBCA,",""legs""",[{\,"symbol\"":\""","BCA\"",\""","ro_symbol\"":\""","BCA\""}]""",id,":2,""name""",NEWTEST,",""description""",,",""charts_symbols""",{\,"1\"":{\""","ymbol\"":\""","BCA\""}}""",is_realtime,":""1""",content,":""{\""","ame\"":\""","EWTEST\"",\""","ayout\"":\""","\"",\""","harts\"":[{\""","anes\"":[{\""","ources\"":[{\""","ype\"":\""","ainSeries\"",\""","d\"":\""","NUxsv\"",\""","tate\"":{\""","tyle\"":8,\""","sdShowDividends\"":true,\""","sdShowSplits\"":true,\""","sdShowEarnings\"":true,\""","sdShowBreaks\"":false,\""","sdFlagSize\"":2,\""","howCountdown\"":false,\""","idAsk\"":{\""","isible\"":false,\""","ineStyle\"":1,\""","ineWidth\"":1,\""","idLineColor\"":\""","2962FF\"",\""","skLineColor\"":\""","F7525F\""},\""","rePostMarket\"":{\""","isible\"":true,\""","ineStyle\"":1,\""","ineWidth\"":1,\""","reMarketColor\"":\""","fb8c00\"",\""","ostMarketColor\"":\""","2962FF\""},\""","ighLowAvgPrice\"":{\""","ighLowPriceLinesVisible\"":false,\""","ighLowPriceLabelsVisible\"":false,\""","verageClosePriceLineVisible\"":false,\""","verageClosePriceLabelVisible\"":false},\""","howInDataWindow\"":true,\""","isible\"":true,\""","howPriceLine\"":true,\""","riceLineWidth\"":1,\""","riceLineColor\"":\""",",\","baseLineColor\"":\""","B2B5BE\"",\""","howPrevClosePriceLine\"":false,\""","revClosePriceLineWidth\"":1,\""","revClosePriceLineColor\"":\""","gba( 85, 85, 85, 1)\"",\""","inTick\"":\""","efault\"",\""","ividendsAdjustment\"":{},\""","ackAdjustment\"":false,\""","ettlementAsClose\"":true,\""","essionId\"":\""","egular\"",\""","essVis\"":false,\""","tatusViewStyle\"":{\""","ontSize\"":17,\""","howExchange\"":true,\""","howInterval\"":true,\""","ymbolTextSource\"":\""","escription\"",\""","howSymbolAsDescription\"":false},\""","andleStyle\"":{\""","pColor\"":\""","53b987\"",\""","ownColor\"":\""","eb4d5c\"",\""","rawWick\"":true,\""","rawBorder\"":true,\""","orderColor\"":\""","378658\"",\""","orderUpColor\"":\""","53b987\"",\""","orderDownColor\"":\""","eb4d5c\"",\""","ickColor\"":\""","737375\"",\""","ickUpColor\"":\""","a9cdd3\"",\""","ickDownColor\"":\""","f5a6ae\"",\""","arColorsOnPrevClose\"":false,\""","rawBody\"":true},\""","ollowCandleStyle\"":{\""","pColor\"":\""","gba( 83, 185, 135, 1)\"",\""","ownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","rawWick\"":true,\""","rawBorder\"":true,\""","orderColor\"":\""","gba( 55, 134, 88, 1)\"",\""","orderUpColor\"":\""","gba( 83, 185, 135, 1)\"",\""","orderDownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","ickColor\"":\""","gba( 115, 115, 117, 1)\"",\""","ickUpColor\"":\""","gba( 169, 220, 195, 1)\"",\""","ickDownColor\"":\""","gba( 245, 166, 174, 1)\"",\""","rawBody\"":true},\""","aStyle\"":{\""","pColor\"":\""","gba( 83, 185, 135, 1)\"",\""","ownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","rawWick\"":true,\""","rawBorder\"":true,\""","orderColor\"":\""","gba( 55, 134, 88, 1)\"",\""","orderUpColor\"":\""","gba( 83, 185, 135, 1)\"",\""","orderDownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","ickColor\"":\""","gba( 115, 115, 117, 1)\"",\""","ickUpColor\"":\""","gba( 83, 185, 135, 1)\"",\""","ickDownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","howRealLastPrice\"":false,\""","arColorsOnPrevClose\"":false,\""","nputs\"":{},\""","nputInfo\"":{},\""","rawBody\"":true},\""","arStyle\"":{\""","pColor\"":\""","gba( 83, 185, 135, 1)\"",\""","ownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","arColorsOnPrevClose\"":false,\""","ontDrawOpen\"":false,\""","hinBars\"":true},\""","iloStyle\"":{\""","olor\"":\""","2962FF\"",\""","howBorders\"":true,\""","orderColor\"":\""","2962FF\"",\""","howLabels\"":true,\""","abelColor\"":\""","2962FF\"",\""","ontSize\"":7,\""","rawBody\"":true},\""","ineStyle\"":{\""","olor\"":\""","gba( 60, 120, 216, 1)\"",\""","inestyle\"":0,\""","inewidth\"":1,\""","riceSource\"":\""","lose\"",\""","tyleType\"":2},\""","reaStyle\"":{\""","olor1\"":\""","gba( 96, 96, 144, 0.5)\"",\""","olor2\"":\""","gba( 1, 246, 245, 0.5)\"",\""","inecolor\"":\""","gba( 0, 148, 255, 1)\"",\""","inestyle\"":0,\""","inewidth\"":1,\""","riceSource\"":\""","lose\"",\""","ransparency\"":50},\""","enkoStyle\"":{\""","pColor\"":\""","gba( 83, 185, 135, 1)\"",\""","ownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","orderUpColor\"":\""","gba( 83, 185, 135, 1)\"",\""","orderDownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","pColorProjection\"":\""","gba( 169, 220, 195, 1)\"",\""","ownColorProjection\"":\""","gba( 245, 166, 174, 1)\"",\""","orderUpColorProjection\"":\""","gba( 169, 220, 195, 1)\"",\""","orderDownColorProjection\"":\""","gba( 245, 166, 174, 1)\"",\""","ickUpColor\"":\""","gba( 83, 185, 135, 1)\"",\""","ickDownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","nputs\"":{\""","ource\"":\""","lose\"",\""","ources\"":\""","lose\"",\""","oxSize\"":3,\""","tyle\"":\""","TR\"",\""","trLength\"":14,\""","icks\"":true},\""","nputInfo\"":{\""","ource\"":{\""","ame\"":\""","ource\""},\""","ources\"":{\""","ame\"":\""","ource\""},\""","oxSize\"":{\""","ame\"":\""","ox size\""},\""","tyle\"":{\""","ame\"":\""","tyle\""},\""","trLength\"":{\""","ame\"":\""","TR Length\""},\""","icks\"":{\""","ame\"":\""","icks\""}}},\""","bStyle\"":{\""","pColor\"":\""","gba( 83, 185, 135, 1)\"",\""","ownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","orderUpColor\"":\""","gba( 83, 185, 135, 1)\"",\""","orderDownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","pColorProjection\"":\""","gba( 169, 220, 195, 1)\"",\""","ownColorProjection\"":\""","gba( 245, 166, 174, 1)\"",\""","orderUpColorProjection\"":\""","gba( 169, 220, 195, 1)\"",\""","orderDownColorProjection\"":\""","gba( 245, 166, 174, 1)\"",\""","nputs\"":{\""","ource\"":\""","lose\"",\""","b\"":3},\""","nputInfo\"":{\""","ource\"":{\""","ame\"":\""","ource\""},\""","b\"":{\""","ame\"":\""","umber of line\""}}},\""","agiStyle\"":{\""","pColor\"":\""","gba( 83, 185, 135, 1)\"",\""","ownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","pColorProjection\"":\""","gba( 169, 220, 195, 1)\"",\""","ownColorProjection\"":\""","gba( 245, 166, 174, 1)\"",\""","nputs\"":{\""","ource\"":\""","lose\"",\""","tyle\"":\""","TR\"",\""","trLength\"":14,\""","eversalAmount\"":1},\""","nputInfo\"":{\""","ource\"":{\""","ame\"":\""","ource\""},\""","tyle\"":{\""","ame\"":\""","tyle\""},\""","trLength\"":{\""","ame\"":\""","TR Length\""},\""","eversalAmount\"":{\""","ame\"":\""","eversal amount\""}}},\""","nfStyle\"":{\""","pColor\"":\""","gba( 83, 185, 135, 1)\"",\""","ownColor\"":\""","gba( 255, 77, 92, 1)\"",\""","pColorProjection\"":\""","gba( 169, 220, 195, 1)\"",\""","ownColorProjection\"":\""","gba( 245, 166, 174, 1)\"",\""","nputs\"":{\""","ources\"":\""","lose\"",\""","eversalAmount\"":3,\""","oxSize\"":1,\""","tyle\"":\""","TR\"",\""","trLength\"":14,\""","neStepBackBuilding\"":false},\""","nputInfo\"":{\""","ources\"":{\""","ame\"":\""","ource\""},\""","oxSize\"":{\""","ame\"":\""","ox size\""},\""","eversalAmount\"":{\""","ame\"":\""","eversal amount\""},\""","tyle\"":{\""","ame\"":\""","tyle\""},\""","trLength\"":{\""","ame\"":\""","TR Length\""},\""","neStepBackBuilding\"":{\""","ame\"":\""","ne step back building\""}}},\""","aselineStyle\"":{\""","aselineColor\"":\""","gba( 117, 134, 150, 1)\"",\""","opFillColor1\"":\""","gba( 83, 185, 135, 0.1)\"",\""","opFillColor2\"":\""","gba( 83, 185, 135, 0.1)\"",\""","ottomFillColor1\"":\""","gba( 235, 77, 92, 0.1)\"",\""","ottomFillColor2\"":\""","gba( 235, 77, 92, 0.1)\"",\""","opLineColor\"":\""","gba( 83, 185, 135, 1)\"",\""","ottomLineColor\"":\""","gba( 235, 77, 92, 1)\"",\""","opLineWidth\"":1,\""","ottomLineWidth\"":1,\""","riceSource\"":\""","lose\"",\""","ransparency\"":50,\""","aseLevelPercentage\"":50},\""","angeStyle\"":{\""","pColor\"":\""","22AB94\"",\""","ownColor\"":\""","F7525F\"",\""","hinBars\"":true,\""","pColorProjection\"":\""","gba( 169, 220, 195, 1)\"",\""","ownColorProjection\"":\""","gba( 245, 166, 174, 1)\"",\""","nputs\"":{\""","ange\"":10,\""","hantomBars\"":false},\""","nputInfo\"":{\""","ange\"":{\""","ame\"":\""","ange\""},\""","hantomBars\"":{\""","ame\"":\""","hantom bars\""}}},\""","ilentIntervalChange\"":false,\""","ymbol\"":\""","BCA\"",\""","hortName\"":\""","BCA\"",\""","imeframe\"":\""",",\","onWidget\"":false,\""","nterval\"":\""","\"",\""","nitId\"":null,\""","urrencyId\"":\""","DR\"",\""","howSessions\"":false,\""","riceAxisProperties\"":{\""","utoScale\"":true,\""","utoScaleDisabled\"":false,\""","ockScale\"":false,\""","ercentage\"":false,\""","ercentageDisabled\"":false,\""","og\"":false,\""","ogDisabled\"":false,\""","lignLabels\"":true,\""","sInverted\"":false,\""","ndexedTo100\"":false}},\""","order\"":0,\""","aStyle\"":{\""","tudyId\"":\""","arSetHeikenAshi#tv-basicstudies-60\""},\""","enkoStyle\"":{\""","tudyId\"":\""","arSetRenko#tv-prostudies-40\""},\""","bStyle\"":{\""","tudyId\"":\""","arSetPriceBreak#tv-prostudies-34\""},\""","agiStyle\"":{\""","tudyId\"":\""","arSetKagi#tv-prostudies-34\""},\""","nfStyle\"":{\""","tudyId\"":\""","arSetPnF#tv-prostudies-34\""},\""","angeStyle\"":{\""","tudyId\"":\""","arSetRange#tv-basicstudies-72\""}},{\""","ype\"":\""","tudy_Volume\"",\""","d\"":\""","InSIU\"",\""","tate\"":{\""","tyles\"":{\""","ol\"":{\""","inestyle\"":0,\""","inewidth\"":1,\""","lottype\"":5,\""","rackPrice\"":false,\""","ransparency\"":50,\""","isible\"":true,\""","olor\"":\""","000080\"",\""","istogramBase\"":0,\""","oinPoints\"":false,\""","itle\"":\""","olume\""},\""","ol_ma\"":{\""","inestyle\"":0,\""","inewidth\"":1,\""","lottype\"":0,\""","rackPrice\"":false,\""","ransparency\"":0,\""","isible\"":false,\""","olor\"":\""","2196f3\"",\""","istogramBase\"":0,\""","oinPoints\"":false,\""","itle\"":\""","olume MA\""},\""","moothedMA\"":{\""","inestyle\"":0,\""","inewidth\"":1,\""","lottype\"":0,\""","rackPrice\"":false,\""","ransparency\"":0,\""","isible\"":false,\""","olor\"":\""","2196f3\"",\""","istogramBase\"":0,\""","oinPoints\"":false,\""","itle\"":\""","moothed MA\""}},\""","alettes\"":{\""","olumePalette\"":{\""","olors\"":{\""","\"":{\""","olor\"":\""","F7525F\"",\""","idth\"":1,\""","tyle\"":0,\""","ame\"":\""","alling\""},\""","\"":{\""","olor\"":\""","22AB94\"",\""","idth\"":1,\""","tyle\"":0,\""","ame\"":\""","rowing\""}}}},\""","nputs\"":{\""","howMA\"":false,\""","ength\"":20,\""","ol_prev_close\"":false,\""","ymbol\"":\""",",\","smoothingLine\"":\""","MA\"",\""","moothingLength\"":9},\""","recision\"":\""","efault\"",\""","ands\"":{},\""","rea\"":{},\""","raphics\"":{},\""","howInDataWindow\"":true,\""","isible\"":true,\""","howLegendValues\"":true,\""","howLabelsOnPriceScale\"":true,\""","lots\"":{\""","\"":{\""","d\"":\""","ol\"",\""","ype\"":\""","ine\""},\""","\"":{\""","d\"":\""","olumePalette\"",\""","alette\"":\""","olumePalette\"",\""","arget\"":\""","ol\"",\""","ype\"":\""","olorer\""},\""","\"":{\""","isible\"":true,\""","utureOnly\"":true,\""","reaks\"":{\""","olor\"":\""","gba(85, 85, 85, 1)\"",\""","isible\"":false,\""","tyle\"":2,\""","idth\"":1}},\""","radingProperties\"":{\""","howPositions\"":true,\""","ositionPL\"":{\""","isibility\"":true,\""","isplay\"":0},\""","howOrders\"":true,\""","howExecutions\"":true,\""","howReverse\"":true,\""","orizontalAlignment\"":2,\""","xtendLeft\"":true,\""","ineLength\"":5,\""","ineWidth\"":1,\""","ineStyle\"":2},\""","riceScaleSelectionStrategyName\"":\""","uto\""},\""","ineToolsGroups\"":{\""","roups\"":[]},\""","ersion\"":3,\""","imezone\"":\""","tc/UTC\"",\""","essions\"":{\""","roperties\"":{\""","raphics\"":{\""","ackgrounds\"":{\""","utOfSession\"":{\""","olor\"":\""","2962FF\"",\""","ransparency\"":92,\""","isible\"":false},\""","reMarket\"":{\""","olor\"":\""","FF9800\"",\""","ransparency\"":92,\""","isible\"":false},\""","ostMarket\"":{\""","olor\"":\""","2962FF\"",\""","ransparency\"":92,\""","isible\"":false}},\""","ertlines\"":{\""","essBreaks\"":{\""","olor\"":\""","4985e7\"",\""","tyle\"":2,\""","isible\"":false,\""","idth\"":1}}}}},\""","houldBeSavedEvenIfHidden\"":true,\""","hartId\"":\""","\""}]}""",------WebKitFormBoundaryHkgxkABfO4DL8g8Q,"Content-Disposition: form-data; name=""symbol""",BBCA,------WebKitFormBoundaryHkgxkABfO4DL8g8Q,"Content-Disposition: form-data; name=""resolution""",D,------WebKitFormBoundaryHkgxkABfO4DL8g8Q--
I have been googling the whole web, mostly outdated answer from years ago and I don't find any proper easy way to parse it, but I found my own manual way, please anyone, have a better way to parse please share. thanks.
LBoundary := ExtractHeaderSubItem(Request.ContentType, 'boundary', QuoteHTTP);
tmpstring := Request.Content;
tmpstring := trim(tmpstring);
i := Pos('Content-Disposition: form-data; name="name"', tmpstring);
j := Pos('Content-Disposition: form-data; name="content"', tmpstring);
k := j - i;
tmpstring := (Copy(tmpstring, i, k));
tmpstring := StringReplace(tmpstring, 'Content-Disposition: form-data; name="name"', '', [rfReplaceAll]);
tmpstring := StringReplace(tmpstring, 'Content-Disposition: form-data; name="content"', '', [rfReplaceAll]);
tmpstring := StringReplace(tmpstring, '--'+LBoundary, '', [rfReplaceAll]);
tmpstring := StringReplace(tmpstring, #13#10, '', [rfReplaceAll]);
tmpstring := StringReplace(tmpstring, #32, '', [rfReplaceAll]);
tmpstring := StringReplace(tmpstring, #9, '', [rfReplaceAll]);
name:= tmpstring;
I am getting List of 8-bit unsigned int from a mic source for each sample rate which looks like this
[61, 251, 199, 251, 56, 252, 138, 252, 211, 252, 18, 253, 91, 253, 194, 253, 25, 254, 54, 254, 19, 254, 190, 253, 80, 253, 249, 252, 233, 252, 46, 253, 180, 253, 54, 254, 136, 254, 157, 254, 110, 254, 38, 254, 208, 253, 117, 253, 68, 253, 57, 253, 83, 253, 163, 253, 20, 254, 151, 254, 51, 255, 215, 255, 105, 0, 207, 0, 246, 0, 249, 0, 10, 1, 64, 1, 162, 1, 4, 2, 64, 2, 97, 2, 111, 2, 110, 2, 89, 2, 40, 2, 241, 1, 199, 1, 178, 1, 192, 1, 241, 1, 45, 2, 77, 2, 70, 2, 45, 2, 36, 2, 83, 2, 176, 2, 21, 3, 121, 3, 229, 3, 87, 4, 185, 4, 225, 4, 197, 4, 129, 4, 26, 4, 150, 3, 7, 3, 128, 2, 55, 2, 65, 2, 134, 2, 223, 2, 25, 3, 41, 3, 28, 3, 255, 2, 234, 2, 240, 2, 25, 3, 62, 3, 92, 3, 146, 3, 219, 3, 65, 4, 149, 4, 164, 4, 130, 4, 51, 4, 195, 3, 69, 3, 164, 2, 244, 1, 75, 1, 187, 0, 81, 0, 240, 255, 135, 255, 19, 255, 155, 254, 64, 254, 22, 254, 58, 254, 146, 254, 217, 254, 248, 254, 215, 254, 144, 254, 92, 254, 84, 254, 141, 254, 229, 254, 39, 255, 96, 255, 170, 255, 248, 255, 69, 0, 117, 0, 128, 0, 137, 0, 131, 0,
so how can I convert this into signed decimal value or someone can guide me to the right path
That depends on what the bytes mean.
Looking at the bytes, every other byte is either very low or very high. That suggests to me that the bytes are really little-endian signed 16-bit values.
In that case, you just need to view them as such. If we assume that the platform is little-endian (most are), you can just do:
List<int> list = ...;
Uint8List bytes = Uint8List.fromList(list); //
Int16List words = Int16List.sublistView(bytes);
Then the words list contains signed 16-bit numbers.
(If the list is already a Uint8List, you can skip the first step.)
If that's not what the bytes mean, you'll have to figure out what they do mean.
Dart int type provide a method to convert from signed to unsigned and from unsigned to signed.
For example:
int a = 16;
int b = 239;
print(a.toSigned(5).toString()); // Print -16
print(b.toSigned(5).toString()); // Print 15
the toSigned method parameter indicate the bit order of the sign bit.
You can get more information here: https://api.flutter.dev/flutter/dart-core/int/toSigned.html
A toUnsigned method exixts too: https://api.flutter.dev/flutter/dart-core/int/toUnsigned.html
Building a pem file in Elixir requires several steps, including building an entity. In OTP 17, the following works:
{public, private} = :crypto.generate_key(:ecdh, :secp256k1)
ec_entity = {:ECPrivateKey,
1,
:binary.bin_to_list(private),
{:namedCurve, {1, 3, 132, 0, 10}},
{0, public}}
der_encoded = :public_key.der_encode(:ECPrivateKey, ec_entity)
pem = public_key.pem_encode([{:ECPrivateKey, der_encoded, :not_encrypted}])
But using OTP 18, the following error occurs:
{public, private} = :crypto.generate_key(:ecdh, :secp256k1)
ec_entity = {:ECPrivateKey,
1,
:binary.bin_to_list(private),
{:namedCurve, {1, 3, 132, 0, 10}},
{0, public}}
der_encoded = :public_key.der_encode(:ECPrivateKey, ec_entity)
** (MatchError) no match of right hand side value: {:error, {:asn1, :badarg}}
public_key.erl:253: :public_key.der_encode/2
What is the source of this error?
The source of the error is a change in the way that the public_key entity is constructed between OTP 17 and OTP 18. If we reverse the process, starting with a pem file, we can see the difference.
OTP 17:
iex(6)> pem = "-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEIJniJF4vtTqE4wS5AkhmMZsHIbil0l3XfRButkw5IJYFoAcGBSuBBAAK\noUQDQgAEtxm+jijBB0JxZTceHnCHE0HpMXJp1ScVUZ5McvDUVsS/Dek8IdAsMOPz\nnnVALflZzXtH/wU9p2LrFdJeuXwL8g==\n-----END EC PRIVATE KEY-----\n\n"
"-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEIJniJF4vtTqE4wS5AkhmMZsHIbil0l3XfRButkw5IJYFoAcGBSuBBAAK\noUQDQgAEtxm+jijBB0JxZTceHnCHE0HpMXJp1ScVUZ5McvDUVsS/Dek8IdAsMOPz\nnnVALflZzXtH/wU9p2LrFdJeuXwL8g==\n-----END EC PRIVATE KEY-----\n\n"
iex(7)> [{type, decoded, _}] = :public_key.pem_decode(pem)
[{:ECPrivateKey,
<<48, 116, 2, 1, 1, 4, 32, 153, 226, 36, 94, 47, 181, 58, 132, 227, 4, 185, 2, 72, 102, 49, 155, 7, 33, 184, 165, 210, 93, 215, 125, 16, 110, 182, 76, 57, 32, 150, 5, 160, 7, 6, 5, 43, 129, 4, 0, 10, ...>>,
:not_encrypted}]
iex(8)> :public_key.der_decode(type, decoded)
{:ECPrivateKey, 1,
[153, 226, 36, 94, 47, 181, 58, 132, 227, 4, 185, 2, 72, 102, 49, 155, 7, 33,
184, 165, 210, 93, 215, 125, 16, 110, 182, 76, 57, 32, 150, 5],
{:namedCurve, {1, 3, 132, 0, 10}},
{0,
<<4, 183, 25, 190, 142, 40, 193, 7, 66, 113, 101, 55, 30, 30, 112, 135, 19, 65, 233, 49, 114, 105, 213, 39, 21, 81, 158, 76, 114, 240, 212, 86, 196, 191, 13, 233, 60, 33, 208, 44, 48, 227, 243, 158, 117, ...>>}}
OTP 18:
iex(5)> [{type, decoded, _}] = :public_key.pem_decode(pem)
[{:ECPrivateKey,
<<48, 116, 2, 1, 1, 4, 32, 153, 226, 36, 94, 47, 181, 58, 132, 227, 4, 185, 2, 72, 102, 49, 155, 7, 33, 184, 165, 210, 93, 215, 125, 16, 110, 182, 76, 57, 32, 150, 5, 160, 7, 6, 5, 43, 129, 4, 0, 10, ...>>,
:not_encrypted}]
iex(6)> entity = :public_key.der_decode(type, decoded)
{:ECPrivateKey, 1,
<<153, 226, 36, 94, 47, 181, 58, 132, 227, 4, 185, 2, 72, 102, 49, 155, 7, 33, 184, 165, 210, 93, 215, 125, 16, 110, 182, 76, 57, 32, 150, 5>>,
{:namedCurve, {1, 3, 132, 0, 10}},
<<4, 183, 25, 190, 142, 40, 193, 7, 66, 113, 101, 55, 30, 30, 112, 135, 19, 65, 233, 49, 114, 105, 213, 39, 21, 81, 158, 76, 114, 240, 212, 86, 196, 191, 13, 233, 60, 33, 208, 44, 48, 227, 243, 158, 117, 64, ...>>}
The difference is in how the public and private keys are represented.
The signature of an ECPrivateKey Record is:
ECPrivateKey'{ version, privateKey, parameters, publicKey}
In Erlang 18, both values are represented at plain binaries, in 17, the private key is a list and the public key is part of a tuple, {0, binary}.
So in order to build the pem file correctly, the entity representation has to change.
{public, private} = :crypto.generate_key(:ecdh, :secp256k1)
entity = {:ECPrivateKey,
1,
private,
{:namedCurve, {1, 3, 132, 0, 10}},
public}
Using the new representation of the record will solve the problem.
I didn't really check why your version works on some versions, but I've got some code that works on all these erlang versions: 19.0, 18.2.1, 18.1, 18.0, 17.5, R16B03 (running on travis).
-include_lib("public_key/include/public_key.hrl").
genPEMKey() ->
CurveId = secp256k1,
{PubKey, PrivKey} = crypto:generate_key(ecdh, CurveId),
Key = #'ECPrivateKey'{version = 1,
privateKey = PrivKey,
parameters = {
namedCurve,
pubkey_cert_records:namedCurves(CurveId)},
publicKey = PubKey},
DERKey = public_key:der_encode('ECPrivateKey', Key),
public_key:pem_encode([{'ECPrivateKey', DERKey, not_encrypted}]).
This piece of code was based on the examples found in the OTP codebase:
https://github.com/erlang/otp/blob/master/lib/public_key/test/erl_make_certs.erl#L407
I have a problem where I receive a lot of histograms from some database images. Those histograms are represented as vectors (0...255), and I have to identify and work with the bimodal histograms.
Is there a formula to automatically identify which histograms are bimodal and which aren't? Since they are numeric vectors, I could use a programming language (Java/C#) to work with it.
Is there a criterion on literature to identify bimodal histograms by software?
Here are 3 examples of histograms and format inputs I'm working with. Each histogram is a vector with 256 (0...255) positions.
Histogram 1
8029, 41, 82, 177, 135, 255, 315, 591, 949, 456, 499, 688, 446, 733, 712, 1595, 2633, 3945, 6134, 9755, 9236, 11911, 11888, 9450, 13119, 8819, 5991, 4399, 6745, 2017, 3747, 1777, 2946, 1623, 2151, 454, 3015, 3176, 2211, 1080, 391, 580, 750, 473, 10424, 334, 559, 621, 340, 2794, 1094, 5274, 2822, 204, 389, 728, 268, 15, 1060, 58, 113, 2728, 52, 3166, 11, 103, 522, 107, 351, 97, 66, 565, 315, 444, 3305, 245, 647, 306, 147, 112, 103, 672, 69, 317, 61, 224, 71, 52, 479, 62, 106, 166, 215, 132, 137, 321, 998, 427, 846, 787, 542, 1054, 1429, 615, 697, 580, 642, 768, 1244, 462, 4107, 1701, 2394, 4954, 4869, 1841, 1807, 1032, 3075, 331, 488, 627, 1281, 233, 1010, 1178, 727, 830, 1619, 728, 1428, 1849, 4826, 351, 745, 320, 888, 335, 741, 1151, 734, 689, 2143, 1130, 2482, 3609, 4779, 5678, 4186, 2654, 1668, 1290, 702, 1093, 476, 438, 445, 271, 98, 368, 226, 90, 75, 26, 33, 62, 16, 824, 21, 37, 34, 24, 54, 42, 101, 112, 18, 24, 17, 15, 3, 50, 7, 6, 54, 3, 58, 9, 10, 66, 12, 11, 10, 6, 25, 11, 7, 172, 13, 18, 21, 9, 8, 9, 42, 16, 15, 6, 12, 17, 7, 591, 6, 7, 14, 24, 7, 7, 19, 87, 18, 8, 9, 9, 35, 55, 4, 17, 10, 18, 22, 46, 8, 852, 15, 14, 12, 11, 9, 3, 50, 163, 12, 4, 18, 129, 6, 35, 47, 14, 18, 150, 21, 46, 24, 0
Histogram 2
8082, 4857, 1494, 2530, 1604, 1636, 1651, 1681, 1630, 1667, 1636, 1649, 1934, 1775, 1701, 1691, 1478, 1649, 1449, 1449, 1503, 1475, 1497, 1398, 1509, 1747, 1301, 1539, 1575, 1496, 1754, 1432, 1759, 1786, 1679, 1816, 2435, 1174, 1780, 1344, 1749, 2026, 1779, 1742, 1722, 1835, 2306, 1662, 1965, 1885, 2212, 2139, 1930, 2306, 2707, 2289, 2307, 2082, 2360, 2216, 2480, 2243, 2222, 1824, 4555, 1918, 2116, 2275, 2615, 2240, 2703, 2481, 2626, 2708, 3008, 2696, 2561, 2906, 3625, 2419, 3137, 2793, 2747, 2861, 2774, 4124, 3155, 3243, 3523, 3432, 3277, 3456, 2984, 2902, 2819, 2778, 3158, 2997, 2591, 2717, 2553, 2464, 3657, 2296, 2352, 2046, 2124, 1965, 2014, 2096, 1664, 1373, 1607, 1322, 1272, 1113, 1156, 1055, 924, 881, 1019, 669, 929, 636, 590, 463, 524, 177, 1267, 378, 409, 413, 415, 435, 385, 379, 267, 413, 266, 282, 499, 194, 360, 199, 337, 92, 986, 183, 160, 230, 124, 213, 188, 334, 164, 159, 130, 143, 135, 331, 25, 118, 114, 98, 74, 301, 92, 119, 94, 72, 192, 38, 64, 100, 138, 30, 98, 65, 226, 23, 46, 78, 78, 61, 55, 234, 26, 36, 95, 31, 49, 214, 25, 34, 58, 37, 101, 20, 41, 34, 150, 16, 50, 25, 53, 18, 30, 67, 27, 36, 42, 23, 60, 12, 21, 36, 12, 45, 21, 58, 53, 18, 51, 16, 25, 9, 24, 15, 18, 30, 33, 20, 19, 12, 23, 16, 14, 21, 14, 10, 20, 13, 12, 9, 6, 9, 7, 10, 7, 2, 0, 0, 0, 0, 0, 2087
Histogram 3
50, 226, 857, 2018, 1810, 1795, 1840, 1929, 1942, 1693, 1699, 1547, 1564, 1556, 1451, 1439, 1448, 1357, 1428, 1419, 1383, 1705, 1670, 1777, 1826, 1865, 1897, 1924, 2003, 1973, 1813, 1801, 1827, 1696, 1717, 1654, 1678, 1705, 1621, 1523, 1494, 1559, 1434, 1370, 1358, 1385, 1348, 1380, 1368, 1367, 1389, 1445, 1514, 1471, 1465, 1461, 1475, 1484, 1390, 1403, 1324, 1339, 1426, 1432, 1487, 1460, 1469, 1460, 1546, 1504, 1425, 1373, 1391, 1391, 1382, 1311, 1368, 1354, 1325, 1323, 1263, 1325, 1363, 1357, 1325, 1322, 1429, 1419, 1412, 1371, 1266, 1179, 1166, 1076, 1100, 1083, 1103, 1053, 1116, 1080, 1071, 1025, 1088, 1060, 1011, 984, 958, 959, 954, 937, 982, 950, 1001, 963, 965, 875, 1010, 954, 990, 894, 959, 972, 963, 1101, 971, 1042, 1064, 1075, 1029, 1088, 1090, 1068, 1073, 1058, 1102, 1105, 1009, 1062, 1005, 1048, 973, 998, 1034, 1013, 961, 1006, 983, 948, 1031, 972, 952, 1013, 954, 964, 970, 881, 887, 967, 941, 928, 994, 1019, 1106, 1056, 1113, 1071, 1158, 1108, 1178, 1071, 1080, 1074, 1050, 1076, 1106, 1048, 973, 1042, 997, 1034, 934, 863, 935, 845, 839, 803, 764, 782, 787, 771, 766, 751, 745, 804, 789, 765, 681, 658, 690, 672, 650, 635, 695, 619, 572, 499, 535, 565, 564, 520, 516, 568, 530, 479, 507, 424, 446, 455, 380, 395, 371, 360, 391, 373, 351, 388, 426, 349, 417, 421, 400, 443, 470, 485, 456, 495, 452, 484, 457, 518, 519, 631, 652, 693, 762, 771, 807, 906, 991, 1138, 1433, 1545, 2467, 4907, 6743, 1921
smooth histogram
this filter out small local min max and noise. Use symmetric smoothing to avoid shifting to one side. I smooth from left then from the right which lower the shifting a lot.
find/count the local max peaks
Count only big enough peaks (by some treshold). If peak count is not 2 then it is not a bimodal histogram unless you have different definition of bimodal like:
noise count too
no matter how many peaks but single gap must be present
It depends on what for the histograms are used
Here is some code in C++ I busted for this:
void histograms(Graphics::TBitmap *bmp,int xs,int ys,int **pyx)
{
// clear buffer
bmp->Canvas->Brush->Color=clBlack;
bmp->Canvas->FillRect(TRect(0,0,xs,ys));
bmp->Canvas->Font->Color=clAqua;
bmp->Canvas->TextOutA( 5,5,"Raw histogram");
bmp->Canvas->TextOutA(285,5,"Smoothed histogram");
int his1[256]={ 8029, 41, 82, 177, 135, 255, 315, 591, 949, 456, 499, 688, 446, 733, 712, 1595, 2633, 3945, 6134, 9755, 9236, 11911, 11888, 9450, 13119, 8819, 5991, 4399, 6745, 2017, 3747, 1777, 2946, 1623, 2151, 454, 3015, 3176, 2211, 1080, 391, 580, 750, 473, 10424, 334, 559, 621, 340, 2794, 1094, 5274, 2822, 204, 389, 728, 268, 15, 1060, 58, 113, 2728, 52, 3166, 11, 103, 522, 107, 351, 97, 66, 565, 315, 444, 3305, 245, 647, 306, 147, 112, 103, 672, 69, 317, 61, 224, 71, 52, 479, 62, 106, 166, 215, 132, 137, 321, 998, 427, 846, 787, 542, 1054, 1429, 615, 697, 580, 642, 768, 1244, 462, 4107, 1701, 2394, 4954, 4869, 1841, 1807, 1032, 3075, 331, 488, 627, 1281, 233, 1010, 1178, 727, 830, 1619, 728, 1428, 1849, 4826, 351, 745, 320, 888, 335, 741, 1151, 734, 689, 2143, 1130, 2482, 3609, 4779, 5678, 4186, 2654, 1668, 1290, 702, 1093, 476, 438, 445, 271, 98, 368, 226, 90, 75, 26, 33, 62, 16, 824, 21, 37, 34, 24, 54, 42, 101, 112, 18, 24, 17, 15, 3, 50, 7, 6, 54, 3, 58, 9, 10, 66, 12, 11, 10, 6, 25, 11, 7, 172, 13, 18, 21, 9, 8, 9, 42, 16, 15, 6, 12, 17, 7, 591, 6, 7, 14, 24, 7, 7, 19, 87, 18, 8, 9, 9, 35, 55, 4, 17, 10, 18, 22, 46, 8, 852, 15, 14, 12, 11, 9, 3, 50, 163, 12, 4, 18, 129, 6, 35, 47, 14, 18, 150, 21, 46, 24, 0 };
int his2[256]={ 8082, 4857, 1494, 2530, 1604, 1636, 1651, 1681, 1630, 1667, 1636, 1649, 1934, 1775, 1701, 1691, 1478, 1649, 1449, 1449, 1503, 1475, 1497, 1398, 1509, 1747, 1301, 1539, 1575, 1496, 1754, 1432, 1759, 1786, 1679, 1816, 2435, 1174, 1780, 1344, 1749, 2026, 1779, 1742, 1722, 1835, 2306, 1662, 1965, 1885, 2212, 2139, 1930, 2306, 2707, 2289, 2307, 2082, 2360, 2216, 2480, 2243, 2222, 1824, 4555, 1918, 2116, 2275, 2615, 2240, 2703, 2481, 2626, 2708, 3008, 2696, 2561, 2906, 3625, 2419, 3137, 2793, 2747, 2861, 2774, 4124, 3155, 3243, 3523, 3432, 3277, 3456, 2984, 2902, 2819, 2778, 3158, 2997, 2591, 2717, 2553, 2464, 3657, 2296, 2352, 2046, 2124, 1965, 2014, 2096, 1664, 1373, 1607, 1322, 1272, 1113, 1156, 1055, 924, 881, 1019, 669, 929, 636, 590, 463, 524, 177, 1267, 378, 409, 413, 415, 435, 385, 379, 267, 413, 266, 282, 499, 194, 360, 199, 337, 92, 986, 183, 160, 230, 124, 213, 188, 334, 164, 159, 130, 143, 135, 331, 25, 118, 114, 98, 74, 301, 92, 119, 94, 72, 192, 38, 64, 100, 138, 30, 98, 65, 226, 23, 46, 78, 78, 61, 55, 234, 26, 36, 95, 31, 49, 214, 25, 34, 58, 37, 101, 20, 41, 34, 150, 16, 50, 25, 53, 18, 30, 67, 27, 36, 42, 23, 60, 12, 21, 36, 12, 45, 21, 58, 53, 18, 51, 16, 25, 9, 24, 15, 18, 30, 33, 20, 19, 12, 23, 16, 14, 21, 14, 10, 20, 13, 12, 9, 6, 9, 7, 10, 7, 2, 0, 0, 0, 0, 0, 2087 };
int his3[256]={ 50, 226, 857, 2018, 1810, 1795, 1840, 1929, 1942, 1693, 1699, 1547, 1564, 1556, 1451, 1439, 1448, 1357, 1428, 1419, 1383, 1705, 1670, 1777, 1826, 1865, 1897, 1924, 2003, 1973, 1813, 1801, 1827, 1696, 1717, 1654, 1678, 1705, 1621, 1523, 1494, 1559, 1434, 1370, 1358, 1385, 1348, 1380, 1368, 1367, 1389, 1445, 1514, 1471, 1465, 1461, 1475, 1484, 1390, 1403, 1324, 1339, 1426, 1432, 1487, 1460, 1469, 1460, 1546, 1504, 1425, 1373, 1391, 1391, 1382, 1311, 1368, 1354, 1325, 1323, 1263, 1325, 1363, 1357, 1325, 1322, 1429, 1419, 1412, 1371, 1266, 1179, 1166, 1076, 1100, 1083, 1103, 1053, 1116, 1080, 1071, 1025, 1088, 1060, 1011, 984, 958, 959, 954, 937, 982, 950, 1001, 963, 965, 875, 1010, 954, 990, 894, 959, 972, 963, 1101, 971, 1042, 1064, 1075, 1029, 1088, 1090, 1068, 1073, 1058, 1102, 1105, 1009, 1062, 1005, 1048, 973, 998, 1034, 1013, 961, 1006, 983, 948, 1031, 972, 952, 1013, 954, 964, 970, 881, 887, 967, 941, 928, 994, 1019, 1106, 1056, 1113, 1071, 1158, 1108, 1178, 1071, 1080, 1074, 1050, 1076, 1106, 1048, 973, 1042, 997, 1034, 934, 863, 935, 845, 839, 803, 764, 782, 787, 771, 766, 751, 745, 804, 789, 765, 681, 658, 690, 672, 650, 635, 695, 619, 572, 499, 535, 565, 564, 520, 516, 568, 530, 479, 507, 424, 446, 455, 380, 395, 371, 360, 391, 373, 351, 388, 426, 349, 417, 421, 400, 443, 470, 485, 456, 495, 452, 484, 457, 518, 519, 631, 652, 693, 762, 771, 807, 906, 991, 1138, 1433, 1545, 2467, 4907, 6743, 1921 };
int *his,tmp[256],a,x0,y0,x,y,h,tr=12,sm=10,peak[256],peaks;
// loop through histograms
for (y0=20,h=0;;h++)
{
x0=5;if (h==0) his=his1;
else if (h==1) his=his2;
else if (h==2) his=his2;
else break;
// rescale his <0,?> to tmp <0-100>
for (y=his[0],x=0;x<256;x++) if (y<his[x]) y=his[x]; // y=max
for ( x=0;x<256;x++) tmp[x]=(his[x]*100)/y;
// draw tmp
for (x=0;x<256;x++) for (pyx[y0+100][x0+x]=0x00004040,y=0;y<tmp[x];y++) pyx[y0+100-y][x0+x]=(40+x)*0x00010101;
x0+=280;
// smooth tmp few times
for (y=0;y<sm;y++)
{
// from both directions to avoid shifting to one side
for (x=0;x<255;x++) tmp[x]=((90*tmp[x])+(10*tmp[x+1]))/100;
for (x=255;x>0;x--) tmp[x]=((90*tmp[x])+(10*tmp[x-1]))/100;
}
// find (count) peaks
for (peaks=0,a=0,y=0,x=0;x<255;x++)
{
if (tmp[x]<tmp[x+1]){ if ((y< 0)&&(a-tmp[x]>tr)){ a=tmp[x]; } y=+1; }
else if (tmp[x]>tmp[x+1]){ if ((y>=0)&&(tmp[x]-a>tr)){ a=tmp[x]; peak[peaks]=x; peaks++; } y=-1; }
}
// draw tmp
for (x=0;x<256;x++) for (pyx[y0+100][x0+x]=0x00004040,y=0;y<tmp[x];y++) pyx[y0+100-y][x0+x]=(40+x)*0x00010101;
// draw peaks
bmp->Canvas->Pen->Color=clAqua;
bmp->Canvas->Brush->Color=clAqua;
for (a=0;a<peaks;a++)
{
x=x0+peak[a];
y=y0+100-tmp[peak[a]];
bmp->Canvas->Ellipse(x-5,y-5,x+5,y+5);
}
// draw cross for not bimodal histograms
if (peaks!=2)
{
bmp->Canvas->Pen->Color=clRed;
bmp->Canvas->MoveTo(x0 ,y0 );
bmp->Canvas->LineTo(x0+256,y0+100);
bmp->Canvas->MoveTo(x0+256,y0 );
bmp->Canvas->LineTo(x0 ,y0+100);
}
y0+=128;
}
}
you can ignore the pyx[y][x] and bmp-> stuff it is just rendering
pyx[y][x] is direct 32bit pixel access of bitmap bmp
and bmp->Canvas is VCL encapsulated Windows GDI interface of bitmap bmp
xs,ys is bitmap resolution
set treshold tr and smooth sm to suite your needs best
If you have too much different types of histogram then you need to apply dynamic tresholding or different approach for peak finding this is how it looks like for your histograms:
Where Histogram 1 is the top one. Hope the code is clear enough if not comment me... if you rescale to power of 2 instead of 100 then you can change the multiplications and divisions to bit shifts to speed this a bit. I choose 100 for more clear selection of tresholds and smoothing coefficients...
I don't think there is a simple and straightforward solution for that.
If you think each peak in your histogram as a cluster, you can try to implement some sort of clustering algorithm that is able to automatically detect the number of peaks in your histogram.
You can start looking at here:
Determining the number of clusters in a data set
Kmeans without knowing the number of clusters?
AUTOMATIC DETERMINATION OF THE NUMBER OF CLUSTERS USING SPECTRAL
ALGORITHMS
I have a long list of group_ids like so:
#group_ids = #groups.map(&:group_id)
Rails.logger.info #group_ids
[182, 122, 181, 173, 167, 58, 13, 11, 180, 40, 71, 1, 29, 47, 142, 52, 174, 7, 168, 171, 156, 120, 79, 72, 54, 26, 65]
How can I take all those group_ids and output:
group_id:11 OR group_id:22 etc
Just do:
#group_ids * " OR "
(Ruby is great)
#group_ids = [182, 122, 181, 173, 167, 58, 13, 11, 180, 40, 71, 1, 29, 47, 142, 52, 174, 7, 168, 171, 156, 120, 79, 72, 54, 26, 65]
#group_ids.map{|id| "group_id:#{id}"}.join(" OR ")
#=> "group_id:182 OR group_id:122 OR group_id:181 OR group_id:173 OR group_id:167 OR group_id:58 OR group_id:13 OR group_id:11 OR group_id:180 OR group_id:40 OR group_id:71 OR group_id:1 OR group_id:29 OR group_id:47 OR group_id:142 OR group_id:52 OR group_id:174 OR group_id:7 OR group_id:168 OR group_id:171 OR group_id:156 OR group_id:120 OR group_id:79 OR group_id:72 OR group_id:54 OR group_id:26 OR group_id:65"