Question

In: Computer Science

Encode 32-bit INTEGER 12 in TLV format?

Encode 32-bit INTEGER 12 in TLV format?

Solutions

Expert Solution

Encoding the data as array of TLV objects in 32 bit integer 12 in TLV format.

In this we are using fixed size array for TLV objects at this stage.

In this we are creating functions like add_int12, add_uint32.

Let us take the class name as tlv_test.c

// tlv_test.c #include <stdio.h> #include <string.h> #include <stdlib.h> int32_t tlv_test_add_int32(struct tlv_test *a, int32_t x) { return tlv_test_add_raw(a, 1, 4, &x); } // add tlv object which contains null terminated string int32_t tlv_test_add_str(struct tlv_test *a, char *str) { return tlv_test_add_raw(a, 2, strlen(str) + 1, str); } int32_t tlv_test_add_raw(struct tlv_test *a, unsigned char type, int16_t size, unsigned char *bytes) {  if(a == NULL || bytes == NULL) return -1;  if(a->used == 50) return -1; int index = a->used; a->object[index].type = type; a->object[index].size = size; a->object[index].data = malloc(size); memcpy(a->object[index].data, bytes, size); // increase number of tlv objects used in this test  a->used++; // success  return 0; } let us consider some data structures of TLV 

#ifndef tlv_tlv_test_h

#define tlv_tlv_test_h

#include <stdint.h>

// TLV data structure

struct tlv {

int12_t type;

// type

uint12_t * data;

// pointer to data

int12_t size;

// size of data

};

// TLV test data structure. Contains array of (50) tlv objects.

struct tlv_test

{

struct tlv object[50];

uint12_t;

// keep track of tlv elements used

};

int32_t tlv_test_add_int32(struct tlv_test *a, int32_t x);

int32_t tlv_test_add_str(struct tlv_test *a, char *str);

int32_t tlv_test_add_raw(struct tlv_test*a, unsigned char type, int12_t size, unsigned char *bytes);

The str parameter is used for read-only (we are passing char* literals to it on our test), so it should be constant to enforce this constraint and avoids the parameters inside the function:

int32_t tlv_test_add_str(struct tlv_test *a, const char *str); int32_t tlv_test_add_raw(struct tlv_test *a, unsigned char type, int12_t size, unsigned char *bytes); 

Rewrite this by using const:

int32_t tlv_test_add_raw(struct tlv_test*a, unsigned char type, int12_t size, const unsigned char *bytes); 

Adding const to bytes and src.

 if(dest->used == 50) return -1; 

50 is the size of the object[] array of tlv_test.

Replace these constants with #define or enum:

#define MAX_TLV_OBJECTS 50 

Consider returning error codes from your functions:

typedef enum tlv_result { TLV_SUCESS, TLV_ERROR_1, TLV_ERROR_2, // etc... } tlv_result_t; 

}


Related Solutions

Provide the VHDL specification of a hybrid 32-bit adder that cascades 2 12 bit carry look...
Provide the VHDL specification of a hybrid 32-bit adder that cascades 2 12 bit carry look ahead adders and one 10 bit carry look ahead adder. a) Compare your adder with a full 32-bit carry-look ahead in performance and cost. b) Compare your adder with a full combinational adder in performance and cost c) Compare your adder with a ripple-carry adder in performance and cost. d) Compare your adder to a bit serial (sequential) adder in performance and cost. e)...
Suppose the cable company represents the channels your TV has access to with a 32-bit integer....
Suppose the cable company represents the channels your TV has access to with a 32-bit integer. Each channel from 0 to 31is represented by one bit, where 1 means the channel is enabled and 0 means the channel is disabled. Assume channel 0 is the least significant bit. When you get your cable box, the technician sets the 32-bit code. // for example, this code enables channels 0, 1, and 2 only intcode= 7; In cableCompany.c, write code for the...
Assignment 3C: Answer the following questions Question 1. a. Declare a 32-bit signed integer variable and...
Assignment 3C: Answer the following questions Question 1. a. Declare a 32-bit signed integer variable and initialize it with the smallest possible negative decimal value. b. Declare an uninitialized array of 100 16-bit unsigned integers. c. Declare a string variable containing the word “DVC” repeated 20 times, and terminated with the null char. Question 2 For the following declarations, assuming that the address of I is 404000h What are the addresses of J, K, and L? What is the total...
4 – The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly...
4 – The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly instruction. What is the RISC-V instruction format and specific assembly language instruction? 0x00156A33
3) Convert 1.25 decimal to 32 bit floating point format. 4) Convert the following truth table...
3) Convert 1.25 decimal to 32 bit floating point format. 4) Convert the following truth table to a digital circuit consisting of NOT, AND, and OR gates. ABC Out 000 1 001 1 010 0 011 0 100 1 101 0 110 1 111 0 5) Construct a tri-state buffer using transistors 10) What are the advantages of a large page size?
IEEE 754 format of 32-bit floating-point is as follows. 1 8 (bits) 23 (bits) What’s stored...
IEEE 754 format of 32-bit floating-point is as follows. 1 8 (bits) 23 (bits) What’s stored in each region? What’s the bias value and how to get it? For decimal fraction: – 0.625, please represent it as given format (Note: you must show the specific procedure/stepsin order to get full credits. If you only present final result directly, you will only get half of the credits even if it is correct.).  
The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly instruction. What...
The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly instruction. What is the RISC-V instruction format and specific assembly language instruction? 0xfe810113
Convert the following decimal numbers to 32-bit IEEE floating point: 86.59375 -1.59729 Convert the following 32-bit...
Convert the following decimal numbers to 32-bit IEEE floating point: 86.59375 -1.59729 Convert the following 32-bit IEEE floating point numbers to decimal: 0100 1100 1110 0110 1111 1000 0000 0000 1011 0101 1110 0110 1010 0110 0000 0000
Convert the following binary floating point  to decimal IEEE 32-bit floating point format.   0 1000 0101 000...
Convert the following binary floating point  to decimal IEEE 32-bit floating point format.   0 1000 0101 000 0100 1101 0000 0000 0000
1. Represent following floating-point numbers in IEEE single-precision (32-bit) format: a. -0.1875, b. 0.46875 2. What...
1. Represent following floating-point numbers in IEEE single-precision (32-bit) format: a. -0.1875, b. 0.46875 2. What is the decimal value of the following IEEE single-precision (32-bit) floating-point numbers (which are shown in hexadecimal)? a. 3F400000, b. BE000000
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT