0

lets say I have a string "\xfa\xbc\x53\x34\2c" I would like to pass this string in a c code as command line argument. I would like my C code to interpret first character of string as \xfa, second character as \xbc and so on(\xfa,\xbc are ascii vallues of non-printable chars).

my C code is like this:

int main(int argc,char* argv[]){ 
   printf("%s",argv[1]);
}

$./a.out f(\x55\x56\x57\x58)

f() is some encoding scheme may-be

output should be UVWX(\x55 is ascii value of U)

I cannot change my C code I would like a way to pass a string as hexadecimal chars(I hope u understand my question, pardon me if I am confusing)

Mike
  • 9
  • 3
  • First check `argc` to ensure the command line arguments exist. – Harith Feb 05 '23 at 05:17
  • @Haris I didn't understand can u please elaborate? – Mike Feb 05 '23 at 05:19
  • So, what's the problem? You already know how to take command line arguments. – Harith Feb 05 '23 at 05:19
  • `argc` - the first parameter of the `main()` function. `argc` is the number of arguments being passed into your program from the command line – Harith Feb 05 '23 at 05:20
  • @Haris I edited the question with an example please have a look at it – Mike Feb 05 '23 at 05:23
  • `would like my C code to` `I cannot change my C code` I do not understand. What do you _can_ then? What "command-line" do you use? Do you want _your C code_ to convert the _4_ characters ```\``` `x` `f` `a` to `U`, or do you want the conversion to happen _before_ running your C code? Bottom line, _what_ should be the entity doing the conversion? – KamilCuk Feb 05 '23 at 05:23
  • @KamilCuk I use ubuntu command line, I would like some encoding scheme of a string when I give it my command line args so that it interprets \x55 as U(\x55 is ascii value of U in hexadecimal form) – Mike Feb 05 '23 at 05:31
  • `ubuntu command line` See https://en.wikipedia.org/wiki/Unix_shell https://swcarpentry.github.io/shell-novice/01-intro/index.html `when I give it my command line` Is this related to C language at all? Is your program different than `echo`? The syntax `f(\x55\x56\x57\x58)` is invalid in shell. "Encoding scheme" is more unclear. Do you want a _shell function_ to convert the string `\x55` to a character `U`? – KamilCuk Feb 05 '23 at 05:33
  • Check out https://stackoverflow.com/questions/66093712/in-unix-shell-how-to-convert-from-hex-string-to-stdout-bytes-in-machine-endian , https://stackoverflow.com/questions/73997293/interpret-hex-string-as-hex-bytes-for-sha256-in-bash , https://stackoverflow.com/a/58998077/9072753 . Does `echo "$(xxd -p -r <<<'55565758')"` answer your question? – KamilCuk Feb 05 '23 at 05:36
  • @KamilCuk If my question is unclear please have a visit to the following question [link](https://stackoverflow.com/questions/23491997/how-to-pass-an-hex-value-as-string-and-put-it-in-char-variable) please have a look at the both answers along with the comments(I cannot comment on that question because of low reputation) – Mike Feb 05 '23 at 05:39
  • 2
    If your shell is Bash, use [ANSI C quoting](https://www.gnu.org/software/bash/manual/bash.html#ANSI_002dC-Quoting) and write `$'\x55\x56\x57\x58'` to encode the arguments. You end up with `UVWX` passed as the values to the command (`a.out`). – Jonathan Leffler Feb 05 '23 at 05:57
  • Do you expect to see the `\xAB` notation with `./a.out $(cat file)`, or do you expect to see the converted bytes? If you expect to see the `\xAB` notation, what you've written is what you need. If you expect to see the decoded characters, then you need a program to decode the notation — and I'm not aware of a standard utility that does that. It wouldn't be hard to write; the primary problem would be specifying what notations it should convert and what it should leave alone. Octal (`\007`)? C escape sequences (`\f`)? Unicode short escapes (`\uABCD`)? Unicode long escapes (`\U00012345`)? – Jonathan Leffler Feb 06 '23 at 14:48
  • @JonathanLeffler Yeah thank you figured out a way – Mike Feb 06 '23 at 18:06

0 Answers0