Separating octets in a hex string in MatLab -
i have long string of hex values @ point lost formatting , looks single long string, such as:
str1= 001ba212c6b4001f162e9ab208004500003f1bdc40008011322ea9fe58a6a9fe0001c350c350002b0345001ba212c6b400007100be002c151300be0000000108000b000133ffffffffffffffff i want separate octets. great string vector octets divided so:
str2= 00 1b a2 12 ... 00 00 00 the intuitive approach tried manages separate single characters instead of single hex value yields 1 row vector 2 chars back:
for i=2:2:length(str1) str2(i/2,1)=str1(i-1); str2(i/2,2)=str1(i); end the output is:
str2= 00 1b a2 12 .. which not bad, better. ideas or fancy functions not aware of?
the simplest way reshape string two-column matrix of chars:
str2 = reshape(str1, 2, [])'; if want delimit octet strings spaces, can use sprintf instead:
str2 = [sprintf('%c%c ', str1(1:end - 2)), str1(end - 1:end)]; sprintf reads str1, 2 chars @ time, , adds interleaving spaces. since last 2 chars don't need trailing space, concatenated separately.
Comments
Post a Comment