Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I convert a var decimal val into equivalent ASCII char or Hex value?

I wish to convert a variable decimal value (checksum) into the equivalent ASCII character or Hex value(\x). This is so I may send the checksum decimal value as a ASCII character to my Rs232 port using LabWindows CVI.
 
 
 
 
0 Kudos
Message 1 of 9
(5,820 Views)
Hi,
  a decimal number is still a number in Hex format. It's the view of the actual data value that gives you it in Hex.
Interms of converting it to Ascii, simply use the convert to U8, and then use the typecast function.(defaults to string type)
 
Hope that helps
Sacha Emery
National Instruments (UK)
// it takes almost no time to rate an answer Smiley Wink
0 Kudos
Message 2 of 9
(5,810 Views)

Hi - ok I misread that - sorry CVI (not LabVIEW)

Code snippet attached :

#include <userint.h>
#include <ansi_c.h>

static unsigned int x = 65;
static unsigned char y;
y = (unsigned char)x;

// for showing the information only :
printf("X = %d, Y = %c",x,y);

 

Thanks

Sacha Emery

 

// it takes almost no time to rate an answer Smiley Wink
0 Kudos
Message 3 of 9
(5,809 Views)

I tried your example, but the code would not compile, with warning errors starting at the line y = (unsigned char)x;          

the error warnings were as below:

ascii.c - 5 errors,1 warning
  7, 3     Redeclaration of 'y' previously declared at ascii.c:6.
  7, 3     Warning: Inconsistent linkage for 'y' previously declared at ascii.c:6.
  7, 21    Initializer must be constant.
  7, 21    Too many initializers.
  10, 8    syntax error; found 'string constant' expecting ')'.
  10, 29   Redeclaration of 'printf' previously declared at stdio.h:194.

0 Kudos
Message 4 of 9
(5,799 Views)
Hi,
  incredibly strange that didn't work for you. I did it in the interactive window rather than create an entire project for it so the declarations and stuff might not have gone into the correct place for you.
What version of CVI are you using?
 
Here's a project in cvi 7.1.
It's only a .c file being used, so you could always make a new project file if you're on a previous version
 
Thanks

Sacha Emery
National Instruments (UK)
// it takes almost no time to rate an answer Smiley Wink
0 Kudos
Message 5 of 9
(5,793 Views)

I am using Cvi version 6.

Also I opened your .zip file and there is no file/data in it to extract.

 

 

0 Kudos
Message 6 of 9
(5,790 Views)

Hi,

  I think you have greater issues than not being able to run the code then.

I checked the files are in there, and that they run ok. Do you have another computer with internet access that you can pull down the files with?

Do you have another PC with LabWindows/CVI that you can try the code snippet on I embedded in the previous message?

Thanks

Sacha Emery

National Instruments (UK)

// it takes almost no time to rate an answer Smiley Wink
0 Kudos
Message 7 of 9
(5,776 Views)
I tried another computer using Winzip version 8; the zip file is empty, maybe you are using another version of Winzip?
 
Please can you check using another computer that the files are there in the zip folder?
 
 
 
 
0 Kudos
Message 8 of 9
(5,667 Views)
I just opened the zip file straight from the post, (not downloaded and saved first) and it worked fine.  Very odd indeed.
0 Kudos
Message 9 of 9
(5,648 Views)