I am new to C programming. I'm writing a function that converts an integer into hexadecimal.
For some reason, I am getting a segmentation fault 11. Please advise. Thank you!
Here is the code for my function:
it converts the integer to binary first
adds 0s where it is needed so binary length would be multiples of 4
reverses the order of the binary
converts every 4 numbers into hexadecimal
void printHexadecimalForm( int X )
//Purpose: Print parameter X in hexadecimal form
//Output: Hexadecimal representation of X directly printed
//Assumption: X is non-negative (i.e. >= 0)
{
//[TODO] CHANGE this to your solution.
int input = X;
int output[32];
int i = 0;
while(input != 0){
if(input%2 != 0){
input = input - 1;
input /= 2;
output[i] = 1;
i++;
}
else{
input /= 2;
output[i] = 0;
i++;
}
}
while(i % 4 != 0){
output[i + 1] = 0;
i++;
}
for (int j = 0; j < i/2; j++)
{
int temp = output[j];
output[j] = output[i - 1 - j];
output[i - 1 - j] = temp;
}
int c, k = 0;
for(int z = 0; z < i; z += 4; ){
for (c = z; c < c + 4; c++){
k = 10 * k + output[c];
}
if(k == 0000){
printf("%d",0);
}
if(k == 0001){
printf("%d",1);
}
if(k == 0010){
printf("%d",2);
}
if(k == 0011){
printf("%d",3);
}
if(k == 0100){
printf("%d",4);
}
if(k == 0101){
printf("%d",5);
}
if(k == 0110){
printf("%d",6);
}
if(k == 0111){
printf("%d",7);
}
if(k == 1000){
printf("%d",8);
}
if(k == 1001){
printf("%d",9);
}
if(k == 1010){
printf("%c", 'A');
}
if(k == 1011){
printf("%c", 'B');
}
if(k == 1100){
printf("%c", 'C');
}
if(k == 1101){
printf("%c", 'D');
}
if(k == 1110){
printf("%c", 'E');
}
if(k == 1111){
printf("%c", 'F');
}
}
}
I suggest you to take deep breath and start all over again. First remember, there's no need to convert anything to binary. Everything is binary already.
Maybe this little piece, which retrieves two hexadecimal characters could help you to get on to the track: (this is just one method)
int n = 165; // this is the number we want to display in hex (165 is 0xa5)
int i, hexChar;
i = n & 0xF; // bitwise AND with 00...001111
if(i < 10) // look up to ASCII table for more info
hexChar = i + 48; // character '0' is code 48, '1' is 49 etc.
else
hexChar = i + 55; // character 'A' is code 65, 'B' is 66 etc.
printf("Rigth most hex: %c\n", hexChar);
i = n >> 4; // shift all bits 4 steps to the right
i = i & 0xF; // bitwise AND with 00...001111
if(i < 10) // look up to ASCII table for more info
hexChar = i + 48; // character '0' is code 48, '1' is 49 etc.
else
hexChar = i + 55; // character 'A' is code 65, 'B' is 66 etc.
printf("Second hex: %c\n", hexChar);
Related
#include<stdio.h>
#include<unistd.h>
void ft_putchar(char x){
write(1, &x, 1);
}
void ft_print_comb()
{
char i, j, k;
i = '0';
while(i <= 7){
i++;
j = i+1;
while(j <= 8){
j++;
k = j+1;
while(k <= 9){
k++;
ft_putchar(i);
ft_putchar(j);
ft_putchar(k);
ft_putchar(',');
ft_putchar(' ');
}
}
}
}
int main(){
ft_print_comb();
return 0;
}
I have tried to do couple changes but it either broke the code or kept giving me no output. What I am trying to do is create a function that displays all different combinations of three different digits in
ascending order, listed by ascending order. for loop and printf functions are not allowed.
Since you are using chars, you should compare with character literals instead of integers. For instance, the while loop is never entered because the ASCII code for '0' is 48, which is greater than 7.
while (i <= '7') {
j = i + 1;
while (j <= '8') {
k = j + 1;
while (k <= '9') {
ft_putchar(i);
ft_putchar(j);
ft_putchar(k);
ft_putchar(',');
ft_putchar(' ');
k++;
}
j++;
}
i++;
}
i is a character with a value of 48 (the ASCII code of '0') so the while loop is never entered. Set i this way:
i = 0;
#include<stdio.h>
#include<string.h>
void baseconversion(char s[20], int, int);
main()
{
char s[20];
int base1, base2;
printf("Enter the number and base:");
scanf("%s%d", s, &base1);
printf("Enter the base to be converted:");
scanf("%d", &base2);
baseconversion(s, base1, base2);
}
void baseconversion(char s[20], int b1, int b2)
{
int count = 0, r, digit, i, n = 0, b = 1;
for(i = strlen(s) - 1; i >= 0; i--)
{
if(s[i] >= 'A' && s[i] <= 'Z')
{
digit = s[i] - '0' - 7;
}
else
{
digit = s[i] - '0';
}
n = digit * b + n;
b = b * b1;
}
while(n != 0)
{
r = n % b2;
digit = '0' + r;
if(digit > '9')
{
digit += 7;
}
s[count] = digit;
count++;
n = n / b2;
}
for(i = count - 1; i >= 0; i--)
{
printf("%c", s[i]);
}
printf("\n");
}
I know this code converts chars to integers, but I've never seen it before, never used C.
If someone could explain a bit of what's going on with the conversions I'd appreciate it, thank you.
I understand that at some point the digits get reversed.
It does it through two steps, the first one is converting the number into its decimal form, in this part:
for(i = strlen(s) - 1; i >= 0; i--) //Start from right to left
{
if(s[i] >= 'A' && s[i] <= 'Z')
digit = s[i] - '0' - 7; //Get the integer equivalent to the letter
else
digit = s[i] - '0'; //Get the integer equivalent to the numerical character
n = digit * b + n; //Add the value of this character at this position
b = b * b1; //The value of the next character will be higher b times
}
Then it transforms the result to the desired base, in this part:
while(n != 0)
{
r = n % b2; //The remaining will be the rightmost value for the new base
digit = '0' + r; //Get the integer for the new digit
if(digit > '9')
digit += 7; //Here the digit will be a letter
s[count] = digit;
count++;
n = n / b2; //Remove the rightmost digit to get the next one
}
I am coding in C in a university course and we got a project to take equations from the user and give solutions for matrices etc...
My problem is that I am trying to use atof() function and for a reason I can't find in the same loop once it works and the other times it doesn't.
I have tried already other functions to replace atof like strtod but it doesn't work as well.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdbool>
void main()
{
int num, check = 0,i,j,k=0,len1=0;
char equ[80],tempx[20],tempy[20], tempz[20], tempd[20];
double *x, *y, *z, *d;
printf_s("Number of equations (1-3): ");
scanf_s("%d", &num);
getchar();
while (check == 0) //a check to see if we got a number between 1-3.
{
if (num > 0 && num < 4)
check = 1;
else
{
printf_s("Please enter a number between 1-3.\n");
printf_s("Number of equations (1-3): ");
scanf_s("%d", &num);
}
}
x = malloc(sizeof(double)*num);
if (!x) exit(1);
y = malloc(sizeof(double)*num);
if (!y) exit(1);
z = malloc(sizeof(double)*num);
if (!z) exit(1);
d = malloc(sizeof(double)*num);
if (!d) exit(1);
for (i = 0; i < num; i++) //getting the equations and putting them into the matrix
{
printf_s("Enter equation %d: ", i + 1);
gets_s(equ, sizeof(equ));
len1 = strlen(equ);
for (j = 0; j <len1 ; j++)
{
if (equ[j] == 'x')
{
k = 0;
while ((equ[j-k] != '+' || equ[j-k] != '-') && j-k>=0)
{
tempx[j-k] = equ[j-k];
k++;
}
x[i] = atof(tempx);
}
else if (equ[j] == 'y')
{
k = 0;
while ((equ[j-k] != '+' || equ[j-k] != '-') && j - k >= 0)
{
tempy[j-k] = equ[j-k];
k++;
}
y[i] = atof(tempy);
}
else if (equ[j] == 'z')
{
k = 0;
while ((equ[j - k] != '+' || equ[j - k] != '-') && j - k >= 0)
{
tempz[j-k] = equ[j - k];
k++;
}
z[i] = atof(tempz);
}
else if (equ[j] == '=')
{
k = 0;
while (equ[j+k])
{
tempd[k] = equ[j + k];
k++;
}
d[i] = atof(tempd);
}
}
}
free(x);
free(y);
free(z);
free(d);
}
I expected to get the same result in d[i] as I did in x[i] but every time I try to print d[i] I get 0.0000. When I tried the function _strrev on tempd inside atof I got the reverse result inside d[i].
So the problem was that in the last loop i inserted a string that start with "=" and not a number. Apparently atof() doesn't work when the first char is not a number.
My intent with this code is to make a vignere cipher.
I can compile my code with gcc but when I run the program and pass 2 strings in the command line I have a segmentation fault. Initially I had tried to control the int value of j with a separate if-statement in my for-loop such as:
if( i % strlen(msg) == 0) {
j = 0;
}
else {
j++;
}
However, it was not clear to me if this would produce my intended result, to execute an expression in this if-statement and to then continue with the next if-statement. So, I substitute a conditional statement and the code compiles. However, in both cases I get a segmentation fault, using either method with j.
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *msg = argv[1];
char *key = argv[2];
int i, j, sl, ky, mg;
sl = strlen(key);
j = 1;
int encrypt[strlen(msg)];
if(isalpha(key) && argc == 3) {
;
}
else
perror("run program from cmd line with 2 strings \n");
return 1;
for(i = 1; i <= sl; i++) {
(i % strlen(key) == 0) ? j = 0 : j++;
mg = msg[i];
ky = key[j];
if(isalpha(msg[i])) {
if(isupper(msg[i])) {
msg[i] = 'A' + (('A' - msg[i]) + ('A' - key[j]) % 26);
}
else if(islower(msg[i])) {
msg[i] = 'a' + (('a' - msg[i]) + ('a' - key[j]) % 26);
}
else {
msg[i] = msg[i];
}
}
}
return 0;
}
Okay well I fixed some oversights such as lines 38 and 42. This snippet of code works as a cipher but it only works some of the time.
for(i = 0; i < sl; i++) {
(i % strlen(key) == 0) ? j = 0 : j++;
mg = msg[i];
ky = key[j];
if(isalpha(msg[i])) {
if(isupper(msg[i])) {
encrypt[i] = 'A' + ((( msg[i] - 'A') + (key[j]) - 'A') % 26);
}
else if(islower(msg[i])) {
encrypt[i] = 'a' + (((msg[i] - 'a') + (key[j]) - 'a') % 26);
}
else {
encrypt[i] = msg[i];
}
}
}
for(i = 0; i < strlen(msg); i++) {
printf("%c", encrypt[i]);
I'm trying to create a code that converts a decimal into any base between 2 and 16. But I don't know how to write a new value in my string.
void base_conversion(char s[], int x, int b) {
int j, y;
j = 0;
// your code here
while(b > 1 && b < 17) {
if (x < 0) {
x = (-x);
}else if(x == 0) {
s[j] = '\0';
}
x = (x/b);
while(x > 0) {
y = (x%b);
if (y == 10) {
s[j] = 'A';
}else if(y == 11) {
s[j] = 'B';
}else if(y == 12) {
s[j] = 'C';
}else if(y == 13) {
s[j] = 'D';
}else if(y == 14) {
s[j] = 'E';
}else if(y == 15) {
s[j] = 'F';
}else{
s[j] = y;
}
}
}j = j + 1;
}
You were almost there, although several mistakes, so I have "improved" your code. The infinite loop testing the base which needed to be done once only. The while() loops weren't quite organised right - x/b being done outside the digit extraction loop. Another change I made was to use a lookup array to convert each digit to a character, which saves a lot of laborious testing. I also returned the string passed as the function value - might as well add a tad more functionality. In the case of passing a bad base value, I could have returned NULL instead of an empty string. Note also I update j in the same statements where I use it as an index, which makes the code a little more fluent.
#include <stdio.h>
char *base_conversion (char *s, int x, int b) {
char charr[] = "0123456789ABCDEF";
int i, j = 0, len, digit, neg = 0;
*s = 0; // terminate the string passed
if (b < 2 || b > 16) // check the base
return s; // return an empty string
if (x < 0) {
x = -x; // adjust for negative input
neg = 1;
}
do {
digit = x % b; // extract each l.s. digit
s[j++] = charr [digit]; // convert to character
} while (x /= b); // implicitly test for 0
if (neg) // negative input
s[j++] = '-'; // append a minus sign
s[j] = 0; // terminate the string
// reverse the string
len = j;
for (i=0; i<len/2; i++) {
digit = s[i];
s[i] = s[--j]; // pre-decrement j to next char index
s[j] = digit;
}
return s;
}
int main () {
int n;
char strig[65];
for (n=1000; n>=-1000; n-=2000) {
printf ("Binary %d: %s\n", n, base_conversion (strig, n, 2));
printf ("Ternary %d: %s\n", n, base_conversion (strig, n, 3));
printf ("Octal %d: %s\n", n, base_conversion (strig, n, 8));
printf ("Decimal %d: %s\n", n, base_conversion (strig, n, 10));
printf ("Hexadecimal %d: %s\n", n, base_conversion (strig, n, 16));
}
return 0;
}
Program output:
Binary 1000: 1111101000
Ternary 1000: 1101001
Octal 1000: 1750
Decimal 1000: 1000
Hexadecimal 1000: 3E8
Binary -1000: -1111101000
Ternary -1000: -1101001
Octal -1000: -1750
Decimal -1000: -1000
Hexadecimal -1000: -3E8