C 中的无符号字符连接
Unsigned Char Concat In C
我正在尝试将字符串消息转换为 C 中的十六进制值。
例如,如果我有一条像 "abc" 这样的消息,我想在 162636 等之前收到它。我的代码如下。在这段代码中,我必须做一些连接操作来存储它们,但现在我只能存储 36 个。我该如何存储它们?
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));
}
编辑
我的十六进制编码解决方案:IDEOne
如果你想让你的文本被十六进制编码,你必须分配两次 space原始消息:
"abc" (3 bytes) ==> "616263" (6 bytes)
所以你需要:
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;
}
输出
Hexed is 48656c6c6f20576f726c64
我正在尝试将字符串消息转换为 C 中的十六进制值。
例如,如果我有一条像 "abc" 这样的消息,我想在 162636 等之前收到它。我的代码如下。在这段代码中,我必须做一些连接操作来存储它们,但现在我只能存储 36 个。我该如何存储它们?
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));
}
编辑
我的十六进制编码解决方案:IDEOne
如果你想让你的文本被十六进制编码,你必须分配两次 space原始消息:
"abc" (3 bytes) ==> "616263" (6 bytes)
所以你需要:
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;
}
输出
Hexed is 48656c6c6f20576f726c64