Nп/п : 34 из 100
От : Ben Bacarisse 2:5075/128 04 сен 23 03:39:46
К : Janis Papanagnou 04 сен 23 05:46:05
Тема : Re: Split a hex string into bytes and compute the xor of them.
----------------------------------------------------------------------------------
@MSGID: <87jzt6vez1.fsf@bsb.me.uk> 2e3c4546
@REPLY: 1@dont-email.me> 00d27d0a
@REPLYADDR Ben Bacarisse <ben.usenet@bsb.me.uk>
@REPLYTO 2:5075/128 Ben Bacarisse
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID: <87jzt6vez1.fsf@bsb.me.uk>
<
8404323f-40c3-473b-a5fd-a37e7af1cb3bn@googlegroups.com>
il.me>
@TZUTC: 0100
@PID: Gnus/5.13 (Gnus v5.13) Emacs/28.2
(gnu/linux)
@TID: FIDOGATE-5.12-ge4e8b94
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 03.09.2023 08:26, hongy...@gmail.com wrote:
>> I want to split a hex string into bytes and compute their xor,
as shown below:
>>
>> werner@X10DAi:~$ data=$( sed `s/\\(..\\)/\\1 /g` <<<
23230DFE4C5336413245313636504135303333333401000A130A1814300A01010101 | sed -e `s/[ ]*$//` )
>> werner@X10DAi:~$ IFS=` ` read -ra ADDR <<< "$data"; xor=0; for i
in "${ADDR[@]}"; do xor=$((xor ^ 0x$i)); done; printf `%x
` $xor
>> ea
>>
>> I wonder if there is a more concise and simple method for this purpose.
>
> You`ve already got a couple solutions. So just for completeness here`s
> a Kornshell solution without a shell loop or external commands
>
> data=...
> printf "%2x
" $((${data//@(??)/0x\\1^}0))
Much neater.
> (functionally it`s a variant of Ben`s proposal). Unfortunately it seems
> that bash (with extglob set) does not support the \\1 in the
> expression?
You can use & but I think it`s new:
printf `0x%X
` $((${data//??/0x&^}0))
and it can be turned on and off via a variable so not idea in something
portable.
--
Ben.
--- Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
* Origin: A noiseless patient Spider (2:5075/128)
SEEN-BY: 5001/100 5005/49 5015/255 5019/40 5020/715
848 1042 4441 12000
SEEN-BY: 5030/49 1081 5075/128
@PATH: 5075/128 5020/1042 4441