473,395 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Convert long into ASCII Char[]

3
hi

I have a long variable:

long id = 0x0457;

which in decimal is 1111

I need to convert id into a char array with its ascii values.

e.g

char arr[4];

arr[0] = '1';
arr[1] = '1';
arr[2] = '1';
arr[3] = '1';

thanks
Nov 7 '08 #1
2 9891
Banfa
9,065 Expert Mod 8TB
Try sprintf, or itoa although the latter function (itoa) while support by several compilers is actually non-standard and I would say may well suffer from re-entrancy issues since it returns a char * to a presumably static buffer.
Nov 7 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Use a common algorithm.

This 1111 is really

1 *1000 + 1 * 100 + 1 * 10 +1.

To see how many 100's there are you

1111/100

That gives the number of 100's. Now to get the digit in the 100's position you:

(1111/100) %10

Now you have a value between 0 and 9. It is an integer value. For a string you have to convert this to an ASCII char.

(1111/100) %10 +'0'


So, constuct a loop so you divide by 1000 then by 100 then by 10 etc and append the result to your string. When your divisor is 0, you are done.
Nov 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: loki | last post by:
hello, when i do ..... set xmlhttp = CreateObject("WinHTTP.WinHTTPRequest.5.1") ...... StrXmlhttpResponseText = xmlhttp.responseText StrXmlhttpResponseText = replace(StrXmlhttpResponseText,...
4
by: Kay | last post by:
how to convert int to char * ? I have found this in a web site. However, it doesn't work even I change itoa to atoi including stdlib.h char str; int i=567; str=itoa(i, str, 10);
4
by: John Smith | last post by:
Hello, Suppose I have the following C# code which I want to convert to VB: for (int i = 0; i < nFieldLength; i++) Console.Write((char) sValue); sValue is a byte array. The problem is...
5
by: tienlx | last post by:
Hi all, I'm have a problem that i don't know how to convert object to char in C# . Does anybody know ? Thanks.
2
by: dotnetchic | last post by:
I'm trying to parse a binary file to help me with some analysis...what I'd like to do is take a byte of data and display its ascii char value (37='%', 38='&', etc.). private void...
1
by: keithb | last post by:
How can I convert an ascii code into a character using C# in ASP.NET? Thanks, Keith
0
by: Bipin Mistry | last post by:
Hello all, I an new to XML read-write, and I am using "LibXml2" to work around with XML data. I am having 2 task to do (development in c++ using libxml2-2.6.24.win32) 1. To read & parse XML...
6
by: Aneesh E Warrier | last post by:
How can I convert Unicode Codepoint (dec) value to ASCII char? For example: 49324 is 사 a Korean alphabet, and I want to convert it to ASCII letter. Thanks!
1
by: tedpottel | last post by:
Hi, Newbe needs help, How can I convert a ascii code to a charter?? -Ted
8
by: AGRAJA | last post by:
how to convert unsigned to char? Ref: http://www.thescripts.com/forum/thread477545.html how do I print without the leading ffffff (yet the result should be char*)?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.