• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

signed integer to hex

 
Greenhorn
Posts: 29
  • Number of slices to send:
    Optional 'thank-you' note:
Hello! I am trying to take a signed integer and convert it to hex. I am able to get it to work when the integer is positive, but not when it's negative.
My thought process was to use this code (code listed below) to convert the negative integer to the 2s compliment binary. I then wanted to take the 2s compliment binary and convert it to an int. For example if I wanted -10 in hex I was going to use the below code to convert -10 to I then wanted to convert it so it would look like this At that point I could then use the below code to turn the to 18446744073709551606 (used a binary to decimal calculator to get this number)
Since I would now have a positive int,  I could just sick it into my original code get the hex for -10. The problem with this is that my code stores the binary number in an integer array. I couldn't find a way to convert that array to an integer. I'm new to all this so my plan could be complexly stupid and overly complex and there might be a simpler way to solve my original problem. If so I would appreciate any help. Thanks!
 
Marshal
Posts: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:
An array isn't an integer and an integer isn't an array.
There is no way to “convert” an int from one format to another; it is recorded in binary and that's that. Your array contains the eight chars which would represent your number. It is possible to display an integer in hexadecimal with the %x tag. Or make your array one too large, make the last element 0 and display it with the %s tag.

Don't use the int literals 48 and 55 which are not clear, and error‑prone (55 is wrong, by the way). Use the char literals '0' and 'a'. You may need to cast the expressions to (char).
 
Ranch Hand
Posts: 124
  • Number of slices to send:
    Optional 'thank-you' note:
Isn't there a String.format method that would do that
 
Damon McNeill
Ranch Hand
Posts: 124
  • Number of slices to send:
    Optional 'thank-you' note:

I am trying to take a signed integer and convert it to hex.



In Java, every integer is signed.

Why are you posting C code here? Are you confused?

EDIT: Apologies I did not realize this was a C forum. Converting from binary to hex is a simple matter of division. I will not have the time to give you the solution (as this is likely a homework assignment and the given code is unreadable).
 
Rancher
Posts: 508
15
  • Number of slices to send:
    Optional 'thank-you' note:
Hi Juan - assuming you want -255 to be displayed as -000000FF then you can just make a few modifications to your first attempt.

Check if n is negative, and if so, negate it (so it becomes >=0) and set a flag. Then when you print the hex array at the end, if the flag is set, precede the hex with a '-'.

I tried this with your code and it worked fine; does that sound ok?
 
John Matthews
Rancher
Posts: 508
15
  • Number of slices to send:
    Optional 'thank-you' note:
Alternatively, if you want -255 to be displayed as hex FFFFFF01 then store n in an unsigned variable (after the atoi()), and work with the unsigned variable instead of n in the rest of the code.
 
Campbell Ritchie
Marshal
Posts: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:

Damon McNeill wrote:Isn't there a String.format method that would do that

Only in Java®, and as you noticed, this is C code.
OP: You could consider a translation arrayRemember that you can assign that “String literal” to a char[] or a char* directly because of the way C handles text. But you need to assign space for a 0x11 (=17decimal) size array to accommodate the final 0.Find out whether you can use i & 0x0f (maybe better as follows:- i & 0x10 − 1) instead of the % operator. Remember that the bitwise and operator won't cause problems by giving you a negative result.
 
John Matthews
Rancher
Posts: 508
15
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:Remember that you can assign that “String literal” to a char[] or a char* directly


You don't even need to do that - works fine
 
Juan González
Greenhorn
Posts: 29
  • Number of slices to send:
    Optional 'thank-you' note:
Thank you everyone for the help, I really appreciate it! I was able to get my program working and now have a much better understanding about the topic. Thank you for such detailed responses, it really helped.
 
Campbell Ritchie
Marshal
Posts: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:

Juan González wrote:Thank you everyone . . .

That's a pleasure
Please show us the working version of your code; maybe it can be improved even more.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |