need a math about (byte) 190 = -66 - type-conversion

Arduino decode problem :
inString = "BE";
val = unhex(inString); // the result of val = 190
(byte) val;// val 190 convert to byte return -66
How val converted from int to byte? any math?

You're working with signed byte (which is typical for, say, Java):
0xBE == 190
But 190 > 127 that's why it's negative (providing that byte is signed and so should be in range [-128..127]). To find out the value of 190 representation you should find N such that
190 + N * 256 lies in [-128..127] range
in your case N == -1, and the answer is
190 - 256 == -66

Related

How to convert 20 length byte Ethereum wallet address to hex format found in Metamask?

Using some of the code from Geth's source code https://github.com/ethereum/go-ethereum, I'm trying to find a way to generate valid Ethereum wallets. For starters, I'm using the hex version of secp256k1 prime MINUS 1, which is "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140". Plugging that into Metamask I get an address of "0x80C0dbf239224071c59dD8970ab9d542E3414aB2". I would like to use the functions to get the equivalent address.
I've so far put in the hex format of the private key "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140". I get a 20 length byte array [164 121 62 11 173 85 89 159 11 68 30 45 4 221 247 191 191 44 73 181]
To be specific, I defined the secp256k1 prime - 1's hex value and used Geth's Crypto's newKey() function
var r io.Reader
r = strings.NewReader("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140")
tmp, err := newKey(r)
if err != nil {
fmt.Println(err)
}
fmt.Println(tmp.Address)
I have two questions. First, did I input the private key right as a hex? Second, how do I convert this 20 length byte array to a "normal" hex address?

Converting hex string (delimited with colons) to decimal array: Matlab/Octave

I am extracting some data form a file, including strings like:
n ={[1,1] = 0:7:80:bc:eb:64
[2,1] = 0:7:80:bc:eb:69
[3,1] = 0:7:80:bc:eb:69
[4,1] = 0:7:80:bc:eb:69
}
I need to change the '0' to '00', and the '7' to '07'. Then use the function hex2dec to convert it into decimal numbers.
I'm using the following code:
r=strrep(0:7:80:bc:eb:69 , ':', '');
m= hex2dec(r)
Maybe there is a better way to do it?
You can just split each string on : using strsplit. That gives a cell array of strings (char vectors) that you can directly pass to hex2dec; no need for zero-padding:
n = {'0:7:80:bc:eb:64';
'0:7:80:bc:eb:69';
'0:7:80:bc:eb:69';
'0:7:80:bc:eb:69'}; % data: cell array of strings
k = 1; % select one cell
t = strsplit(n{k}, ':');
result = hex2dec(t);
This gives
t =
'0' '7' '80' 'bc' 'eb' '64'
result =
0
7
128
188
235
100
To get the numbers from all strings as a matrix, join the strings of the cell array using strjoin, apply the above, and then apply reshape:
n = {'0:7:80:bc:eb:64';
'0:7:80:bc:eb:69';
'0:7:80:bc:eb:69';
'0:7:80:bc:eb:69'}; % data: cell array of strings
nj = strjoin(n, ':');
t = strsplit(nj, ':');
result = hex2dec(t);
result = reshape(result, [], numel(n)).';
This gives
result =
0 7 128 188 235 100
0 7 128 188 235 105
0 7 128 188 235 105
0 7 128 188 235 105
strsplit and hex2dec work fine as the above answer suggested. I am providing a simpler and faster solution by sscanf:
n = {'0:7:80:bc:eb:64';
'0:7:80:bc:eb:69';
'0:7:80:bc:eb:69';
'0:7:80:bc:eb:69'}; % data: cell array of strings
t = sscanf(n{1}, '%x:')'
t =
0 7 128 188 235 100

16-bit CRC-CCITT in Matlab

