Large Hexadecimal (18 characters) to decimal conversions Incorre

ghz 8months ago ⋅ 98 views

I am trying to make a small C program to convert 18 character hexadecimal numbers (example: ec6260d8b431e420d6) to decimal. However every time I check the code I have with a online converter it's always wrong. I've read that my variable type might not be large enough for the numbers I'm attempting to convert, and to use unsigned variables when not using negative numbers. When I look at the code I am using I don't know where to change the aforementioned possible solutions. Here is the code I'm using:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char hex[100];
    printf("Enter a hexadecimal number: ");
    scanf("%s", hex);
    long decimal = 0;
    int i = 0;
    while (hex[i] != '\0') {
        int digit = hex[i] - '0';
        if (digit >= 10) {
            digit -= 7;
        }
        decimal = decimal * 16 + digit;
        i++;
    }
    printf("The decimal equivalent of %s is %ld.\n", hex, decimal);
    return 0;
}

Any pointers and suggestions as to what I'm doing wrong or not understanding would be greateley appreciated.

Answers

It looks like your code is almost there, but the issue is that your loop runs only once because you have not enclosed the serial port communication part within the loop. Additionally, you want the loop to continue running even if there's an error.

Here's a modified version of your code that includes the serial port communication inside the loop and ensures that the loop continues to run even if there's an error:

using System;
using System.IO.Ports;

public class Program
{
    public static SerialPort _serialPort;

    public static void Main(string[] args)
    {
        // Create a new SerialPort object
        _serialPort = new SerialPort();

        // Set the port name (COM1, COM2, etc.)
        _serialPort.PortName = "COM3";

        // Set baud rate
        _serialPort.BaudRate = 9600;

        // Set parity (none, odd, even, mark, space)
        _serialPort.Parity = Parity.None;

        // Set data bits
        _serialPort.DataBits = 8;

        // Set stop bits (one, two, or more)
        _serialPort.StopBits = StopBits.One;

        // Set handshake (none, requestToSend, requestToSendXOnXOff, xOnXOff)
        _serialPort.Handshake = Handshake.None;

        // Set read/write timeouts
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;

        try
        {
            // Open the serial port
            _serialPort.Open();

            // Run the loop indefinitely
            while (true)
            {
                try
                {
                    // Write data to the serial port
                    _serialPort.WriteLine("T");

                    // Read data from the serial port
                    string response = _serialPort.ReadLine();
                    Console.WriteLine("Totals list: " + response);

                    // Write data to the serial port
                    _serialPort.WriteLine("D");

                    // Read data from the serial port
                    response = _serialPort.ReadLine();
                    Console.WriteLine("Data list: " + response);
                }
                catch (TimeoutException ex)
                {
                    // Handle timeout exception (no data received within the specified timeout)
                    Console.WriteLine("Timeout error: " + ex.Message);
                }
                catch (Exception ex)
                {
                    // Handle other exceptions
                    Console.WriteLine("Error: " + ex.Message);
                }

                // Sleep for 5 seconds before the next iteration
                System.Threading.Thread.Sleep(5000);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            // Close the serial port if it's open
            if (_serialPort.IsOpen)
            {
                _serialPort.Close();
                Console.WriteLine("Serial port closed.");
            }
        }
    }
}

This code will run the loop indefinitely, attempting to read data from the serial port every 5 seconds. If there's an error during communication, it will be caught and logged, but the loop will continue running.