Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Hexadecimal to decimal

Status
Not open for further replies.

pnielsen

Member level 2
Member level 2
Joined
Mar 18, 2002
Messages
50
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
319
I am trying to convert hexidecimal to decimal in C code. If I uncommit the // the program will display ox141E into decimal. But I am stuped at the syntax to convert the hexadacimal to decimal. Any help would be tremendous.

Thank You

Philip




Code:
void Display_Result(void)
{
	
   // value 0x141E;
    LCD_Char_Position(0,6);
    LCD_Char_PrintInt8(HYT221_Read_Buff[0] & 0x3f);		//Mask HYT humidity the highest two status bits
	LCD_Char_PrintInt8(HYT221_Read_Buff[1]);
	LCD_Char_PrintString("      ");	
	LCD_Char_Position(1,6);
    sprintf(HYT221_Read_Buff, "0x%X", decimal_value);
  //   temp =(buffer, "%d", HYT221_Read_Buff[1]);        

  //   temp =(buffer, "%d", value);
  //   temp = (temp *100/16383);
  //   LCD_Char_Position(1,6);
  //  LCD_Char_PrintNumber(temp);
    
 }

/* [] END OF FILE */
 
Last edited by a moderator:

These two lines are incorrect.

// temp =(buffer, "%d", HYT221_Read_Buff[1]);
// temp =(buffer, "%d", value);

The RHS look like arguments to some function like printf but there are mistakes. I don't know how arguments to a function can be assigned to a variable. Post the full code so that it can be fixed.
 

You can try the sscanf function. Use %X for the format field and give it the address of the variable to store the result.
Something like this: sscanf(HexNumber,"%X",&Result);.

The other method which may use less code is to convert each character to it's decimal value and add them together:
Result = (HexValue & 0xF000) * 4096;
Result += (HexValue & 0x0F00) * 256;
Result += (HexValue & 0x00F0) * 16;
Result += HexValue & 0x000F;

If you want to display using the code you already have, try changing the sprintf so it has %d in the format field instread of %X.

Brian.
 

Hello!

I am trying to convert hexidecimal to decimal in C code.

You probably don't want to convert hexadecimal to decimal, but binary to text.
- Hexadecimal is just a representation to make binary readable by humans. If you write
a = 0x141E, a is not a hexadecimal value, it is just a 2-byte binary value. The most
significant byte is 20 and the least significant byte is 30. Hexadecimal does not exist as such
in your processor.
- A character LCD expects text.

Now if you want to display the decimal value of the variable (represented in hexa by 0x141E,
which is 5150), then you have to separate its 4 digits.

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
uint8 t, h, d, u; // For thousands, hundreds, tens (d for deca since t is used), u for units
uint16 val = 5150;
 
t = val / 1000; // You get t = 5
val -= 1000 * t; // You get 5150 - 5000 = 150;
h = val / 100; // You get h = 1
val -= 100*h; // You get val = 150 - 1*100 = 50
d = val/10; // You get d = 5;
u = val-10*d; // You get finally u = 0;
 
//Then, move to a place where there is enough space, and write the corresponding ascii values.
LCDWrite('0'+t);
LCDWrite('0'+h);
 
etc...



That should make it, right?

Note that this algorithm is inefficient because it uses divisions. I wrote another one on this
forum a few weeks ago, which is quite efficient because it replaces divisions by multiplications,
and which works for all values up to 2^14 if I remember correctly.

Dora.
 

You can try the sscanf function. Use %X for the format field and give it the address of the variable to store the result.
Something like this: sscanf(HexNumber,"%X",&Result);.

The other method which may use less code is to convert each character to it's decimal value and add them together:
Result = (HexValue & 0xF000) * 4096;
Result += (HexValue & 0x0F00) * 256;
Result += (HexValue & 0x00F0) * 16;
Result += HexValue & 0x000F;

If you want to display using the code you already have, try changing the sprintf so it has %d in the format field instread of %X.

Brian.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Complete Code:
Code:
/*******************************************************************************
* File Name: main.c
*
* Version: 1.0
*
* Description:
*  
*
* Parameters used:
*  I2CM Master
*   Implementation        Fixed function
*   Data rate            100kbps
*   SDA SCL pin config  Open drain, drives low
*   Pull-up resistors    2.67k each
*
*
*
********************************************************************************
* Copyright 2012, Cypress Semiconductor Corporation. All rights reserved.
* This software is owned by Cypress Semiconductor Corporation and is protected
* by and subject to worldwide patent and copyright laws and treaties.
* Therefore, you may use this software only as provided in the license agreement
* accompanying the software package from which you obtained this software.
* CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*******************************************************************************/

#include <device.h>
#include <string.h>
/* HYT221 command define */
#define HYT_MR	0
#define HYT_DR	1
/* The I2CM Slave address by default in a PSoC device is 8 */
#define I2CM_SLAVE_ADDRESS    (0x28u)

/* The I2CM buffer define */
uint8 HYT221_Read_Buff[4] = {0};
uint8 HYT221_Write_Buff[4] = {0};

/* The HYT221 function define */
void HYT_MR_Initialization(void);
void HYT_DR_Operation(void);

/* The Display function define */
void  Display_Result(void);

