datahex = sprintf('%02x', T1)??

T1 =00111001 10001010 01011001 1011010010101100000000000100100011010001010110011110001001101010111100110111101111
datahex =
30303131313030312031303030313031302030313031313030312031303131303130303130313031313030303030303030303030313030313030303131303130303031303130313130303131313130303031303031313031303130313131313030313130313131313031313131
T1=[T1 count]
datahex = sprintf('%02x', T1)
I don't know why output in that format],T1 is character

 Risposta accettata

Guillaume
Guillaume il 16 Gen 2015
Modificato: Guillaume il 16 Gen 2015
First, if there are spaces in your T1 string, get rid of them:
T1(T1 == ' ') = []; %remove spaces
For demo purpose:
T1 = char(randi(double(['0' '1']), 1, 128)); %random demo string of 128 '0' and '1'
Then, assuming that the length of T1 is a multiple of 8:
datahex = num2cell(lower(dec2hex(bin2dec(reshape(T1, [], 8)))), 2)'
Basically, reshape your string into columns of 8 characters. Convert each row from binary string to decimal number and then from decimal number to hexadecimal string. Optionally, convert to lowercase and cell array.

3 Commenti

I write that ( I test for 64 bit): T1 = char('10011000 00011011 10100110 10000010 01001100 00011011 11111011 00011010') T1(T1 == ' ') = []; %remove spaces datahex = num2cell(upper(dec2hex(bin2dec(reshape(T1, [], 8)))), 2)'
the output is: datahex =
'B2' '0A' '22' 'C7' 'CF' '28' '77' '46'
but It should be
Plaintext = (hex) 981ba682 4c1bfb1a b4854720 29b71d80
the output in right shape but wrong value thanks
Doh! Sorry, should have tested and thought through the code better.
Reshape into 8 rows and transpose, rather than 8 columns:
datahex = num2cell(lower(dec2hex(bin2dec(reshape(T1, 8, [])'))), 2)'
Radwa
Radwa il 16 Gen 2015
thanks so much, I have another Question If I want to add 1 first to the 128 bit then convert, the problem I face is that to add 1 it must be dec, and if I use bin2dec it must be less than 52 bit

Accedi per commentare.

Più risposte (1)

Geoff Hayes
Geoff Hayes il 16 Gen 2015
Modificato: Geoff Hayes il 16 Gen 2015

0 voti

Radwa - your T1 is a string of characters, so the zeros and ones are characters. The ASCII code for a character 0 and character 1 is 48 and 49 respectively. The hexadecimal equivalent of each is 30 and 31, so your above answer makes sense. (The 29 would be for the space character.)

2 Commenti

Radwa
Radwa il 16 Gen 2015
ّI want to deal with it as binary string and convert to hexadecimal string
I count deal with bin2hex for 2reasons: 1. count will be 128 bits 2. I want to make it in this format {'39' '8a' '59' 'b4' 'ac' '00' '00' '00''00' '00' '00' '00' '00' '00' '00' '00'}
Geoff Hayes
Geoff Hayes il 16 Gen 2015
I don't understand your format. How does your input string translate to that? There are limitations to the size of integers that you can use with these functions, and if you insist on using 128 bits, you may need to create a class that can handle a data type of that size.

Accedi per commentare.

Categorie

Richiesto:

il 16 Gen 2015

Commentato:

il 16 Gen 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by