How to create buffer of hexadecimal value from hexa string? [duplicate] - c

This question already has answers here:
Hexadecimal string to byte array in C
(21 answers)
How to correctly convert a Hex String to Byte Array in C?
(4 answers)
Closed 2 months ago.
given the following string:
char *hexa_string= "ab010210000000000000000000000a0040890080006400950000920050050900300c3b811504a0ff2c8cf5e91740e466b219c2a3318ce80b66e1c43ba65ade66"
how can I convert it to buffer so that any cell in the buffer will be the value of any two digits from the above string ?
I tried to do something like that:
char *hexa_string= "ab010210000000000000000000000a0040890080006400950000920050050900300c3b811504a0ff2c8cf5e91740e466b219c2a3318ce80b66e1c43ba65ade66";
int hexa_string_size = strlen(hexa_string);
uint8_t hexa_buffer[64];
char *temp = hexa_string;
for(int i = 0; i < hexa_string_size; i++){
sscanf(temp, "%02hhx", &hexa_buffer[i]);
temp += 2;
}
but while I trying to printf the hexa_buffer I see nothing..
I want to get the following buffer:
hexa_buffer[0] = 0xab
hexa_buffer[1] = 0x01
hexa_buffer[2] = 0x02
..

Related

Size of a character is showing 2 bytes in ANSI C? [duplicate]

This question already has answers here:
Sizeof string literal
(2 answers)
Closed 2 years ago.
I want to know the reason behind why I am getting 2 bytes as a size of the character and at the same time 1 byte for the similar character when stored in a place-holder.
#include <stdio.h>
void main(void) {
char a = "$";
printf("%d\n", sizeof("$")); // Here output is 2 which should be 1
printf("%d", sizeof(a)); // Here output is 1 which is correct
}
it treats "$" as a string, so it's NULL terminated and has a sizeof 2.
same would happen if you would do sizeof "a".
if you want to get the sizeof the character itself you should do sizeof with '$', that's the symbol for character.

How to extract the first X characters of a string and use them into another in C [duplicate]

This question already has answers here:
How should character arrays be used as strings?
(4 answers)
Closed 3 years ago.
I have made this function to extract the first 2 characters of a string:
string get_salt(string hash)
{
char pre_salt[2];
for (int i = 0; i < 2; i++)
{
pre_salt[i] = hash[i];
}
string salt = pre_salt;
printf("%s\n", salt);
return(salt);
}
But when I run it with a string that has "50" (is the example I'm using) as the first 2 characters, I get this output:
50r®B
And to be honest I have no idea why is it adding the 3 extra characters to the resulting string.
You are missing the string's NUL terminator '\0' so it keeps printing until it finds one.
Declare it like:
char pre_salt[3]={0,0,0};
And problem solved.

How to convert int to string in C? [duplicate]

This question already has answers here:
How can I convert an int to a string in C?
(10 answers)
Closed 7 years ago.
I'm trying to convert an integer to a string in C but the current code doesn't make it.
I'm not seeking to display it in the screen, so all the functions printf, sprintf... are irrelevant.
int X = 15;
char *T;
T = (char*)X;
// Expected result : "15"
Can anyone help please ?
Thanks.
Not displaying it to screen doesn't invalidate functions like sprintf() since they literally "print to string".
int X = 15;
char buffer[10];
memset(&buffer, 0, sizeof(buffer)); // zero out the buffer
sprintf(buffer, "%d", X);
// Expected result : "15"
printf("contents of buffer: %s\n", buffer);
sprintf will print to a string, not the screen.
It's exactly what you're looking for.

How do i change an int variable to a string variable in c? [duplicate]

This question already has answers here:
Converting int to string in C
(14 answers)
Closed 8 years ago.
You read the question I need to be able to scan a int value and print it back out as a string variable
If you just want to print it back, you can use the "%d" specifier when using printf. If you want to convert the integer value to a character string for other purposes, you can use itoa.
YOu should use sprintf instead of itoa as it iota is not a starndard
int aInt;
scanf("%d", &aInt)
char str[50];
sprintf(str, "%d", aInt);
detail use of sprintf
You can use the sprintf function to do it.
int a;
// [log(2^63 - 1)] + 1 = 19 where
// [x] is the greatest integer <= x
// +1 for the terminating null byte
char s[19+1];
// read an integer
scanf("%d", &a);
// store the integer value as a string in s
sprintf(s, "%d", a);

how to print a certain number of bytes(characters) pointed by a pointer [duplicate]

This question already has answers here:
How can I convert an integer to a hexadecimal string in C?
(7 answers)
Closed 9 years ago.
I have a pointer which point to a memory of 20 bytes, and then copy something to the memory
u_char* pkt=malloc(20);
memcpy(pkt, somecontent, 20);
I want to examine the 20 bytes starting from pkt
so I want to print all the bytes with a format like 0xa6
how to do this in language C
thanks!
Try printf :
int i=0;
for (; i<20; i++)
printf("0x%.2x ", pkt[i]);
printf("\n");
The following code is sufficient,
int i=0;
u_char * ptr=pkt;
for (; i<20; i++)
printf("%x ", ptr++);
printf("\n");

Resources