I am trying to split an unsigned int into the numbers into places: for example the number 234 becomes in the unsigned char array {0,0,0,2,3,4} and i am seeing strange effects , i am not sure i am doing it the right way, is there a better one?
here is the code i am using now:
void display_decl(unsigned int j)
{
unsigned char lst[6];
lst[5] = j & 0x0f;
lst[4] = j >> 4 & 0x0f;
lst[3] = j >> 8 & 0x0f;
lst[2] = j >> 12 & 0x0f;
lst[1] = j >> 16 & 0x0f;
lst[0] = j >> 20 & 0x0f;
display_digits(lst);
}
To split a number into char code needs use the intended base and null character terminate to treat as a string.
#include <limits.h>
void display_decl(unsigned int j) {
char lst[sizeof j * CHAR_BIT + 1];
unsigned base = 10; // or 16 or any base 2 to 16
char *p = &lst[sizeof lst] - 1;
*p = '\0';
do {
p--;
*p = "0123456789ABCDEF"[j%base];
j /= base;
} while (j > 0);
display_string(p);
}
Related
I want to convert 1byte of array element into 2 bytes
E.g
arr[size] = {0x1F};
So, I want 0x1F will be stored in
2nd array like,
arr_2[size] = {0x01, 0x0f}
I have tried like following way...
for(i=j=0; j<2; i++){
arr_2[j] =(0xF0 & arr[i]) >> 4;
arr_2[j++]=(0x0F & arr[i]);
}
Thanks is advance..!!
In fact you are doing all correctly except the for loop statement
for(i=j=0; j<2; i++){
arr_2[j] =(0xF0 & arr[i]) >> 4;
arr_2[j++]=(0x0F & arr[i]);
}
in the body of which you are setting at least the same element of the array arr_2 twice.
It is redundant.
You could just write
arr_2[0] =(0xF0 & arr[0]) >> 4;
arr_2[1] = 0x0F & arr[0];
To build on Vlad's answer, maybe you used a loop because you really want to expand n bytes into n*2 bytes.
for ( size_t i = 0; i < n; ++i ) {
dst[ i*2+0 ] = ( src[ i ] >> 4 ) & 0xF;
dst[ i*2+1 ] = ( src[ i ] >> 0 ) & 0xF;
}
or
for ( size_t j = 0, i = 0; i < n; ++i ) {
dst[ j++ ] = ( src[ i ] >> 4 ) & 0xF;
dst[ j++ ] = ( src[ i ] >> 0 ) & 0xF;
}
You were almost there, but didn't use the proper for() syntax to increment multiple iterators.
Example:
#define SIZE 1 // for example, but the same principles
// would apply for malloc'd arrays
unsigned char arr[SIZE] = {0x1F}; // 'SIZE' bytes
unsigned char arr_2[SIZE * 2]; // You'll end up with twice as many bytes.
// ...
int i, j;
// note how we test the input iterator (i) against the input size and how
// the output iterator (j) is incremented by 2 on each iteration
for (i = 0, j = 0; i < SIZE; i++, j += 2)
{
arr_2[j] = (arr[i] & 0xF0) >> 4;
arr_2[j + 1] = arr[i] & 0x0F;
}
You don't need to maintain two index variables. Here is a solution assuming you want to store the elements in an array of twice the length of the source array:
#include <stdio.h>
#define LEN(arr) (sizeof (arr) / sizeof (arr)[0])
int main(void)
{
unsigned char arr[] = {0x1f, 0x2f, 0x3f, 0x4f};
unsigned char arr_2[2 * LEN(arr)];
size_t i;
for (i = 0; i < LEN(arr); i++) {
arr_2[2 * i] = arr[i] >> 4;
arr_2[2 * i + 1] = arr[i] & 0x0f;
}
for (i = 0; i < LEN(arr_2); i++) {
printf("%x", arr_2[i]);
}
printf("\n");
return 0;
}
Output:
1f2f3f4f
Through overall search in Stack Overflow. I got a hint :For binary data using memcpy
for (int i=0; i < N; ++i)
memcpy(buffer + i * byte_sequence_length, byte_sequence,
byte_sequence_length);
But even though, the code is not working, please suggest me what mistake in the below code.
Code
void main ( int bit)
static unsigned lfsr = 0xCD;
int i,j;
int buff[];
for ( i = 0; i < 50; i++)
{
bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 4) ) & 1;
lfsr = (lfsr >> 1) | (bit << 7);
buff[i] = bit;
}
for (int j=0; j < 50; ++j)
{
memcpy(buff+ j, lfsr, 50*sizeof(int));
}
If you declare the buff array as a local variable, HLS will not generate a memory port to your top function. You should make it an argument of the function.
Input:
char arr1[9] = "+100-200" // (+ is 2B, - is 2D, 1 is 31 and 2 is 32)
Output:
unsigned int arr2[4]= [0x2B31,0x3030,0x2D32,0x3030]
How can I do this?
Your question seems inconsistent: 0 should convert to 0x30, its ASCII value.
Why this modification, the code is quite straightforward:
char arr1[8] = "+100-200";
unsigned int arr2[4];
for (int i = 0; i < 8; i += 2) {
arr2[i / 2] = ((unsigned int)(unsigned char)arr1[i] << 8) |
(unsigned int)(unsigned char)arr1[i + 1];
}
for (int i = 0; i < 4; i++) {
printf("0x%04X ", arr2[i]);
}
printf("\n");
Output:
0x2B31 0x3030 0x2D32 0x3030
I am trying to store 8 bytes in a byte array to store the value of a pointer.
int main() {
unsigned long a = 0;
char buf[8];
int i = 0;
int *p = &i;
a = (unsigned long)p;
while (i < 8)
{
buf[i] = (a >> (8 * i)) & 0xFF;
i++;
}
a = 0;
i = 0;
while (i < 8)
{
a = ?
i++;
}
p = (int *)a;
}
The first loop stores successive bytes of p, as casted into usigned long in a, but I don't know how to retrieve the value in the second loop. Does somebody have a clue?
This is the inverse code to yours:
a = 0;
while (i < 8)
{
a |= ((unsigned long)buf[i] & 0xff ) << (8 * i);
i++;
}
I have an array with 16 elements. I would like to evaluate these to a boolean 0 or 1 and then store this in 2 bytes so i can write to a binary file. How do I do this?
Something like this you mean?
unsigned short binary = 0, i;
for ( i = 0; i < 16; ++i )
if ( array[i] )
binary |= 1 << i;
// the i-th bit of binary is 1 if array[i] is true and 0 otherwise.
You have to use bitwise operators.
Here's an example:
int firstBit = 0x1;
int secondBit = 0x2;
int thirdBit = 0x4;
int fourthBit = 0x8;
int x = firstBit | fourthBit; /*both the 1st and 4th bit are set */
int isFirstBitSet = x & firstBit; /* Check if at least the first bit is set */
int values[16];
int i;
unsigned short word = 0;
unsigned short bit = 1;
for (i = 0; i < 16; i++)
{
if (values[i])
{
word |= bit;
}
bit <<= 1;
}
This solution avoid the use of the if inside the loop:
unsigned short binary = 0, i;
for ( i = 0; i < 16; ++i )
binary |= (array[i] != 0) << i;
Declare an array result with two bytes, then you loop through the source array:
for (int i = 0; i < 16; i++) {
// calclurate index in result array
int index = i >> 3;
// shift value in result
result[index] <<= 1;
// check array value
if (theArray[i]) {
// true, so set lowest bit in result byte
result[index]++;
}
}
Something like this.
int values[16];
int bits = 0;
for (int ii = 0; ii < 16; ++ii)
{
bits |= (!!values[ii]) << ii;
}
unsigned short output = (unsigned short)bits;
the expression (!!values[ii]) forces the value to be 0 or 1, if you know for sure that the values array already contains either a 0 or a 1 and nothing else, you can leave of the !!
You could also do this if you don't like the !! syntax.
int values[16];
int bits = 0;
for (int ii = 0; ii < 16; ++ii)
{
bits |= (values[ii] != 0) << ii;
}
unsigned short output = (unsigned short)bits;