Given the calcCRC() C function shown below, what is the equivalent Matlab function?
16-bit CRC-CCITT in C:
/*
* FUNCTION: calcCRC calculates a 2-byte CRC on serial data using
* CRC-CCITT 16-bit standard maintained by the ITU
* ARGUMENTS: queue_ptr is pointer to queue holding are a to be CRCed
* queue_size is offset into buffer where to stop CRC calculation
* RETURNS: 2-byte CRC
*/
unsigned short calcCRC(QUEUE_TYPE *queue_ptr, unsigned int queue_size) {
unsigned int i=0, j=0;
unsigned short crc=0x1D0F; //non-augmented initial value equivalent to augmented initial value 0xFFFF
for (i=0; i<queue_size; i+=1) {
crc ^= peekByte(queue_ptr, i) << 8;
for(j=0;j<8;j+=1) {
if(crc & 0x8000) crc = (crc << 1) ^ 0x1021;
else crc = crc << 1;
}
}
return crc;
}
Below is the Matlab code I have come up with that seems to be equivalent but does not output the same results:
(Incorrect) 16-bit CRC-CCITT in Matlab:
function crc_val = crc_ccitt_matlab (message)
crc = uint16(hex2dec('1D0F'));
for i = 1:length(message)
crc = bitxor(crc,bitshift(message(i),8));
for j = 1:8
if (bitand(crc, hex2dec('8000')) > 0)
crc = bitxor(bitshift(crc, 1), hex2dec('1021'));
else
crc = bitshift(crc, 1);
end
end
end
crc_val = crc;
end
Here is a sample byte array, represented as an integer array:
78 48 32 0 251 0 215 166 201 0 1 255 252 0 1 2 166 255 118 255 19 0 0 0 0 0 0 0 0 0 0 0 0 3 0
The expected output is two bytes base10(44 219) which is base2(00101100 11011011) or base10(11483).
My Matlab function gives base10(85) which is base2(00000000 01010101).
Any ideas on what is causing the output to not be the expected?
You should try bitsll() instead of bitshift(). The former is guaranteed to do what you want, whereas the behavior of the latter depends on properties of crc.
You will also need to and with 0xffff at the end.

Matlab : How I can creat a polynomial generator Reed Solomon for QR Code

I have to make a matlab program, which should create a QR Code.
My problem is the Reed Solomon error correction
The user enters the word he wants. [...] I got a string of numbers I should be gone in a polynomial generator (Reed Solomon) (I found some sites that do this very well: http://www.pclviewer.com/rs2/calculator.html)
I would like it to happen: for example I input: 32 91 11 120 209 114 220 77 67 64 236 17 236
[Reed Solomon generator polynomial]
and I want to find out: 168 72 22 82 217 54 156 0 46 15 180 122 16
I found the functions rsenc comm.rsencoder gf ... But it is impossible to understand the operation of these functions. Functions are detailed: http://www.mathworks.fr/fr/help/comm...n.html#fp12225
I tried a code of this type :
n = 255; k = 13; % Codeword length and message length
m = 8; % Number of bits in each symbol
msg = [32 91 11 120 209 114 220 77 67 64 236 17 236]; % Message is a Galois array.
obj = comm.RSEncoder(n, k);
c1 = step(obj, msg(1,:)');
c = [c1].';
He produced a string of 255 while I want 13 output.
Thank you for your help.
I think that you are committing a mistake.
'n' is the length of final message with parity code.
'k' is the lenght of message (number of symbols)
I guess that this will help you:
clc, clear all;
M = 16; % Modulation Order || same that Max value, at your case: 256! 2^m
hEnc = comm.RSEncoder;
hEnc.CodewordLength = M - 1; % Max = M-1, Min = 4, Must be greater than MessageLenght
hEnc.MessageLength = 13; % Experiment change up and down value (using odd number)
hEnc.BitInput = false;
hEnc
t = hEnc.CodewordLength - hEnc.MessageLength;
frame = 2*hEnc.MessageLength; % multiple of MensagemLength
fprintf('\tError Detection (in Symbols): %d\n',t);
fprintf('\tError Correction: %.2f\n',t/2);
data = randi([0 M-1], frame, 1); % Create a frame with symbols range (0 to M-1)
encodedData = step(hEnc, data); % encod the frame

Agilent Vee or Matlab: 4 byte ASCII to floating point

I currently have an instrument that sends 4 bytes representing a floating point number of 32-bit in little endian format, the data looks like:
Gz*=
<«�=
N×e=
or this
à|ƒ=
is there a conversion for this in matlab, Agilent vee and manually
To convert an array of char to single, you can use typecast:
c = 'Gz*=';
f = typecast(c, 'single')
f = 0.041621
Just implicitly!
>> data = ['Gz*=';'<«�=';'N×e=']
data =
Gz*=
<«�=
N×e=
>> data+0
ans =
71 122 42 61
60 171 65533 61
78 215 101 61
data+0 forces it to be interpreted as a number which is fine.
If it's interpreted it backwards (I'm not sure if MATLAB is big or little endian) just use the swapbytes function.