How to print unsigned char data? - c

Im learning C so i have a little problem.
How to print: unsigned char *tlv_buffer = NULL;
In main function:
unsigned char *tlv_buffer = NULL;
int size = 1;
int len = 0;
int result;
tlv_buffer = BKS_MALLOC(size);
result = append_bertlv_data(&tlv_buffer, &size, &len, 0xDF04, 2,
"\x34\x56");
result = append_bertlv_data(&tlv_buffer, &size, &len, 0xDF81, 3, "ref");
BKS_TRACE("-------- success : %d --------- \n", result);
BKS_TRACE("======== %u =======", &tlv_buffer);
(I cannot see what happens in append_bertlv_data)
It should print df 04 02 34 56 df 81 03 72 65 66 ,
but it does not show like that.
My result is 3204447612

You can use the following:
for (int i = 0 ; i < strlen(tlv_buffer); i++)
printf("%02x ",*(tlv_buffer + i));
It will print each byte in hex.
edit:
use a space to separate and if you want the specific length bytes then specify the length instead of size. best is to use strlen.

Related

Float array to bytes converts as null

I am trying to continuously send array of 6 floats over MQTT protocol. I was sending them as ASCII characters using sprintf function. I decided to send them as raw bytes. I put these floats into a union to represent them as unsigned char. The problem is when any of these floats is integer value, their byte representation becomes null after the position of integer.
union {
float array[6];
unsigned char bytes[6 * sizeof(float)];
} floatAsBytes;
If all of floatAsBytes.array consist float values, there is no problem at all.
If I say floatAsBytes.array[0] = 0, floatAsBytes.bytes becomes null.
If I say floatAsBytes.array[3] = 4, I can see first 8 bytes however this time last 16 bytes becomes null.
Minimal example of my client side C-code
#define QOS 0
#define TIMEOUT 1000L
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include "MQTTClient.h"
union bitFloat{
float f[6];
unsigned char s[6*sizeof(float)];
};
void publish(MQTTClient client, char* topic, char* payload) {
MQTTClient_message pubmsg = MQTTClient_message_initializer;
pubmsg.payload = payload;
pubmsg.payloadlen = strlen(pubmsg.payload);
pubmsg.qos = QOS;
pubmsg.retained = 0;
MQTTClient_deliveryToken token;
MQTTClient_publishMessage(client, topic, &pubmsg, &token);
MQTTClient_waitForCompletion(client, token, TIMEOUT);
}
int main(){
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
MQTTClient_create(&client, "MQTTADDRESS:MQTTPORT", "TestClient",
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(-1);
}
int i;
while(1){
union bitFloat payload;
payload.f[0] = 4.53; payload.f[1] = 2.39; payload.f[2] = 28.96; payload.f[3] = -1.83; payload.f[4] = -27.0; payload.f[5] = 9.32;
publish(client, "MyTestTopic", payload.s);
sleep(1);
}
return 0;
}
Python script to receive messages and display them
# !/usr/bin/env python
import struct
import numpy as np
import paho.mqtt.client as mqtt
def on_message(client, userdata, message):
test1 = struct.unpack('<f', message.payload[0:4])[0]
test2 = struct.unpack('<f', message.payload[4:8])[0]
test3 = struct.unpack('<f', message.payload[8:12])[0]
test4 = struct.unpack('<f', message.payload[12:16])[0]
test5 = struct.unpack('<f', message.payload[16:20])[0]
test6 = struct.unpack('<f', message.payload[20:24])[0]
print(test1, test2, test3, test4, test5, test6)
client = mqtt.Client()
client.on_message = on_message
client.connect("MQTTADDRESS", MQTTPORT)
client.subscribe("MyTestTopic")
client.loop_forever()
This line
pubmsg.payloadlen = strlen(pubmsg.payload);
is wrong. You are using strlen on something that isn't a string. Due to use of strlen the length will be wrong as strlen only count til it sees a byte that are zero.
Example:
Consider payload.f[0] = 1;. The binary representation of 1.0 is 3f800000
On little endian systems this will be saved as 00 00 80 3f so using strlen will result in 0.
On big endian systems this will be saved as 3f 80 00 00 so using strlen will result in 2.
In other words - strlen is the wrong function.
You probably need
pubmsg.payloadlen = 6 * sizeof(float);
The code works as expected.
Here is a Minimal Complete Verifiable Example. I am guessing that you are doing something right this. When you provide your own code, it may show that I have misunderstood your question:
#include <stdio.h>
int main()
{
union {
float array[6];
unsigned char bytes[6 * sizeof(float)];
} floatAsBytes;
// load up array with some date
for(int i = 0; i < 6; i++) {
floatAsBytes.array[i] = 1.99 + i;
}
puts("\nfirst run:");
floatAsBytes.array[0] = 0;
// dump array
for(int i = 0; i< 6; i++) {
printf("float #%d: %f\n", i, floatAsBytes.array[i]);
}
// dump bytes
for(int i = 0; i < sizeof(float)*6; i++) {
if(i % sizeof(float) == 0)
printf("\n");
printf(" %2x",floatAsBytes.bytes[i]);
}
// second example
puts("\nSecond run:");
floatAsBytes.array[3] = 4;
// dump array
for(int i = 0; i< 6; i++) {
printf("float #%d: %f\n", i, floatAsBytes.array[i]);
}
// dump bytes
for(int i = 0; i < sizeof(float)*6; i++) {
if(i % sizeof(float) == 0)
printf("\n");
printf(" %2x",floatAsBytes.bytes[i]);
}
return 0;
}
Here is the output:
first run:
float #0: 0.000000
float #1: 2.990000
float #2: 3.990000
float #3: 4.990000
float #4: 5.990000
float #5: 6.990000
0 0 0 0
29 5c 3f 40
29 5c 7f 40
14 ae 9f 40
14 ae bf 40
14 ae df 40
Second run:
float #0: 0.000000
float #1: 2.990000
float #2: 3.990000
float #3: 4.000000
float #4: 5.990000
float #5: 6.990000
0 0 0 0
29 5c 3f 40
29 5c 7f 40
0 0 80 40
14 ae bf 40
14 ae df 40
Process finished with exit code 0
I am not seeing the behavior that you are describing. The code works as expected.

