Nп/п : 37 из 100
 От   : hongy...@gmail.com                  2:5075/128        05 сен 23 17:31:51
 К    : Ben Bacarisse                                         05 сен 23 03:34:02
 Тема : Re: Split a hex string into bytes and compute the xor of them.
----------------------------------------------------------------------------------
                                                                                 
@MSGID:
<fa93482f-c6a7-454e-88d0-b83e9842a0a2n@googlegroups.com> c4435ac2
@REPLY: <87ledlu66t.fsf@bsb.me.uk> 1860c100
@REPLYADDR hongy...@gmail.com
<hongyi.zhao@gmail.com>
@REPLYTO 2:5075/128 hongy...@gmail.com
@CHRS: CP866 2
@RFC: 1 0
@RFC-References:
<8404323f-40c3-473b-a5fd-a37e7af1cb3bn@googlegroups.com> 1@dont-email.me> <87jzt6vez1.fsf@bsb.me.uk>
<c47b672e-ea58-4f16-8de3-405411cf05e4n@googlegroups.com> <87ledlu66t.fsf@bsb.me.uk>
@RFC-Message-ID:
<fa93482f-c6a7-454e-88d0-b83e9842a0a2n@googlegroups.com>
@TZUTC: -0700
@PID: G2/1.0
@TID: FIDOGATE-5.12-ge4e8b94
On Tuesday, September 5, 2023 at 2:47:11 AM UTC+8, Ben Bacarisse wrote:
> "hongy...@gmail.com" <hongy...@gmail.com> writes: 

> > On Monday, September 4, 2023 at 10:39:52 AM UTC+8, Ben Bacarisse wrote: 
> >> Janis Papanagnou pap...@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?
> Note that Janis is talking about bash now...
> >> You can use & but I think it`s new: 
> >> 
> >> printf `0x%X
` $((${data//??/0x&^}0)) 
> > 
> > I tried to understand the above code snippet with the help of 
> > chatgpt-4 as follows: 
> > 
> > ### begin quote ### 
> > data=... 
> > printf `0x%X
` $((${data//??/0x&^}0)) 
> > 
> > explain the above ksh code.
> It`s not ksh code (or not intended to be at last).
 > > The provided script line is an example of shell arithmetic
expansion and parameter substitution in KornShell (ksh). 
> > 
> > The code does the following: 
> > 
 > > 1. `data=...`: This defines a variable `data`. It`s supposed to
have some hex value. The value isn`t assigned in your example; it`s
merely given as ellipsis `...`. 
> > 
> > 2. `printf `0x%X
` $((${data//??/0x&^}0))`: This is a more complex part. 
> > 
> > - `printf `0x%X
 ``: This is a printf command that prints a number in hexadecimal
format (`0x` at the beginning). The `%X` is a format specification for the
hexadecimal output. 
> > 
 > > - `${data//??/0x&^}0`: This is a pattern substitution with
arithmetic expansion. 
> > 
 > > - `${data//??/0x&}`: This will replace every two consecutive
characters (`??`) in `data` with `0x&` (where `&` represents the matched part).
So, if `data` was `1234`, after this operation it would become
`0x120x34`. 
> > 
 > > - `${data//??/0x&^}0`: The `^` is bitwise XOR in ksh. It`s
used to XOR all the hexadecimal numbers together. The trailing `0` is
necessary as the first XOR operation is between the `0` and the first
replaced value. 
> > 
 > > The whole command is useful in converting multiple byte ASCII
values into one big endian hexadecimal value. It opens up the byte string,
calculates the XOR of all bytes, and prints out the final value in
hexadecimal format. 
> > ### end quote ### 
> > 
> > But I`m not sure if the above explanation is completely correct.
> Nor me. Why use something like ChatGPT that needs checking? Why not 
> check it yourself from the bash manual? Or you could ask the group to 
> explain it if there`s some bit you don`t follow. 

I tried to test the above code snippet as follows:

werner@X10DAi:~$ echo $SHELL
/bin/bash

 werner@X10DAi:~$
data=23230DFE4C5336413245313636504135303333333401000A130A1814300A01010101
werner@X10DAi:~$ printf `0x%X
` $((${data//??/0x&^}0)) 
0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^
0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0:"^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x
&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0x&^0")
werner@X10DAi:~$ printf `0x%X
` $((${data//??/0x\\1^}0)) 
0x0
 
> -- 
> Ben.

Zhao
--- G2/1.0
 * Origin: usenet.network (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



   GoldED+ VK   │                                                 │   09:55:30    
                                                                                
В этой области больше нет сообщений.

Остаться здесь
Перейти к списку сообщений
Перейти к списку эх