/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*  main function performs following functions:
*
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
void main()
{
	/* Start I2CMm */
	I2CM_EnableInt();
    I2CM_Start();
    /* The LCD will display Standare info */
    LCD_Char_Start();
	LCD_Char_ClearDisplay();
	LCD_Char_Position(0,0);
    LCD_Char_PrintString("Hraw:");
	LCD_Char_Position(1,0);
    LCD_Char_PrintString("Traw:");

    /* Enable global interrupts - required for I2CM */
    CyGlobalIntEnable;

    for(;;)
    {
		HYT_MR_Initialization();
		HYT_DR_Operation();
		Display_Result();
        /* Delay introduced for ease of reading LCD */
        CyDelay(3000u/*ms*/);

    }  /* End forever loop */
} /* End main */

/*******************************************************************************
* Function Name: HYT_DR_Operation
********************************************************************************
*
* Summary:
*  Read HYT measurement result and put into HYT221_Read_Buff
*
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
void HYT_DR_Operation(void)
{
	uint8 status = 0;
	uint8 i;
	
	status = I2CM_MasterSendStart(I2CM_SLAVE_ADDRESS, HYT_DR);
	if(I2CM_MSTR_NO_ERROR == status) /* Check if transfer completed without errors */
	for(i=0; i<4; i++)
	{
		if(i < 3)
		{
			HYT221_Read_Buff[i] = I2CM_MasterReadByte(I2CM_ACK_DATA);
		}
		else
		{
			HYT221_Read_Buff[i] = I2CM_MasterReadByte(I2CM_NAK_DATA);
		}		
	}
	else
	{
		LCD_Char_Position(0,6);
	    LCD_Char_PrintString("Bus Error");
		LCD_Char_Position(1,6);
	    LCD_Char_PrintString("Bus Error");		
	}
	I2CM_MasterSendStop(); /* Send Stop */		
}

/*******************************************************************************
* Function Name: HYT_MR_Initialization
********************************************************************************
*
* Summary:
*  Initializate HYT enable a measure cycle
*
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
void HYT_MR_Initialization(void)
{
	uint8 status = 0;
	
	status = I2CM_MasterSendStart(I2CM_SLAVE_ADDRESS, HYT_MR);
	if(I2CM_MSTR_NO_ERROR == status) /* Check if transfer completed without errors */
	{
		//Delay to debug - If routine execute to here, only stop need to archive MR operation
		CyDelay(1);
	}
	else
	{
		LCD_Char_Position(0,6);
	    LCD_Char_PrintString("Bus Error");
		LCD_Char_Position(1,6);
	    LCD_Char_PrintString("Bus Error");		
	}
	I2CM_MasterSendStop(); /* Send Stop */		
}


/*******************************************************************************
* Function Name: Display_Result
********************************************************************************
*
* Summary:
*  Update LCD with latest result
*
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
void Display_Result(void)
{
	
   // value 0x141E;
    LCD_Char_Position(0,6);
    LCD_Char_PrintInt8(HYT221_Read_Buff[0] & 0x3f);		//Mask HYT humidity the highest two status bits
	LCD_Char_PrintInt8(HYT221_Read_Buff[1]);
	LCD_Char_PrintString("      ");	
	LCD_Char_Position(1,6);
//    sprintf(HYT221_Read_Buff, "0x%X", decimal_value);
  
//    sscanf((HYT221_Read_Buff,%x,&Result);
  //   temp = (temp *100/16383);
  //   LCD_Char_Position(1,6);
  //  LCD_Char_PrintNumber(temp);
    
 }

/* [] END OF FILE */
 
Last edited by a moderator:

Now you have completely commented out the LCD routine so nothing will display.

In C, the sprintf, fprintf,and printf routines use a format field to control how the number is to be displayed. As draemon stated, a number is just a number, hex, binary, decimal and characters are just different ways of showing it. The format field controls which way the number is to be displayed or saved to a string or file. If you use "%d" it will display in decimal, %x will show the same number in hexadecimal , and %c will show it as equivalent characters and so on. If you want to control leading zeroes or decimal places, add the number (in decimal) of digits before and after a decimal point in the format field, for example %03.2f will show it with up to 3 leading zeroes and two decimal places.

Brian.
 
I am using Cypress MCU. It is a 8051. The actual part number is CY8C3866AXI-040, TQFP100. The compiler is made by Kiel. PK51 Professiona; Kit for PSOC
 

I have tried to use printf but I get error:
Verify Checksum...
Device 'PSoC 3 CY8C3866AX*-040' was successfully programmed at 04/12/2013 15:33:39.
Warning: Cannot set breakpoint: 'printf:read'. Encountered error (No symbol "printf" in current context.). _________________
Continuing target program
The target program has stopped at: file: line: -1 function: _PUTCHAR address: 0x00001C7F


#include <device.h>
#include <stdio.h>
#include <stdarg.h>

Any ideas? I think my compiler is very limited. It came with cypress free.
 

The issue is closed. If anyone is interested.


void Display_Result(void)
{
unsigned int result;


float buffer;
float final;
float Voltage;


union
{
unsigned int integer;
unsigned char byte[2];
}

temp16bitint;
temp16bitint.byte[0]=HYT221_Read_Buff[0] & 0x3f;
temp16bitint.byte[1]=HYT221_Read_Buff[1];
result = temp16bitint.integer;
LCD_Char_Position(1u,8u);
buffer=result;
final = ((buffer*100)/16383);
LCD_Char_PrintNumber(final);




Voltage=(final/1);

IDAC8_1_SetValue(Voltage);
// CyDelay(1500u/*ms*/);
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top