any solution to convert given char*(number) to required unsi

Forum for questions, queries and information regarding the programming langauge C, and it's variations.

Moderator: Community Moderators

passionpatel
Posts: 2
Joined: Sun Jul 06, 2008 2:35 pm

any solution to convert given char*(number) to required unsi

Postby passionpatel » Sun Jul 06, 2008 2:37 pm

Given For e.g,
char input[] = "10011210582553796";

now Hexadecimal of 10011210582553796 is 2391249A8B74C4.

So output unsigned char* should have value,
unsigned char output[8] = {0x00,0x23,0x91,0x24,0x9A,0x8B,0x74,0xC4};

Any solution to convert given input char* to output char* with above mentioned requirement?

code should run on platform for which __int64/signed long long is not supported.

Thanx in advance...

User avatar
heebyjeebys
Posts: 1352
Joined: Thu Feb 28, 2008 10:24 pm
Contact:

Re: any solution to convert given char*(number) to required unsi

Postby heebyjeebys » Sat Jul 12, 2008 11:55 pm

passionpatel wrote:Given For e.g,
char input[] = "10011210582553796";

now Hexadecimal of 10011210582553796 is 2391249A8B74C4.

So output unsigned char* should have value,
unsigned char output[8] = {0x00,0x23,0x91,0x24,0x9A,0x8B,0x74,0xC4};

Any solution to convert given input char* to output char* with above mentioned requirement?

code should run on platform for which __int64/signed long long is not supported.

Thanx in advance...


right this is converting to hex with c#

Code: Select all

// Store integer 182
int decValue = 182;
// Convert integer 182 as a hex in a string variable
string hexValue = decValue.ToString("X");
// Convert the hex string back to the number
int decAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);


for VB

Code: Select all

label1.text = hex$(input.text).Convert.ToString()   


i shortend that, orginal page is : http://www.vb-helper.com/howto_dec_hex_oct_bin.html

for c++

Code: Select all

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

/*
*   To convert 53 to the character 'S':
*   char returnVal = hexToString('5', '3');
*/
char hexToAscii(char first, char second)
{
   char hex[5], *stop;
   hex[0] = '0';
   hex[1] = 'x';
   hex[2] = first;
   hex[3] = second;
   hex[4] = 0;
   return strtol(hex, &stop, 16);
}
int main(int argc, char* argv[])
{
   printf("%c\n", hexToAscii('5', '3'));
}


if you want to use the original basic, then just use:

Code: Select all

Print HEX$({value})
syntax. But it does it number by number so you will need a for/next loop:

Code: Select all

input "How many numbers ";n
dim$ values (n)
result$ = ""
for i = 1 to n
print "Value "; i ;
input value(i)
next
input "Save, print or display hex values [s,p,d]";r$
if r$ = "s" then goto save
if r$ = "p" then goto print
if r$ = "d" then goto display
else end
 
save:
open o,#2,"result.txt"
for i = 1 to n
print #2, hex$(value (i))
next i
close #2
end

print:
for i = 1 to n
lprint hex$(value(i))
next
end

display:
for i = to n
print hex$(value(i))
next
end


if theres anyproblem with the BASIC one, i'm sorry. I havn't done basic for quite a while. Thats a (should be) fully working program there (i just wrote that on the spot), provided that you use quick basic
Heeby's here! :)

passionpatel
Posts: 2
Joined: Sun Jul 06, 2008 2:35 pm

Re: any solution to convert given char*(number) to required unsi

Postby passionpatel » Tue Jul 15, 2008 5:33 pm

found solution

int number = 0;
char numberchars[] = "10011210582553796";
int i = 0;
int ans = 0;
int carry = 0;

char answerArray[100] = {0};

char remainderArray[100] = {0};
int remindex = 0;
int ansindex = 0;
int remainder = 0;

while( numberchars[i] != '\0' )
{
while( numberchars[i] != '\0' )
{
char currentchar[2] = {0};
currentchar[0]=numberchars[i];

int num = atoi(currentchar);
num += remainder;
remainder = 0;

if ( num < 2 )
{
remainder = num;
if(i>0)
answerArray[ansindex++] = '0';
}
else
{
remainder = num % 2;
int answer = num / 2;

char a[2] = {0};
itoa(answer,a,10);

answerArray[ansindex++] = a[0];
}

i++;
remainder *= 10;
}

char a[2] = {0};
int rval = remainder / 10;
itoa(remainder / 10,a,10);

remainderArray[remindex++] = a[0];
int size = sizeof(answerArray);
memcpy(numberchars,answerArray,sizeof(answerArray));
size = sizeof(answerArray);
memset(answerArray,0,sizeof(answerArray));
ansindex = 0;
remainder = 0;
i=0;
}

char int64[8] = {0};

for(int k=0;remainderArray[k]!= '\0';k++)
{
int64[7-(k/8)] |= ((remainderArray[k]-'0') << (k%8));
}


Return to “C / C# / C++”

Who is online

Users browsing this forum: No registered users and 1 guest