Store 2 ascii characters into 1 integer in C [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently working on a project involving ascii command strings for a display. I have a string in my program (command string defined by a separate function) that I need to take apart and store into an array. I need to store 2 ascii characters into each element of the array. Here's my problem, the device is looking for ascii in HEX, my hardware will store the variables as decimal ascii. So if I wanted to send the characters 'B' and 'A' to the first element, the display would expect to see 4241 in HEX, it would be 16961 in decimal rather than 6665. If anyone has any suggestions, I would be extremely interested. Thank you

You can just combine the two chars using a shift and bitwise OR:
char ch1 = 'A', ch2 = 'B';
uint16_t buff = ch2 << 8 | ch1; // buff = 0x4241 = 16961
LIVE DEMO
Note on programming style: even though it's not necessary, some people prefer to add parentheses for clarity:
uint16_t buff = (ch2 << 8) | ch1; // buff = 0x4241 = 16961

Related

How to merge multiple numbers into one number like 4,0,0 into 400 without stdlib [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm trying to parse a file that has following data eg:
MAGICNUMBER 400
4 is = 0x34
0 is = 0x30
4
0
0
are different unsigned chars
what i want is those different chars to be converted into
unsigned int x = 400;
when parsing them into my program i want to merge them into one integer i tried bitshifting but it didn't work and i probably did it very wrong and got a very large number probably due misunderstanding of something, what i'm susposed to do to merge those numbers without string tricks and without using std but only using bitshift with a explanation how it works?
Each digit is c - '0'. When you get a new digit, you know that prior ones are one decimal place greater, so you multiply the current number by 10 and add the new digit:
char *s = "400";
int sum = 0;
while(*s >= '0' && *s <= '9') {
sum = 10 * sum + (*s - '0');
s++;
}

How can I delete the first zero(s) in a string? (Without using atoi) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.
Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}

Convert multiple bytes array into single long variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Im getting 10 bytes of data in an char array like which contains hex value
Data1[0] = 0x00,Data1[1] = 0x00,Data1[0] = 0x9 Data1[2]=0x01and so on...
Now I want to get this different array bytes into single long variable . Like
Long_var = 091...
How can do it any method can be accepted.
Sorry, i forgot to mention, i want to do this in 8051 code
There are generally two ways to do type punning in C, both involving arrays.
The first is to use a plain array of 32-bit integers, and then copy the bytes into that array:
char data[12];
// data is initialized...
uint32_t integers[3];
memcpy(integers, data, 12);
printf("First value is 0x%08x\n", integers[0]);
The other way is to use unions:
union type_punning_union
{
uint32_t integers[3];
char data[12];
};
union type_punning_union u;
// Initialize u.data...
printf("First value is 0x%08x\n", u.integers[0]);
Big important note 1: Your byte array have a size mismatch for matching all data evenly to 32-bit integers.
Big important note 2: The code shown above doesn't care about endianness, meaning the results printed might not be exactly what you expect.

How to create a file with random char in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have no problem in generate random numbers with rand()
I have to create an output file with a string of char e.g. "AQSDJEIOFHDUK"
When the result of rand ()%26 +1 is 1 I've to print in the file "A" when the result is "2" B and so on. I already know a priori how many char will be in the string let's say 50.
I've to do that in C language.
Which function I should use?
Strcat?
Simple set an array with random letters selected via rand()
#define RANDOM_STRING_LENGTH 50
char buf[RANDOM_STRING_LENGTH + 1];
for (size_t i = 0; i < RANDOM_STRING_LENGTH; i++) {
buf[i] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand() %26];
}
buf[RANDOM_STRING_LENGTH] = '\0';
puts(buf);
You can use the ASCII table of characters to generate valid characters. The base for the uppercase alphabet is at 65, so when you generate
int rnd = rand() % 26;
you can say something like
char new_char = 65 + rnd;
or as others have stated, it is easier to read
char new_char = 'A' + rnd;
which should give you one character in the ascii table of characters based off of the result of rand() % 26. Remember, in C an unsigned char is really just a data type that can range from the number 0, to the number 255.
(as stated below, a signed char can range from -126 to 127)
Things like 'a' and 'b' are translated into their ascii equivalent. (i.e. the corresponding number between 0 and 255 that they represent.)
(unless you are using UNICODE of course.)
I hope this helped.

I am finding difficulties to solve an expression [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am finding difficulties to solve an expression please help me to do so.
Declare four variables of type char. Initialize one variable to ‘z’. Initialize the other variables to the ASCII integer value for ‘z’, the ASCII octal value for ‘z’, and
the ASCII hexadecimal value for ‘z’.
Just declare a char variable and assign the value. To assign hex prefix with 0x, for octal use 0 and for decimal just write the number with no prefix.
The decimal ascii value of 'z' is 122.
#include <stdio.h>
int main() {
char a = 122;
char b = 0x7a;
char c = 0172;
char d = 'z';
putchar(a);
putchar(b);
putchar(c);
putchar(d);
}
All these char variables have the same value so four zs will be printed.

Resources