C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 17th, 2003, 09:57 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
ascii to hex

im having a real hard time converting a string of char's to the hex equivalent. lets say i have:

void main()
{
char x = 'A';
_asm
{
mov AL, x
mov x, AL
}
_putch(x);
}

i disassemble and of course, AL contains 41, the hex equivalent of 'A'. but when i put the char, it outputs 'A'. i tried doing:

cout << hex << x;

but that doesnt work either. i know this can be done in assembly, i DONT want to write this out with some gigantic switch statement, anyone help plz?

Reply With Quote
  #2  
Old April 17th, 2003, 10:28 PM
GNUbie's Avatar
GNUbie GNUbie is offline
Throws Rocks
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Cincinnati, Ohio
Posts: 392 GNUbie User rank is Corporal (100 - 500 Reputation Level)GNUbie User rank is Corporal (100 - 500 Reputation Level)GNUbie User rank is Corporal (100 - 500 Reputation Level)GNUbie User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 6 h 31 m 7 sec
Reputation Power: 8
__________________
Two things have come out of Berkeley, Unix and LSD.
It is uncertain which caused the other.

Reply With Quote
  #3  
Old April 17th, 2003, 10:57 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
heh, with this ugly piece of code i have accomplished it, but i'll check out that link.

int m;

while((m = cin.get()) != '\n')
{
cout << setbase(16) << m << "%";
}



edit: awww, man... i didnt even think to do that blatantly obvious, lol. uhhduhh, printf(). . but just out of curiosity, if i was some deranged maniac who was obsessed with assembly, how would i do it that way? a long time ago i wrote a hex to binary/hex to ascii calculator, but i cant seem to work it backwards. the way i did it was rotating -> masking -> add 30h ->etc... this seems like it should be ez since it's already sitting right there in the register in hex...?

Last edited by infamous41md : April 17th, 2003 at 11:03 PM.

Reply With Quote
  #4  
Old April 18th, 2003, 10:04 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
It's not easy because it's "sitting in the register as hex". It's a number. It's not stored in any certain base (well, I guess it's physically stored in binary, but this doesn't make any difference to your code - it might as well be store in decimal). So, you have to think of it as just a number (whether it's hex or decimal or binary or octal, etc). To get it in hex form, you must convert it into a string of hex characters (just as printf needs to convert it into a string of decimal characters to show it to the display). You can do this by grabbing the low 4 bits (x & 15), which is a number from 0..15 - this is the least significant digit. 0..9 should be outputted as '0'..'9' (i.e. ascii 48..57), 10..15 should be outputted as 'A'..'F' (i.e. ascii 65..70). Then move all the bits over 4 (x shr 4), so you can repeat the process.

Note that if you weren't doing this for an even computer base (i.e. base = 2^n, n=integer), for example, if you were doing it in decimal base-10, you would need to use (x % 10) and (x / 10) for getting the last digit, and shifting the number over 1 digit, which involves division (modulus is also a division), which is VERY slow. Because you are converting to base-16, this can be sped up as (x % 16) = (x & 15), and (x / 16) = (x shr 4). The bitwise & and shr instructions are very fast (shr is >> in C/C++, btw).

I hope this helps.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > ascii to hex


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway