Related
i have a function like this in java
public static final byte[] f1504a = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70};
public static final byte[] bArr = {96, 50, (byte) 152, 96, (byte) 147, 80, (byte) 147, 18, 36, 17, 1, 32, (byte) 145, 17, 4, 84, 0, (byte) 145, 3, 1, 6, 0, 0, 0, 0, 0, 3, 116, 87, (byte) 175, 116, 7, (byte) 174, (byte) 206, (byte) 244, 15, (byte) 227, (byte) 162, 68, 9, (byte) 244, (byte) 236, 73, 59, 3, 84, 120, (byte) 132, (byte) 182, (byte) 174, (byte) 176, 80, (byte) 195, 0, 17, 1, 32, 3, (byte) 246, 61, 13, (byte) 255, (byte) 255, (byte) 144, 0};
public static String b(byte[] bArr, int i) {
byte[] bArr2 = new byte[(i * 2)];
int i2 = 0;
int i3 = 0;
for (byte b2 : bArr) {
if (i2 >= i) {
break;
}
i2++;
int i4 = b2 & 255;
int i5 = i3 + 1;
byte[] bArr3 = f1504a;
bArr2[i3] = bArr3[i4 >>> 4];
i3 = i5 + 1;
bArr2[i5] = bArr3[i4 & 15];
}
return new String(bArr2);
}
b(bArr, 8);
RESULT IS : 6032986093509312
how to convert to dart or flutter.
thanks for answering i'm very appreciate
The answer from julemand101 is a perfect translation of the Java code.
A more idiomatic Dart version could be something like:
import 'dart:typed_data';
final byteTable = Uint8List.fromList([
96, 50, 152, 96, 147, 80, 147, 18, 36, 17, 1, 32, 145, 17, 4, 84, 0, 145, //
3, 1, 6, 0, 0, 0, 0, 0, 3, 116, 87, 175, 116, 7, 174, 206, 244, 15, 227, //
162, 68, 9, 244, 236, 73, 59, 3, 84, 120, 132, 182, 174, 176, 80, 195, 0, //
17, 1, 32, 3, 246, 61, 13, 255, 255, 144, 0
]);
// The hex representation of the first [length] bytes of [bytes].
String toHex(Uint8List bytes, int length) {
const hexDigits = "0123456789ABCDEF";
final resultBytes = Uint8List(length * 2);
if (length > bytes.length) length = bytes.length;
for (var i = 0, j = 0; i < length; i++) {
var byte = bytes[i];
resultBytes[j++] = hexDigits.codeUnitAt(byte >> 4);
resultBytes[j++] = hexDigits.codeUnitAt(byte & 15);
}
return String.fromCharCodes(resultBytes);
}
void main() {
print(toHex(byteTable, 8)); // 6032986093509312
}
Please read the comments you got on your question. You should not expect Stackoverflow to just be a service where you can get other people to do your work.
When that said, I got some spare time and ended up writing a solution in Dart for your code as some kind of puzzle. Not entirely sure if it works the same as your Java code but it works for that one example you have provided:
import 'dart:typed_data';
Uint8List f1504a = Uint8List.fromList(
[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70]);
Uint8List bArr = Uint8List.fromList([
96, 50, 152, 96, 147, 80, 147, 18, 36, 17, 1, 32, 145, 17, 4, 84, 0, 145, //
3, 1, 6, 0, 0, 0, 0, 0, 3, 116, 87, 175, 116, 7, 174, 206, 244, 15, 227, //
162, 68, 9, 244, 236, 73, 59, 3, 84, 120, 132, 182, 174, 176, 80, 195, 0, //
17, 1, 32, 3, 246, 61, 13, 255, 255, 144, 0
]);
String b(Uint8List bArr, int i) {
final bArr2 = Uint8List(i * 2);
var i2 = 0, i3 = 0;
for (final b2 in bArr) {
if (i2 >= i) {
break;
}
i2++;
final i4 = b2 & 255;
final i5 = i3 + 1;
final bArr3 = f1504a;
bArr2[i3] = bArr3[i4 >> 4];
i3 = i5 + 1;
bArr2[i5] = bArr3[i4 & 15];
}
return String.fromCharCodes(bArr2);
}
void main() {
print(b(bArr, 8)); // 6032986093509312
}
connecting TCP Socket server and sending Request. and also Server sends the response in Byte array. How to read byte array data in dart.
Socket.connect('localhost', 8081)
.then((socket) {
//Establish the onData, and onDone callbacks
socket.listen((data) {
print(new String.fromCharCodes(data).trim()); //Here data is byte[]
//How to read byte array data
},
onDone: () {
print("Done");
// socket.destroy();
},
onError: (e) {
print('Server error: $e');
});
socket.add([255, 12, 0, 11, 0, 9, 34, 82, 69, 70, 84, 65, 72, 73, 76]);
});
}
It depends on with data type was encoded to bytes. Let's suppose it's String
Then you can do it with dart:convert library.
import 'dart:convert' show utf8;
final decoded = utf8.decode(data);
It's pretty clear that there's a message structure in those bytes. You give two examples of messages:
[255, 12, 0, 11, 0, 9, 34, 82, 69, 70, 84, 65, 72, 73, 76]
and
[255, 20, 0, 11, 0, 0, 0, 15, 80, 82, 69, 77, 84, 65, 72, 73, 76, 45, 53, 53, 57, 55, 48]
Both start with 255, followed by what looks like two or three little endian 16 bit words (12 and 11) and (20, 11 and 0) followed by a string, who's length is encoded in a leading byte. If you are expected to inter-operate with another system, you really need the protocol spec.
Assuming I've guessed the structure correctly, this code
main() {
Uint8List input = Uint8List.fromList([
255,
20,
0,
11,
0,
0,
0,
15,
80,
82,
69,
77,
84,
65,
72,
73,
76,
45,
53,
53,
57,
55,
48
]);
ByteData bd = input.buffer.asByteData();
print(bd.getUint16(1, Endian.little)); // print the first short
print(bd.getUint16(3, Endian.little)); // and the second
print(bd.getUint16(5, Endian.little)); // and the third
int stringLength = input[7]; // get the length of the string
print(utf8.decode(input.sublist(8, 8 + stringLength))); // decode the string
}
produces
20
11
0
PREMTAHIL-55970
as expected
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'm trying to solve this issue in the most accurate way.
I have a UISlider, and a range between 0 to 300 (All primes).
How can I, using the UISlider, get an accurate access to each prime number in this range, while moving the slider? Any thoughts?
Store the first 0-300 prime numbers in an array
Bad way of doing it.
Set slider min to 0, and max to 300, and current to 0
When moving the slider check if the slider number exists in the array, if it does, update the label's text with the prime number
let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]
#IBOutlet weak var label: UILabel!
#IBAction func slider(sender: UISlider) {
let sliderNumber = Int(sender.value)
if primes.contains(sliderNumber) {
label.text = "\(sliderNumber)"
}
}
Good way to do it.
Set slider min to 0, and max to 61, and current to 0.
There are 62 prime numbers in the first 0-300.
When moving the slider, change the label text by indexing the primes array.
let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]
#IBOutlet weak var label: UILabel!
#IBAction func slider(sender: UISlider) {
let sliderNumber = Int(sender.value)
label.text = String(primes[sliderNumber])
}
Tested both the first and this second implementation, and the second one is much cleaner on the value transition.
How to convert signed array of [Int8] to unsigned array of [UInt8].
let arryData: [Int8] = [-108, 11, -107, -14, 35, -57, -116, 118, 54, 91, 12, 67, 21, 29, -44, 111]
I just want to convert this above into array of Unsigned [UInt8]. How to achieve this in swift.? Thanks in advance.
If your intention is to convert signed 8-bit integers to
unsigned ones with the same bit representation (e.g. -1 -> 255):
let intArray: [Int8] = [0, 1, 2, 127, -1, -2, -128]
let uintArray = intArray.map { UInt8(bitPattern: $0) }
print(uintArray)
// [0, 1, 2, 127, 255, 254, 128]
[Int8] -> [UInt8]
You haven't specified how you want to treat negative values; by flipping them to their positive counterpart or by removing them. Below follows both cases.
Transforming negative values to positive ones by flipping sign:
let arrayData: [Int8] = [-108, 11, -107, -14, 35, -57, -116, 118, 54, 91, 12, 67, 21, 29, -44, 111]
let arrayDataUnsigned = arrayData.map { UInt8(abs($0)) }
/* [108, 11, 107, 14, 35, 57, 116, 118, 54, 91,
12, 67, 21, 29, 44, 111] */
Or, by removing the negative values:
let arrayDataUnsigned = arrayData.flatMap { $0 < 0 ? nil : UInt8($0) }
/* [11, 35, 118, 54, 91, 12, 67, 21, 29, 111] */