Trying to print out useful data from Memory

void update_memblock(MEMBLOCK *mb)
{
static unsigned char tempbuf[128 * 1024];
SIZE_T bytes_left;
SIZE_T total_read;
SIZE_T bytes_to_read;
SIZE_T bytes_read;
bytes_left = mb->size;
total_read = 0;
while (bytes_left)
{
bytes_to_read = (bytes_left > sizeof(tempbuf)) ?
sizeof(tempbuf) : bytes_left;
ReadProcessMemory(mb->hProc, mb->addr + total_read,
tempbuf, bytes_to_read, &bytes_read);
if (bytes_read != bytes_to_read) break;
memcpy(mb->buffer + total_read, tempbuf, bytes_read);
bytes_left -= bytes_read;
total_read += bytes_read;
}
mb->size = total_read;
}
This is the current code I have, I am initially reading another process' memory using ReadProcessMemory. Now I have the temporary data stored in tempbuf. I am able to output the data from tempbuf in hexadecimal form. But I was planning to display it as shown in the picture, also another complexity I'm facing here is if bytes_left > sizeof(tempbuf) I'm only reading enough data equivalent to the size of tempbuf. How do I read more data as the array I defined can only support as much data?
If I understand correctly, your main question is about how to mimic the output from hex editors.
This can be broken up into 3 parts:
Printing the address
Printing the hex value of each byte
Printing the ASCII value for each byte
Printing an address in hex is easy. We can use %p to print the address of a pointer like this:
char* mem = malloc(99);
printf("%p\n", (void*)mem);
// output: 0xc17080
Next you can print the hex value of a byte using %02x on a char (1 byte). The 02 specifies a zero padded field width of 2. In this case it's just to make 0 print as 00 to make things line up and look pretty.
printf("%02x", mem[0]);
// output: 0A
Lastly, printing ASCII is the easiest of all... almost. We can use %c to print a byte for some ASCII values, but we don't want to print things like \nor \t. To solve this we can limit the use of %c to the character/symbol region of the ASCII table and print . for everything else.
char c = mem[0];
if ( ' ' <= c && c <= '~' ) {
printf("%c", c);
}
else {
printf(".");
}
//output: .
Now we just need to combine these all together. You can decide how many bytes you want to display per line and print "[address] [n hex bytes] [n ascii bytes]" and repeat until you've gone through the entire memory region. I've given a sample function below and you can run it for yourself here.
void display_mem(void* mem, int mem_size, int line_len) {
/*
mem - pointer to beggining of memory region to be printed
mem_size - number of bytes mem points to
line_len - number of bytyes to display per line
*/
unsigned char* data = mem;
int full_lines = mem_size / line_len;
unsigned char* addr = mem;
for (int linno = 0; linno < full_lines; linno++) {
// Print Address
printf("0x%x\t", addr);
// Print Hex
for (int i = 0; i < line_len; i++) {
printf(" %02x", data[linno*line_len + i]);
}
printf("\t");
// Print Ascii
for (int i = 0; i < line_len; i++) {
char c = data[linno*line_len + i];
if ( 32 < c && c < 125) {
printf(" %c", c);
}
else {
printf(" .");
}
}
printf("\n");
// Incremement addr by number of bytes printed
addr += line_len;
}
// Print any remaining bytes that couldn't make a full line
int remaining = mem_size % line_len;
if (remaining > 0) {
// Print Address
printf("0x%x\t", addr);
// Print Hex
for (int i = 0; i < remaining; i++) {
printf(" %02x", data[line_len*full_lines + i]);
}
for (int i = 0; i < line_len - remaining; i++) {
printf(" ");
}
printf("\t");
// Print Hex
for (int i = 0; i < remaining; i++) {
char c = data[line_len*full_lines + i];
if ( 32 < c && c < 125) {
printf(" %c", c);
}
else {
printf(" .");
}
}
printf("\n");
}
}
Sample output:
0x1e79010 74 65 73 74 79 2a 6e t e s t y * n
0x1e79017 0c 3e 24 45 5e 33 27 . > $ E ^ 3 '
0x1e7901e 18 51 09 2d 76 7e 4a . Q . - v . J
0x1e79025 12 53 0f 6e 0b 1a 6d . S . n . . m
0x1e7902c 31 6e 03 2b 01 2f 2c 1 n . + . / ,
0x1e79033 72 59 1c 76 18 38 3c r Y . v . 8 <
0x1e7903a 6e 6b 5b 00 36 64 25 n k [ . 6 d %
0x1e79041 2d 5c 6f 38 30 00 27 - \ o 8 0 . '
0x1e79048 33 12 15 5c 01 18 09 3 . . \ . . .
0x1e7904f 02 40 2d 6c 1a 41 63 . # - l . A c
0x1e79056 2b 72 18 1a 5e 74 12 + r . . ^ t .
0x1e7905d 0d 51 38 33 26 28 6b . Q 8 3 & ( k
0x1e79064 56 20 0b 0b 32 20 67 V . . . 2 . g
0x1e7906b 34 30 68 2e 70 0f 1c 4 0 h . p . .
0x1e79072 04 50 . P
As for the second part of your question, if you cannot increase the size of tempbuf then you are stuck dealing with that amount of memory at any moment in time.
However, if all you want to do is display the memory as described above you can display each section of memory in chunks. You can get a chunk of memory, display it, then get a new chunk, display the new chunk, repeat.
Something along the lines of
while (bytes_left) {
ReadProcessMemory(mb->hProc, mb->addr + total_read, tempbuf, bytes_to_read, &bytes_read);
// Get new chunk
memcpy(mb->buffer + total_read, tempbuf, bytes_read);
// Display chunk
display_mem(tempbuf, bytes_read);
bytes_left -= bytes_read;
}
You will need to do a little more work to check for errors and make everything look nice but hopefully that gives you a good idea of what could be done.
There is no way to store more data than you have space allocated. If you need to store more data, you will need to allocate more space somewhere (more RAM, a disk file, etc). Compression will allow you to store a bit more data in the space allocated, but you aren't going to gain all that much. For virtually unlimited storage, you are going to need to write to disk.
Alternatively, if you just need to display once and then can forget, read in 16 bytes, display the line, and then read the next 16 bytes into the same memory.

How to get hex values from string?

I have a function with an input parameter "text" , which consists of a string containing unknown number of 2-character Hexadecimal numbers separated by space. I need to read them and put them in separate array indexes or read them in for loop and work with them one by one. Each of these values represents an encrypted char(the hex values are ascii values of the characters). How could i do that? I think i should use sscanf(); but i can't figure out how to do it.
char* bit_decrypt(const char* text){
int size=strlen(text)/3;
unsigned int hex[size];
char*ptr;
for(int i=0; i<size+1; i++){
hex[i] = strtoul(text, &ptr, 16);
printf("%x ",hex[i]);
}
return NULL;
}
output is: "80 80 80 80 80 80 80 80 80 80 80 80"
should be: "80 9c 95 95 96 11 bc 96 b9 95 9d 10"
You scan always the first value of text, because you forgot to move the input for strtoul right after the end of the previous scan. That's what the **end-parameter of strtoul is good for: it points to the character right after the last digit of a successful scan. Note: if nothing could have been read in, the end-pointer is equal the input pointer, and this indicates a "wrongly formated number" or the end of the string. See the following program illustrating this. Hope it helps.
int main() {
const char* input = "80 9c 95 95 96 11 bc 96 b9 95 9d 10";
const char *current = input;
char *end = NULL;
while (1) {
unsigned long val = strtoul(current, &end, 16);
if (current == end) // invalid input or end of string reached
break;
printf("val: %lX\n", val);
current = end;
}
}
This is also possible solution with the use of strchr, and strtoul(tmp,NULL,16);
In your solution remember that size_t arr_len = (len/3) + 1; since the last token is only 2 bytes long.
Input text tokens are converted to bytes and stored in char array:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char text[] = "80 9c 95 95 96 11 bc 96 b9 95 9d 10";
size_t len = strlen(text);
size_t arr_len = (len/3) + 1;
printf("len= %zu arr_len= %zu\n", len, arr_len);
printf("Text:\n%s\n", text);
char array[arr_len];
const char *p1 = text;
char tmp[3];
tmp[2] = 0;
printf("Parsing:\n");
for(size_t i=0; i< arr_len; i++)
{
p1 = strchr(p1,' ');
if(p1)
{
tmp[0] = *(p1-2);
tmp[1] = *(p1-1);
array[i]= (char)strtoul(tmp,NULL,16);
printf("%2x ", (unsigned char) array[i]);
p1++;
if(strlen(p1) == 2 ) // the last char
{
i++;
tmp[0] = *(p1);
tmp[1] = *(p1+1);
array[i]= (char)strtoul(tmp,NULL,16);
printf("%2x", (unsigned char) array[i]);
}
}
}
printf("\nArray content:\n");
for(size_t i=0; i< arr_len; i++)
{
printf("%2x ", (unsigned char) array[i]);
}
return 0;
}
Test:
len= 35 arr_len= 12
Text:
80 9c 95 95 96 11 bc 96 b9 95 9d 10
Parsing:
80 9c 95 95 96 11 bc 96 b9 95 9d 10
Array content:
80 9c 95 95 96 11 bc 96 b9 95 9d 10

C turning char arrays into int behaving weird

I have a much longer char array that I am turning into integers, but I cannot figure out why it behaves weird in some spots.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char x[60] = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08";
printf("%lu\n\n", strlen(x));
for ( int i = 0; i < strlen(x); i+=3 ) {
char num[2];
num[0] = (char)x[i];
num[1] = (char)x[i+1];
printf("%d, ", atoi(num));
}
}
The Output:
8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 500, 773, 916, 89,
Everything is great until.....500, 773, 916, 89...what is happening?
As you can see atoi wants a C-String: a null terminated array of character.
So, this
char num[2];
num[0] = (char)x[i];
num[1] = (char)x[i+1];
Have to be
char num[3] = {0};
num[0] = (char)x[i];
num[1] = (char)x[i+1];
num[2] = '\0'; // this could be avoided in your specific case
The need for a proper string with its null character has been posted by many.
Just wanted to add another coding idea: compound literal. (char[]) { x[i], x[i + 1], '\0' } to implement that.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char x[] = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08";
size_t len = strlen(x);
printf("%zu\n\n", len);
for (size_t i = 0; i < len; i += 3) {
printf("%d, ", atoi((char[] ) { x[i], x[i + 1], '\0' }));
}
}
Output
59
8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8,
Some other fixes made too.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char num[3]; // 3rd byte is the null character
num[3]='\0';
char x[60] = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08";
printf("%lu\n\n", strlen(x));
for ( int i = 0; i < strlen(x); i+=3 ) {
strncpy ( num, x+i, 2 ); // Copies two characters starting from x+i
// You get a null terminated string num here.
printf("%d, ", atoi(num));
}
printf("\n");
}
num[0] = (char)x[i];
num[1] = (char)x[i+1];
printf("%d, ", atoi(num)
This assumes that the number of digits in your input will always be 2(for which num should be declared as char num[3]. Do a dry run of your logic with a smaller input set, for eg:"01 50"
i=0
num[0] = *(num+0) = 0
num[1] = *(num+1) = <space>
num[2] = *(num + 2) = ????? Since this memory is not allocated for num
printf("%d, ", atoi("1<space>")) = 1 (atoi stops after looking at num[1] which is a non-digit character)
i = 3
num[0] = *(num+0) = 0
num[1] = *(num + 1) = 0
num[2] = *(num + 2) = ?????
printf("%d ", atoi("00<garbage>...")) // this is UB since atoi will definitely read memory at `(num + 2)` which is beyond the bounds of what the compiler allocated for it.
Try using sscanf to parse the input rather than relying on the number of digits. That would be much more cleaner and less prone to errors.
int main ()
{
char x[60] = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08";
const char *y = x;
char outputBuffer[10];
for(;sscanf(y, "%s", outputBuffer) > 0; y+=strlen(outputBuffer)) printf("%d, ", atoi(outputBuffer));
}
The answer to your question has already been provided, i.e. that a C string is by definition a NULL terminated char array. Without room for the NULL terminator, at best the results from a string passed to a string function cannot be trusted.
I am offering this just to highlight some additional ideas on reading a char array into int array, where the NULL terminator becomes almost a non-issue.
The following method includes simple string parsing and dynamic memory usage, where the string contents get read directly into int memory, bypassing any need for an intermediate string buffer, resulting in the ability to read any legal integer length from the string, and convert directly into int:
see inline comments for suggestions:
int main(void)
{
//leave array index blank, the compiler will size it for you
char x[] = "08 0223 22 97 382345 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 1000";
// ^
int size = sizeof(x)/sizeof(x[0]);//use sizeof macro to get number of elements in array
char *tok = NULL;
int i = 0;
int count=0;
for(i=0;i<size;i++)
{
if(x[i]==' ')count++;//get count to size int array
}
int *array = malloc((count+1)*sizeof(int));
if(array)
{
i=0;//reinitialize to 0 for use here
tok = strtok(x, " \n");
while(tok)//test after each parse before processing
{
if((i>0)&&(i%6==0))printf("\n");//newlines to format
array[i++] = atoi(tok);
printf("%6d, ", array[i]);
// ^ provide spacing in output
tok = strtok(NULL, " \n");
}
free(array);
}
return 0;
}

Building a float from binary files c

I'm trying to build a float number from a binary file with the following format:
first 4 bytes represents the number of digits on the left side of "."
We'll call that X
Next 4 bytes represents the number of digits on the right side of "."
We'll call that Y
Next byte represents the number's sign : "1" => '-', "0" => "+"
Next X+Y bytes represents the number's digits.
Example:
Data.bin:
4 2 0 2 8 3 9 5 3
The number should be : 2839.53
I've the structure :
typedef struct {
unsigned int digitsBefore;
unsigned int digitsAfter;
char sign;
char *digits;
} Number;
Function used to parse and fill the structure :
Number *fromBinaryToNumber(FILE *file){
int counter = 0, size = 0, i = 0;
size_t bytes_read;
unsigned int firstIndex, secondIndex;
char sign;
char *digits;
Number *nr = calloc(1, sizeof(Number));
// reading digits before
bytes_read = fread(&firstIndex, sizeof(unsigned int), 1, file);
nr->digitsBefore = firstIndex;
printf("Bytes read : %zu\n", bytes_read);
printf("First index %d\n", firstIndex);
size += firstIndex;
// reading digits after
bytes_read = fread(&secondIndex, sizeof(unsigned int), 1, file);
nr->digitsAfter = secondIndex;
printf("Bytes read : %zu\n", bytes_read);
printf("Second index %d\n", secondIndex);
size += secondIndex;
// reading sign
bytes_read = fread(&sign, sizeof(char), 1, file);
nr->sign = sign;
printf("Bytes read : %zu\n", bytes_read);
printf("Sign : %.2X\n", sign);
//reading digits
digits = calloc(size, sizeof(unsigned char));
nr->digits = calloc(size, sizeof(unsigned char));
printf("Digits : \n");
bytes_read = fread(digits, size, 1, file);
for(i = 0 ; i < size; i++){
printf("%.2X ", digits[i]);
}
memcpy(nr->digits, digits, size);
free(digits);
return nr;
}
For the following data in Data.bin (Hexdumped):
04 00 00 00 02 00 00 00 00 02 08 03 09 05 03
I get the output:
Bytes read : 1
First index : 4
Bytes read : 1
Second Index : 2
Bytes read : 1
Sign : 00
Digits :
02 08 03 09 05 03
Function to transform Number strunct in float:
double fromNumbertoDouble(Number *number){
int size = 0;
char *buff;
size = number->digitsAfter + number->digitsBefore + 2;
buff = calloc(size, sizeof(char));
if (!buff) {
fprintf(stderr, "Error calloc in fromNumberToDouble");
exit(1);
}
if (number->sign == '01') {
strcat(buff, "-");
}
strncat(buff, number->digits, number->digitsBefore);
strcat(buff, ".");
strncat(buff, number->digits+number->digitsBefore, number->digitsAfter);
return atof(buff);
}
This always return 0.
When I checked to see what is in the number returned from fromBinaryToNumber, number->digits = "\002\b\003\t\005\003".
I'm aware that atof can't convert something like that into a float.
My question is how can i convert the number->digits into something that would get converted into a float by atof.
Any other idea on how to solve this would also be appreciated.
Thank you for your time.
For each of the digits, add '0', i.e. the codepoint value for the digit 0. This turns the array into a a sequence of digit characters. Of course, it must also be 0-terminated for atof(), which requires a proper string.
You do realize that your format is fantastically awkward, right? A single float would use 4 bytes for the entire number, always. If you're going to convert to float anyway you're not going to be able to keep the arbitrary precision that your format supports.
First your buffer size is wrong - you've not taken into account the decimal point, sign or the zero-terminator. Also you don't initialise buff - so strcat may concatenate anywhere- you should either calloc, or memset the buff to 0 before strcat'ing anything to it.

Resources