Is there a way to save the output to a file directly from the terminal?
I give xxd command to a file on my terminal:
xxd image.jpg
and it gives me its contents in hex:
0003c450: 0124 9248 0492 4920 1249 2480 4924 9201 .$.H..I .I$.I$..
0003c460: 2492 4804 9249 2012 4924 8049 2492 0124 $.H..I .I$.I$..$
0003c470: 9248 0492 4920 1249 2480 4924 9201 2492 .H..I .I$.I$..$.
0003c480: 4804 9249 2012 4924 8049 2492 0124 9248 H..I .I$.I$..$.H
0003c490: 0492 4920 1249 2480 4924 9201 2492 4804 ..I .I$.I$..$.H.
0003c4a0: 9249 2012 4924 8049 2492 0124 9248 0492 .I .I$.I$..$.H..
0003c4b0: 4920 1249 2480 49db ba49 203f ffd9 I .I$.I..I ?..
What I want is to save these values in a file so that I can use them in another program. Is it possible from the terminal? If not how do I incorporate xxd or hexdump into a program?
My guess is that when you say you want to use the output "in another program", you mean you're going to compile it into something. This is what the -i option is for: generate an include file:
xxd -i image.jpg > image.h
Then #include image.h in your project and use the associated variables.
This worked for me:
xxd image.jpg > image.txt
I now have a text file of the hex representation of the image.
When I was using ws npm package, I could use the same computer to create Server and connect client to the server.
But when I use client to connect to another websocket server,
I always get the below-mentioned error. server and client are in the same intranet.
I can make sure that this server could work because I use JavaScript to connect to it.
I'm using node.js version v16.10.0
error: Error: socket hang up
at connResetException (node:internal/errors:691:14)
at Socket.socketOnEnd (node:_http_client:471:23)
at Socket.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1343:12)
at processTicksAndRejections (node:internal/process/task)_queues:83:21) { _queues:83:21) {
code: 'ECONNRESET'
}
Does anybody know why or have any idea?
Here is the client code
import WebSocket from 'ws';
import dotenv from 'dotenv';
dotenv.config();
const ws = new WebSocket('ws://192.168.1.111:9000');
const object = { "message": "config_get" };
const data = JSON.stringify(object);
ws.on('error', (err) => console.log('error:', err));
ws.on('open', function open() {
console.log('success');
});
ws.on('message', function incoming(message) {
console.log(`Server: ${message}`);
});
I know it's a bit late to answer to the issue you had but anyway I hope my solution might help someone or help you if you still need help and did not dump WebSocket aside !
I had the same issue as you with ws
Error: socket hang up
at connResetException (node:internal/errors:691:14)
at Socket.socketOnEnd (node:_http_client:466:23)
at Socket.emit (node:events:532:35)
at Socket.emit (node:domain:475:12)
at endReadableNT (node:internal/streams/readable:1346:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ECONNRESET'
}
for a simple code as:
const ws_session = new WebSocket(proto + '://' + data.ipAddress+":"+data.port);
ws_session.on('open', function open() {
console.log('something');
});
ws_session.on('error', function open() {
console.log('error');
});
ws_session.on('message', function message(data) {
console.log('received: %s', data);
});
I have tried nodejs-websocket and it worked fine !
var options = {extraHeaders: {"origin": "Test"}};
var ws_session = new WebSocket.connect(proto + '://' + data.ipAddress+":"+data.port + '/', options);
ws_session.on('connect', function () {
console.log("### Connected WS");
});
ws_session.on('text', function (msg) {
console.log(msg);
})
So for me the issue was mainly caused by origin field which was missing in the GET HTTP/1.1 .To have a WebSocket Client working correctly with ws just add the origin field like this:
const ws_session = new WebSocket(proto + '://' + data.ipAddress+":"+data.port + '/', {
origin:"Test"
});
A tcpdump of successful connection in case it might help.
tcpdump -i any -s 0 -v -XX -n '(host 10.181.18.224) and (port 9001)'
21:37:00.235233 IP (tos 0x0, ttl 125, id 10420, offset 0, flags [DF], proto TCP (6), length 52)
10.181.18.224.60993 > 172.21.144.81.9001: Flags [S], cksum 0x9925 (correct), seq 4257508728, win 65500, options [mss 1310,nop,wscale 8,nop,nop,sackOK], length 0
0x0000: 0000 0001 0006 0008 e3ff fd90 0000 0800 ................
0x0010: 4500 0034 28b4 4000 7d06 7b14 0ab5 12e0 E..4(.#.}.{.....
0x0020: ac15 9051 ee41 2329 fdc4 6d78 0000 0000 ...Q.A#)..mx....
0x0030: 8002 ffdc 9925 0000 0204 051e 0103 0308 .....%..........
0x0040: 0101 0402 ....
21:37:00.235267 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52)
172.21.144.81.9001 > 10.181.18.224.60993: Flags [S.], cksum 0x5a22 (incorrect -> 0x8999), seq 876928906, ack 4257508729, win 64240, options [mss 1460,nop,nop,sackOK,nop,wscale 10], length 0
0x0000: 0004 0001 0006 00a0 a5e2 51ce 0000 0800 ..........Q.....
0x0010: 4500 0034 0000 4000 4006 e0c8 ac15 9051 E..4..#.#......Q
0x0020: 0ab5 12e0 2329 ee41 3444 df8a fdc4 6d79 ....#).A4D....my
0x0030: 8012 faf0 5a22 0000 0204 05b4 0101 0402 ....Z"..........
0x0040: 0103 030a ...."
21:37:00.283778 IP (tos 0x0, ttl 125, id 10421, offset 0, flags [DF], proto TCP (6), length 40)
10.181.18.224.60993 > 172.21.144.81.9001: Flags [.], cksum 0xc15b (correct), ack 1, win 1028, length 0
0x0000: 0000 0001 0006 0008 e3ff fd90 0000 0800 ................
0x0010: 4500 0028 28b5 4000 7d06 7b1f 0ab5 12e0 E..((.#.}.{.....
0x0020: ac15 9051 ee41 2329 fdc4 6d79 3444 df8b ...Q.A#)..my4D..
0x0030: 5010 0404 c15b 0000 0000 0000 0000 P....[........
21:37:00.285814 IP (tos 0x0, ttl 125, id 10422, offset 0, flags [DF], proto TCP (6), length 206)
10.181.18.224.60993 > 172.21.144.81.9001: Flags [P.], cksum 0x19f2 (correct), seq 1:167, ack 1, win 1028, length 166
0x0000: 0000 0001 0006 0008 e3ff fd90 0000 0800 ................
0x0010: 4500 00ce 28b6 4000 7d06 7a78 0ab5 12e0 E...(.#.}.zx....
0x0020: ac15 9051 ee41 2329 fdc4 6d79 3444 df8b ...Q.A#)..my4D..
0x0030: 5018 0404 19f2 0000 4745 5420 2f20 4854 P.......GET./.HT
0x0040: 5450 2f31 2e31 0d0a 486f 7374 3a20 3137 TP/1.1..Host:.17
0x0050: 322e 3231 2e31 3434 2e38 310d 0a55 7067 2.21.144.81..Upg
0x0060: 7261 6465 3a20 7765 6273 6f63 6b65 740d rade:.websocket.
0x0070: 0a43 6f6e 6e65 6374 696f 6e3a 2055 7067 .Connection:.Upg
0x0080: 7261 6465 0d0a 5365 632d 5765 6253 6f63 rade..Sec-WebSoc
0x0090: 6b65 742d 4b65 793a 2044 354d 6c68 5144 ket-Key:.D5MlhQD
0x00a0: 6d34 775a 6f6b 714c 4e74 4b45 4c4c 513d m4wZokqLNtKELLQ=
0x00b0: 3d0d 0a53 6563 2d57 6562 536f 636b 6574 =..Sec-WebSocket
0x00c0: 2d56 6572 7369 6f6e 3a20 3133 0d0a 6f72 -Version:.13..or
0x00d0: 6967 696e 3a20 5465 7374 0d0a 0d0a igin:.Test....
21:37:00.285843 IP (tos 0x0, ttl 64, id 19753, offset 0, flags [DF], proto TCP (6), length 40)
172.21.144.81.9001 > 10.181.18.224.60993: Flags [.], cksum 0x5a16 (incorrect -> 0xc47a), ack 167, win 63, length 0
0x0000: 0004 0001 0006 00a0 a5e2 51ce 0000 0800 ..........Q.....
0x0010: 4500 0028 4d29 4000 4006 93ab ac15 9051 E..(M)#.#......Q
0x0020: 0ab5 12e0 2329 ee41 3444 df8b fdc4 6e1f ....#).A4D....n.
0x0030: 5010 003f 5a16 0000 P..?Z...
21:37:00.286336 IP (tos 0x0, ttl 64, id 19754, offset 0, flags [DF], proto TCP (6), length 169)
172.21.144.81.9001 > 10.181.18.224.60993: Flags [P.], cksum 0x5a97 (incorrect -> 0xc525), seq 1:130, ack 167, win 63, length 129
0x0000: 0004 0001 0006 00a0 a5e2 51ce 0000 0800 ..........Q.....
0x0010: 4500 00a9 4d2a 4000 4006 9329 ac15 9051 E...M*#.#..)...Q
0x0020: 0ab5 12e0 2329 ee41 3444 df8b fdc4 6e1f ....#).A4D....n.
0x0030: 5018 003f 5a97 0000 4854 5450 2f31 2e31 P..?Z...HTTP/1.1
0x0040: 2031 3031 2053 7769 7463 6869 6e67 2050 .101.Switching.P
0x0050: 726f 746f 636f 6c73 0d0a 5570 6772 6164 rotocols..Upgrad
0x0060: 653a 2077 6562 736f 636b 6574 0d0a 436f e:.websocket..Co
0x0070: 6e6e 6563 7469 6f6e 3a20 5570 6772 6164 nnection:.Upgrad
0x0080: 650d 0a53 6563 2d57 6562 536f 636b 6574 e..Sec-WebSocket
0x0090: 2d41 6363 6570 743a 2064 5946 2f33 6a4b -Accept:.dYF/3jK
0x00a0: 487a 3058 496d 4c6f 616c 6532 3449 7261 Hz0XImLoale24Ira
0x00b0: 4666 3263 3d0d 0a0d 0a Ff2c=....
21:37:00.387744 IP (tos 0x0, ttl 125, id 10425, offset 0, flags [DF], proto TCP (6), length 40)
10.181.18.224.60993 > 172.21.144.81.9001: Flags [.], cksum 0xc034 (correct), ack 130, win 1028, length 0
0x0000: 0000 0001 0006 0008 e3ff fd90 0000 0800 ................
0x0010: 4500 0028 28b9 4000 7d06 7b1b 0ab5 12e0 E..((.#.}.{.....
0x0020: ac15 9051 ee41 2329 fdc4 6e1f 3444 e00c ...Q.A#)..n.4D..
0x0030: 5010 0404 c034 0000 0000 0000 0000 P....4........
21:37:00.387772 IP (tos 0x0, ttl 64, id 19755, offset 0, flags [DF], proto TCP (6), length 128)
172.21.144.81.9001 > 10.181.18.224.60993: Flags [P.], cksum 0x5a6e (incorrect -> 0xb0db), seq 130:218, ack 167, win 63, length 88
0x0000: 0004 0001 0006 00a0 a5e2 51ce 0000 0800 ..........Q.....
0x0010: 4500 0080 4d2b 4000 4006 9351 ac15 9051 E...M+#.#..Q...Q
0x0020: 0ab5 12e0 2329 ee41 3444 e00c fdc4 6e1f ....#).A4D....n.
0x0030: 5018 003f 5a6e 0000 8156 7b22 6d65 7373 P..?Zn...V{"mess
0x0040: 6167 6522 3a22 7265 6164 7922 2c22 7479 age":"ready","ty
0x0050: 7065 223a 2245 4e42 222c 226e 616d 6522 pe":"ENB","name"
0x0060: 3a22 454e 4222 2c22 7665 7273 696f 6e22 :"ENB","version"
0x0070: 3a22 3230 3231 2d30 392d 3138 222c 2274 :"2021-09-18","t
0x0080: 696d 6522 3a34 3433 3334 312e 3937 357d ime":443341.975}
21:37:00.477690 IP (tos 0x0, ttl 125, id 10427, offset 0, flags [DF], proto TCP (6), length 40)
10.181.18.224.60993 > 172.21.144.81.9001: Flags [.], cksum 0xbfdd (correct), ack 218, win 1027, length 0
0x0000: 0000 0001 0006 0008 e3ff fd90 0000 0800 ................
0x0010: 4500 0028 28bb 4000 7d06 7b19 0ab5 12e0 E..((.#.}.{.....
0x0020: ac15 9051 ee41 2329 fdc4 6e1f 3444 e064 ...Q.A#)..n.4D.d
0x0030: 5010 0403 bfdd 0000 0000 0000 0000 P.............
I have trouble compiling my assembly code.
gcc returns: func_select.s:5: Error: invalid character (0xe2) in mnemonic
func_select.s:7: Error: invalid character (0xe2) in mnemonic
here is the code (lines 5-7):
secondStringLength: .string " second pstring length: %d\n"
OldChar: .string "old char: %c,"
NewChar: .string " new char: %c,"
How can I fix this?
Remove the formatting characters embedded in the text.
$ charinfo 'secondStringLength:.string " second pstring length: %d\n"'
U+0073 LATIN SMALL LETTER S [Ll]
U+0065 LATIN SMALL LETTER E [Ll]
...
U+0068 LATIN SMALL LETTER H [Ll]
U+003A COLON [Po]
U+202B RIGHT-TO-LEFT EMBEDDING [Cf]
U+202A LEFT-TO-RIGHT EMBEDDING [Cf]
U+002E FULL STOP [Po]
U+0073 LATIN SMALL LETTER S [Ll]
...
U+0025 PERCENT SIGN [Po]
U+0064 LATIN SMALL LETTER D [Ll]
U+202C POP DIRECTIONAL FORMATTING [Cf]
U+202C POP DIRECTIONAL FORMATTING [Cf]
U+005C REVERSE SOLIDUS [Po]
U+006E LATIN SMALL LETTER N [Ll]
U+0022 QUOTATION MARK [Po]
Igancio Vazquez-Abrams is right. To provide more detail, according to xxd this is your first line:
$ cat b | xxd
00000000: 7365 636f 6e64 5374 7269 6e67 4c65 6e67 secondStringLeng
00000010: 7468 3a20 2020 2020 e280 abe2 80aa 2e73 th: .......s
00000020: 7472 696e 6720 2220 7365 636f 6e64 2070 tring " second p
00000030: 7374 7269 6e67 206c 656e 6774 683a 2025 string length: %
00000040: 64e2 80ac e280 ac5c 6e22 0a0a d......\n"..
Note: e2 80 ab and then e2 80 aa. These are the U+202B and U+202A mentioned earlier. Remove them (as well as the next 2 U+202C).
Heres my code:
factorial :: Integer -> Integer
factorial n = product [1..n]
main = print(factorial 50)
I don't get any errors compiling, but when i run the compiled code
runhaskell test
I get this error:
test:1:1: lexical error at character '\DEL'
What is causing this? How do I solve the problem?
UPDATES
I did a hexdump of the file:
$ hexdump -x test.hs
and got
0000000 6166 7463 726f 6169 206c 3a3a 4920 746e
0000010 6765 7265 2d20 203e 6e49 6574 6567 0a72
0000020 6166 7463 726f 6169 206c 206e 203d 7270
0000030 646f 6375 2074 315b 2e2e 5d6e 6d0a 6961
0000040 206e 203d 7270 6e69 2874 6166 7463 726f
0000050 6169 206c 3035 0029
0000057
Make sure that you're using runhaskell with the source file test.hs rather than the compiled binary test.
If you've used something like ghc to create an executable file, you can just run that directly, with something like:
./test
Be aware that test is probably not a good name for an executable since it's a built-in command on some shells, something that's burnt me before when my test executable doesn't seem to do what I wanted :-)
My web app is displaying some bizarro output (unicode characters that shouldn't be there, etc.). The best I can reckon is that somehow I introduced a bad char somewhere in the source, but I can't figure out where.
I found this answer that states I can do something like:
grep -obUaP "<\x-hex pattern>" .
When I copy the unicode char out of the browser and into my Bless hex editor, it tells me that the exact bytes of the char are:
15 03 01 EF BF BD 02 02
How can I format <\xhex pattern> to match the exact bytes that I need. I tried:
grep -obUaP "<\x-15 03 01 EF BF BD 02 02>" .
But that doesn't work. Thoughts?
Check the post again. FrOsT is not including the '<' and '>' in his actual grep command. He only used the carats to enclose an example statement. His actual statement looks like this:
"\x01\x02"
not:
"<\x01\x02>"
I have a C source file on my computer that begins with the line:
#include <stdio.h>
When I run
grep -obUaP '\x69\x6E\x63\x6C\x75\x64\x65' io.c
I get
1:include
That is, the line number followed by only the string matching the pattern.
You may want to run
man grep
and find out what all those options mean.
It may be easiest to write the pattern of hex bytes to a separate file and load that into stdin for the search.
In this example there is a file sampletext, consisting of the 256 sequential bytes and the occasional newline, and searchstring, a sequence of characters to grep for.
$ xxd sampletext
00000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f ................
00000010: 0a10 1112 1314 1516 1718 191a 1b1c 1d1e ................
00000020: 1f0a 2021 2223 2425 2627 2829 2a2b 2c2d .. !"#$%&'()*+,-
00000030: 2e2f 0a30 3132 3334 3536 3738 393a 3b3c ./.0123456789:;<
00000040: 3d3e 3f0a 4041 4243 4445 4647 4849 4a4b =>?.#ABCDEFGHIJK
00000050: 4c4d 4e4f 0a50 5152 5354 5556 5758 595a LMNO.PQRSTUVWXYZ
00000060: 5b5c 5d5e 5f0a 6061 6263 6465 6667 6869 [\]^_.`abcdefghi
00000070: 6a6b 6c6d 6e6f 0a70 7172 7374 7576 7778 jklmno.pqrstuvwx
00000080: 797a 7b7c 7d7e 7f0a 8081 8283 8485 8687 yz{|}~..........
00000090: 8889 8a8b 8c8d 8e8f 0a90 9192 9394 9596 ................
000000a0: 9798 999a 9b9c 9d9e 9f0a a0a1 a2a3 a4a5 ................
000000b0: a6a7 a8a9 aaab acad aeaf 0ab0 b1b2 b3b4 ................
000000c0: b5b6 b7b8 b9ba bbbc bdbe bf0a c0c1 c2c3 ................
000000d0: c4c5 c6c7 c8c9 cacb cccd cecf 0ad0 d1d2 ................
000000e0: d3d4 d5d6 d7d8 d9da dbdc ddde df0a e0e1 ................
000000f0: e2e3 e4e5 e6e7 e8e9 eaeb eced eeef 0af0 ................
00000100: f1f2 f3f4 f5f6 f7f8 f9fa fbfc fdfe ff0a ................
$ xxd searchstring
00000000: 8081 8283 ....
By redirecting searchstring into stdin, grep can look for the bytes directly
$ grep -a "$(<searchstring)" sampletext | xxd
00000000: 8081 8283 8485 8687 8889 8a8b 8c8d 8e8f ................
00000010: 0a .
$ grep -ao "$(<searchstring)" sampletext | xxd
00000000: 8081 8283 0a .....