-3

im trying to convert a string message to hex value in C.

For example if i have a message like "abc" i want to have it by 162636 etc. My code is below. In this code, i have to do some concat operation to store them all but now i can store only 36. How can i store them?

unsigned char swapNibbles(char x)
{
    return ( (x & 0x0F)<<4 | (x & 0xF0)>>4 );
}

void encode(char *message, char password[40]) {
    unsigned char *reversedInput = malloc(strlen(message));


    for (int i = 0; i < strlen(message); ++i) {
        reversedInput=swapNibbles(message[i]);
    }
    printf("%2x TERS ",reversedInput);
    //unsigned char *bitwiseMessage = (unsigned char*)message;
    //printf("DÜZ %s\n",bitwiseMessage);
    //printf("TERS %u\n", swapNibbles(bitwiseMessage));
}
Berkin
  • 1,565
  • 5
  • 22
  • 48
  • 1
    This answer provide a way to do that http://stackoverflow.com/a/41356195/7076153 – Stargateur Dec 30 '16 at 14:40
  • 5
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Dec 30 '16 at 14:40
  • @SouravGhosh, if i dont cast it, i will get segmentation fault – Berkin Dec 30 '16 at 14:41
  • 1
    @Berkin That's one more reason for not to cast. Did you include `stdlib.h`? – Sourav Ghosh Dec 30 '16 at 14:42
  • Did the compiler not provide warnings? – Paul Hankin Dec 30 '16 at 14:43
  • To expand what @PaulHankin said, if yes, why you chose to ignore them? – Sourav Ghosh Dec 30 '16 at 14:44
  • 2
    `malloc(strlen(...))` is (almost always) wrong. – melpomene Dec 30 '16 at 14:44
  • `unsigned char *reversedInput = (unsigned char*)malloc(strlen(message));` You don't need malloc --> `unsigned char reversedInput;` – BLUEPIXY Dec 30 '16 at 14:45
  • unsigned char *reversedInput = malloc((unsigned char*) strlen(message)); I convert it to this. Also my reversed input is not a single byte. Its more than 1 @BLUEPIXY – Berkin Dec 30 '16 at 14:46
  • 1
    WTF are you doing? Casting `strlen()` to a pointer and passing it to `malloc`?! Your compiler should be screaming at you for that. – melpomene Dec 30 '16 at 14:47
  • `printf("%02x",reversedInput);` move into for-loop. – BLUEPIXY Dec 30 '16 at 14:51
  • 1
    Then your compiler should be complaining about `reversedInput=swapNibbles(message[i]);` because you're assigning a char (which is an integer) to a pointer. – melpomene Dec 30 '16 at 14:51
  • what's "concation"? And put code inside backticks `\`like this\`` to make it readable – phuclv Dec 30 '16 at 14:51
  • @BLUEPIXY, yeah its right but i need to store them all. How can i store them – Berkin Dec 30 '16 at 14:52
  • @melpomene, should i cast it to (unsigned char) ? Im not good at in C. Thanks – Berkin Dec 30 '16 at 14:53
  • You do not need to store it unless you simply display it and use it elsewhere. – BLUEPIXY Dec 30 '16 at 14:53
  • No, you should read a basic C tutorial or take a class or something. You have no idea what you're doing and your idea of problem solving is sprinkling random casts over the code. Casts aren't magic: They just tell the compiler to shut up; they don't fix problems. – melpomene Dec 30 '16 at 14:55
  • @BLUEPIXY, i need it because i need whole string. For example if i have abc i need 616263 – Berkin Dec 30 '16 at 14:55
  • So `swapNibbles` is not necessary. – BLUEPIXY Dec 30 '16 at 14:56
  • Seriously? Ok im changing my answer. I need to store "whole" of the message. If i have "abc" it means i need "162636". Whole of 162636, not 16,26,36. – Berkin Dec 30 '16 at 14:58
  • I just realized `swapNibbles` has nothing to do with the problem you claim you're trying to solve. Did you just copy/paste that from somewhere else into your program? – melpomene Dec 30 '16 at 14:58
  • Im using it to change A-61 to 16. After change it to 16, i have to store them into reversedInput. And it goes like this. Swap nibbles does this – Berkin Dec 30 '16 at 15:00
  • 1
    you have been saying "i need 616263" for a long time. – BLUEPIXY Dec 30 '16 at 15:01
  • It was an example. My function does this convert. Its not a problem. My problem is just stroing them all, together – Berkin Dec 30 '16 at 15:02
  • 1
    It was totally wrong as an example. – BLUEPIXY Dec 30 '16 at 15:05

1 Answers1

1

Edit

My solution for hex-encoding: IDEOne


If you want your text to be hex-encoded, you will have to allocate twice as much space as the original message:

"abc" (3 bytes) ==> "616263" (6 bytes)

So you will need:

unsigned char *reversedInput = malloc(2*strlen(message)+1);  // +1 for the final NULL-terminator

#include <string.h>
#include <malloc.h>

char* HexEncode(char* txt)
{
    char* hexTxt = calloc(2*strlen(txt)+1,1);
    for(char* p=hexTxt; *txt; p+=2)
    {
        sprintf(p, "%02x", *txt++);
    }
    return hexTxt;
}

int main() {
    char* hexText = HexEncode("Hello World");
    printf("Hexed is %s\n", hexText);
    free(hexText);

    return 0;
}

Output

Hexed is 48656c6c6f20576f726c64
abelenky
  • 63,815
  • 23
  • 109
  • 159